I have been trying to make a program in which, I add data of numbers but in texts, that these are added to a listbox and later make the sum of said values, only that they have explained to me that once the data has been added, now I must do a for loop to go through each data of the first array and thus be able to convert each one of them to a numeric value. Only when doing the conversion on the add button, it tells me that it is not in the correct format.
I declared the array in the public partial class like this: string [] array;
private void bt_capturar_Click(object sender, EventArgs e)
{
string datos = txb_numeros.Text;
arreglo = datos.Split(',');
lbx_elementos.Items.Clear();
foreach (string elementos in arreglo)
{
lbx_elementos.Items.Add(elementos);
}
}
private void bt_sumar_Click(object sender, EventArgs e)
{
int suma = 0;
for (int x = 0; x < arreglo.Length; x++)
{
int numero;
numero = int.Parse(arreglo[x]);
suma = suma + numero;
}
MessageBox.Show("La suma es igual a " + suma.ToString());
}
string[] arreglonotstring[] array? What exactly goes wrong?int.Parsemight crash when the string isn't a proper integer (like"string"or even"13.2").