| Author |
Message |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
dear all,
I have a class called precipitate
class Precipitate { private string name; public string Name { get { return name; } set { name = value; } } }
So far so good. I create an array of the precipitate class
Precipitate[] prec = new Precipitate[20]; for (int i = 0; i < 20; i++) prec = new Precipitate();
when I want to access the property name for a particular array element we do it as prec .name. Again no problem here.
Now I want to have something like...
what I want is something like... prec .equation[j].equationname; prec .equation[j].valuex; etc...
how should I do it I just know the basics and dont know if...you can create a property array "equation[]" inside the prec class and make it have properties
I really dont know how to go about it. Any help is appreciated.
cheers prasad..
Visual C#1
|
| |
|
| |
 |
Lepaca

|
Posted: Visual C# Language, arrays and properties. |
Top |
You can use indexer property...
private eqType equation = new eqType[20]; public eqType this [int Index] { { get {return equation[index];} set {equation[Index] = value;} } }
or public array
public eqType equation = new eqType[20];
|
| |
|
| |
 |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
ok but how can the equation property array now have its own properties
like prec .equation[j].itsname
prasad..
|
| |
|
| |
 |
Lepaca

|
Posted: Visual C# Language, arrays and properties. |
Top |
you have to define your class and assign it to array...
class eqType { public string equationname; public string value; }
public eqType equation = new eqType[20];
|
| |
|
| |
 |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
this way I will just have another class array called equation[]. and a seperate class called precipitate[].
But as I told in my 1st post, I need to have this equation array as a property in the precipitate class...
to put it in words each precipitate has many equations and each equation has many properties.
hope you understand..
your answer gives me just equation and precipitate (seperate array of classes) I want to have precipitate .equation[j].name etc...
cheers, prasad..
|
| |
|
| |
 |
boban.s

|
Posted: Visual C# Language, arrays and properties. |
Top |
In that case, you will have a property of generic list or collecion type. Also that list will contain istance of a class that have property equationname and value. class Equation { string _EquationName; public string EquationName { get { return _EquationName; } set { _EquationName = value; } }
int _Value; public int Value { get { return _Value; } set { _Value = value; } } }
So the property will be like this: class Participate { ...... List<Equation> _Equations; public List<Equation> Equations { get { return _Equations; } set { _Equations = value; ) }
.... }
So now you can access your generic list of Equation types like this: prec[ i ].Equations[j].EquationName prec[ i ].Equations[j].Value
But having collection types properties is not good practice. For that you should use methods. This is just a suggestion that you can think of when you done with this problem.
|
| |
|
| |
 |
Lepaca

|
Posted: Visual C# Language, arrays and properties. |
Top |
sorry, I wasn't enough clear...
class precipitate { public eqaution[] eq = new equation[20] }
class equation { public string equationname; public string value; }
void main { precipitate[] p = new precipitate[10];
Debug.WriteLine(p[2].eq[1].equationname); }
|
| |
|
| |
 |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
thanks a ton that worked nicely. BTW, can you please tell me why it is bad to use them and how at all methods can be used to do the same thing
Thank you again for the nice help. cheers, prasad..
|
| |
|
| |
 |
boban.s

|
Posted: Visual C# Language, arrays and properties. |
Top |
|
| |
 |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
how to initialise the list If I do it the following way,
Equation[] myequation = new Equation ;
for(i=0;i<8;i++) myequation = new Equation[];
it gives me null reference error.
should we do.. for(j=0;j<something<j++) for(i=0;i<8;i++)
precipitate[j].myequation = new Equation[];
cheers, prasad..
|
| |
|
| |
 |
James Curran

|
Posted: Visual C# Language, arrays and properties. |
Top |
class Precipitate { private equation[] ex; // Initialize in ctor public equation[] equation { get { return ex; } // just a getter. A setting is unecessary and will cause trouble. } }
So, now...
Precipitate p = new Precipitate(); p.equation is an array of equations and
p.equation[ i ] is one element of that array.
p.equation[ i ].equationname is a property of that one element of that array.
|
| |
|
| |
 |
pessi

|
Posted: Visual C# Language, arrays and properties. |
Top |
I am getting a null reference exception, please help.
code Precipitate[] prec = new Precipitate[30]; for (int i = 0; i < 30; i++) prec = new Precipitate();
FerEqn[] FerriteEquation = new FerEqn ; for (int i = 0; i < totalPrecsConsidered; i++) { for (int j = 0; j < 8; j++) { prec .FerriteEquation[j] = new FerEqn(); } }
class Precipitate { List<FerEqn> ferriteEquation;
internal List<FerEqn> FerriteEquation { get { return ferriteEquation; } set { ferriteEquation = value; } }
class FerEqn { string authName;
public int AuthName { get { return authName; } set { authName = value; } } }
when I try p[0].FerriteEquation[0].AuthName = "Something";
it gives me a null reference exception
Many thanks in advance, prasad..
|
| |
|
| |
 |
James Curran

|
Posted: Visual C# Language, arrays and properties. |
Top |
You missed an important line in you message : // Initialize in ctor
So, let's see if you can fix this: | | class FerEqn { string authName;
public int AuthName { get { return authName; } set { authName = value; } } } | This is fine.
but the the array (Well, now list) of FerEqn is defined inside Precipitate, and should be initialized inside Precipitate
| | class Precipitate { List<FerEqn> ferriteEquation; public List<FerEqn> FerriteEquation { get { return ferriteEquation; } set { ferriteEquation = value; } } public Precipitate() { const int numEqu = 8; ferriteEquation = new List<FerEqn>(numEqu); // SInce you apparently want to immedately use indexer access to the list, // we'll have to put some items in the list. for (int i = 0; i < numEqu; ++i) ferriteEquation.Add(new FerEqu()); } } |
|
| |
|
| |
 |
| |