Noel ST Noel ST - 2 months ago 29
C# Question

C# Class array? - Visual Studio

The following seems to be a class array?

Chemical.ChemicalName[IndexNumber]


It seems that there are several other fields associated with Chemical, such as Cost, Quantity, SupplierName (Chemical.Cost etc).

I was wondering what this type of variable is called? A class array? I've been searching online about arrays and can't seem to find any documentation on this.

And secondly, how do I declare such a variable?

Thanks.

Answer

Variable would look something like that

public class Chemical{
 public ChemicalName[] ChemicalNames = new ChemicalName[5];

  ...
}

So you can invoke it like that

Chemical c = new Chemical();
c.ChemicalNames[index];

OR, you can also declare the Array as static so you wont need an intance of the class to get the array e.g.

public class Chemical{
 public static ChemicalName[] ChemicalNames = new ChemicalName[5];

  ...
}

to call a static, simply use class.variable/method name

Chemical.ChemicalNames[index];