Reputation: 984
Basically the first for statement below creates a list of testvalue labels depending on the user input.
The second for statement is supposed to work out the total of the dynamic labels created but when i play it i get an error saying "Input string was not in a correct format." relating to tots += double.Parse(value[p].ToString());
Any help would be appreciated. Thanks
ArrayList value = new ArrayList();
int p =0;
for (int i = 0; i < weight.Count; i++)
{
Label test = new Label();
System.Drawing.Point l8 = new System.Drawing.Point(440, 48 + s);
test.Location = l8;
value.Add(test);
k += 35;
Label l2 = testpercent1[i] as Label;
Double.TryParse(l2.Text.Trim(), out label2);
Label l = testpercent2[i] as Label;
Double.TryParse(l.Text.Trim(), out label1);
Double testvalue = Math.Round(((label1 * .3) + (label2 * .4)));
test.Text = testvalue.ToString();
}
Double total = 0;
for (int p = 0; p < value.Count; p++)
{
tots += double.Parse(value[p].ToString());
}
Upvotes: 0
Views: 222
Reputation: 16623
It's obvious you are adding a control to the ArrayList. So this don't work:
tots += double.Parse(value[p].ToString());
I reccomend you to do:
value.Add(test.Text);
Then:
tots += double.Parse(value[p]);
PS:
Please use a List<string>
instead of an ArrayList
.
Upvotes: 1
Reputation: 112512
Storing your data in labels is a very bad idea. Use a data structure that is better suited for this purpose like an array or a list of doubles. Only use the lables for displaying the data.
double[] values = new double[N];
Label[] lables = new Label[N]; // Only for display!
// Calculate (just as an example)
double result = values[i] + values[k];
// NOT LIKE THIS!
double result = Double.Parse(labels[i]) + Double.Parse(labels[k]);
Upvotes: 1
Reputation: 1502
You are trying to parse the ToString()
of a label. Were you instead looking to parse some property of the label?
When you call value[p]
, whats being returned is a on object of type Label
. If you wanted to parse the text of the label your code would instead be
tots += double.Parse(((Label)value[p]).Text);
Upvotes: 1
Reputation: 2256
value[p] is of type Label. If you want to get the text value of the label, you should use value[p].Text.
Double total = 0;
for (int p = 0; p < value.Count; p++)
{
tots += double.Parse(((Label)value[p]).Text);
}
Another thing, you should use List<Label>
for value instead of ArrayList
.
Upvotes: 1