Problem by using an Object and a variable inside another function [modified]
-
Hello, trying to build a Windows programm, I 've declared an object in Fornm1.cs by typing
Person[] P = new Person[10];
after a class Person I 've created on another file called Person.cs Then on the function that is being activated after clicking on a button inside the Form I pass simple numerical values to a member attribute like below:
private void btnObjectAdd\_Click(object sender, EventArgs e) { lbltest\_i.Text = i.ToString(); P\[i\] = new Person(); P\[i\].category = i; i++; if (i == 10) { for (i = 0; i < 10; i++) P\[i\] = null; i = 0; } }
Thus I get P[0]=0, P[1]=1, ... P[9]=9. Then I try to call a value of an object on another function like
private void showToolStripMenuItem\_Click(object sender, EventArgs e) { int value = P\[2\].category; }
and I get value = 2. This works perfect. But if I change
int value = P[2].category;
into
int value = P[i].category;
then I get an error that I should create an object with 'new'. Variable i is declared as
int i = 0;
outside the functions but inside the public partial class Form1 : Form Is that the problem?
modified on Monday, February 7, 2011 1:14 PM
-
Hello, trying to build a Windows programm, I 've declared an object in Fornm1.cs by typing
Person[] P = new Person[10];
after a class Person I 've created on another file called Person.cs Then on the function that is being activated after clicking on a button inside the Form I pass simple numerical values to a member attribute like below:
private void btnObjectAdd\_Click(object sender, EventArgs e) { lbltest\_i.Text = i.ToString(); P\[i\] = new Person(); P\[i\].category = i; i++; if (i == 10) { for (i = 0; i < 10; i++) P\[i\] = null; i = 0; } }
Thus I get P[0]=0, P[1]=1, ... P[9]=9. Then I try to call a value of an object on another function like
private void showToolStripMenuItem\_Click(object sender, EventArgs e) { int value = P\[2\].category; }
and I get value = 2. This works perfect. But if I change
int value = P[2].category;
into
int value = P[i].category;
then I get an error that I should create an object with 'new'. Variable i is declared as
int i = 0;
outside the functions but inside the public partial class Form1 : Form Is that the problem?
modified on Monday, February 7, 2011 1:14 PM
There seems to be something very wrong with your design. But if "i" is declared in the same class as your showToolStripMenuItem_Click code then there should be no reason why that is the problem. Perhaps you need to post more of your code - and please you the PRE tags to format is more readably.
return 5;
-
There seems to be something very wrong with your design. But if "i" is declared in the same class as your showToolStripMenuItem_Click code then there should be no reason why that is the problem. Perhaps you need to post more of your code - and please you the PRE tags to format is more readably.
return 5;
Thanks for the 'pre' tip, i was looking for s'thing like that. I got rid of the confusing 0 by changing the array into
new Person[11]
instead of a 10 dimension. After all, we 've got plenty of memory nowdays :-\ As a result everything worked fine! It must have been some other mistake, as the one I thought of.