I'm trying to add textbox and numericUpandDown values to an array, but it doesn't seem to be working.
Carro []carros = new Carro[1];
private Carro carro;
public Form1()
{
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
this.carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
}
private Carro[] AdicionarArray(Carro carro, Carro[] array)
{
AdicionarCarro();
int novoTamanho = array.Length + 1;
Carro[] carros = new Carro[novoTamanho];
for (int i = 0; i < array.Length; i++)
{
carros[i] = array[i];
}
carros[novoTamanho] = carro;
return carros;
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarArray(carro, carros);
}
When I type the values and click on the "buttonGravar", it gives me this
Error:
I'd be much delighted to get some tips/help on it.
Using System.Collection.Generic.List<T> would be much simpler, since it doesn't have a fixed size:
List<Carro> carros = new List<Carro>();
carros.AddRange(array);
carros.Add(carro);
return carros;
Better way:
private List<Carro> Carros;
public Form1()
{
Carros = new List<Carro>();
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
var carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
Carros.Add(carro);
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarCarro();
}
To help you understand your code:
carros[novoTamanho] = carro;
should be
carros[novoTamanho - 2] = carro;
Reason:
Array index starts from 0. novoTamanh represents new length (starting at 1, not 0 unlike index), which is outside array.
It's an index out of range exception because your array Carro is of size tmanho:
Carro[] carros = new Carro[novoTamanho];
and carros can contain exactly "novoTamanho" items indexed from "0" to "novoTamanho -1"
You can simply solve this by defining:
int novoTamanho = array.Length + 2;
Or if you do not want to manage indexes, use Lists:
List<Carro> listCarro = new List<Carro>;
listCarro.AddRAnge(array);
listCarro.Add(carro);
return listCarro.ToArray();
Related
I got a list of 10 items.
when a user puts in a value in "listText" its comparing to the first item in displayArraysString.
Lets say its not the first item in the displayArraysString list, then it doesnt do anything (Because I dont have a loop)
How do I create a loop that will check through my list and display the messagebox once it finds it. I tried with a try catch loop but that didnt work for me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Arrays
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] numbers = new int[5];
List<int> numbersList = new List<int> ();
string text = System.IO.File.ReadAllText(#"C:Directory\list.txt");
private void Form1_Load(object sender, EventArgs e)
{
//numbers[0] = 12;
//numbers[1] = 10;
//numbers[2] = 25;
//numbers[3] = 10;
//numbers[4] = 15;
//numbersList.Add(23);
//numbersList.Add(32);
//numbersList.Add(35);
}
//Array Print
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < numbers.Length; i++)
displayArrays.Text += numbers[i].ToString() + ", ";
}
//List Print
private void button2_Click(object sender, EventArgs e)
{
for (int o = 0; o < text.Length; o++)
{
displayArraysString.Text += text[o].ToString();
if (listText.Text == displayArraysString.Text)
{
MessageBox.Show("Found a match!");
}
else
{
//Something.
}
}
}
}
}
You are trying something wrong here,
as the file you have read from the path in string , it will match a single character for listText, so there will be never a match,
I did it with string array, to convert the text data into string array of every words in it. If you search now than match will be found for listText.
try this code:
namespace Arrays
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] numbers = new int[5];
List<int> numbersList = new List<int> ();
string text = System.IO.File.ReadAllText.Text(#"C:\Directory\list.txt");
string[] displayStringArrays = null;
private void Form1_Load(object sender, EventArgs e)
{
//numbers[0] = 12;
//numbers[1] = 10;
//numbers[2] = 25;
//numbers[3] = 10;
//numbers[4] = 15;
//numbersList.Add(23);
//numbersList.Add(32);
//numbersList.Add(35);
}
//Array Print
private void button1_Click(object sender, EventArgs e)
{
displayArrays.Text = listText.Text;
}
//List Print
private void button2_Click(object sender, EventArgs e)
{
displayStringArrays = text.Split('\n').ToArray();
foreach (var item in displayStringArrays)
{
displayArraysString.Text += item;
if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)
{
MessageBox.Show("Found a match!");
}
else
{
//Something.
}
}
}
}
}
replace this code with your code. I checked this it is working fine now.
I am thinking of finding an easy way to put numbers into an array using a for loop. I have made the easy design with text showing how the name system works!
My code is like this:
double[,] kast = new double[3, 8];
string[] navn = new string[8];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSorter_Click(object sender, EventArgs e)
{
for(int i = 1; i > 4; i++)
{
for(int x = 1; i > 9; i++)
{
kast[i, x] = Convert.ToDouble(txtKast + i + x + .Text);
}
}
}
I don't know how I will make it work with the Convert.ToDouble part since "kast" is a double array.
I'm having a problem printing out an array that I have created through a function.
All it says in the MessageBox is System.int32[], what have I done wrong?
private int[] sekunder(int tid)
{
int sekunder, minuter, timmar;
sekunder = tid;
minuter = sekunder / 60;
timmar = minuter / 60;
int[] beräknaTid = { sekunder, minuter, timmar };
return beräknaTid;
}
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
MessageBox.Show(Convert.ToString(sekunder(tid)));
}
try this:
Array Contains Multiple Elements You Need to Iterate through them
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
foreach (var item in sekunder(tid))
{
MessageBox.Show(Convert.ToString(item));
}
// for comma separated
//use this : MessageBox.Show(string.Join(",",sekunder(tid)))
}
you can also join all values in your array and show them
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
MessageBox.Show(string.Join(", ",sekunder(tid)));
}
How to access array from another method in this example? I'm new in c# and i really appreciate if you will help me. Thanks in advance!
private void button1_Click(object sender, EventArgs e) {
int[] array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
int[] array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
As Jon Skeet mentioned in his comment, local variables are by definition local to the method which defines them. If you want something to be visible globally, you need to make it an instance variable, or return it from the method.
Assuming that only array1 needs to be visible, it suffices to declare that one outside.
// Declare the array globally.
int[] array1 = new int[5];
private void button1_Click(object sender, EventArgs e)
{
// Initially the array inside this method.
for(int i=0;i<5;i++)
array1[i]=i;
}
private void button2_Click(object sender, EventArgs e)
{
int[] array2 = new int[5];
// Copy from the global array
for(int i=0;i<5;i++)
{
array2[i]=array1[1];
}
}
Declare both arrays outside to enable access.
int[] array1, array2;
private void button1_Click(object sender, EventArgs e) {
array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
I have a folder browser button and a combobox which shows selected path. I want it keep previously selected paths. I wrote this code and it works fine. But I wanna know if there is more efficient way to do this.
private void Form1_Load_1(object sender, EventArgs e)
string hurrem, hurrem2, hurrem3, hurrem4, hurrem5;
hurrem = Settings.Default["com1"].ToString();
hurrem2 = Settings.Default["com2"].ToString();
hurrem3 = Settings.Default["com3"].ToString();
hurrem4 = Settings.Default["com4"].ToString();
hurrem5 = Settings.Default["com5"].ToString();
comboBox2.Items.Add(hurrem);
comboBox2.Items.Add(hurrem2);
comboBox2.Items.Add(hurrem3);
comboBox2.Items.Add(hurrem4);
comboBox2.Items.Add(hurrem5);
comboBox2.SelectedIndex = 0;
and
private void button1_Click(object sender, EventArgs e)
{
//..code
Settings.Default["com5"] = Settings.Default["com4"];
Settings.Default["com4"] = Settings.Default["com3"];
Settings.Default["com3"] = Settings.Default["com2"];
Settings.Default["com2"] = Settings.Default["com1"];
Settings.Default["com1"] = path1.ToString();
Settings.Default.Save();
You can use the LimitedStack from here: Limit size of Queue<T> in .NET?. You could then add methods for serializing/deserializing the data using Settings.Default.
public void Save()
{
int i = 0;
foreach (var item in _stack)
{
Settings.Default["com" + i++] = item;
}
Settings.Default.Save();
}
public void Load()
{
for (int i = 0; i < Limit; i++)
{
_stack.Add((T)Settings.Default["com" + i++]);
}
}
Storing the items (assuming you have the limited stack saved in a variable called myStack):
myStack.Push(path1.ToString());
myStack.Save();
Adding the items to the combobox:
for (int i = 0; i < myStack.Limit; i++)
{
comboBox2.Items.Add(myStack.Pop());
}