Reputation: 45
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?
Upvotes: 0
Views: 378
Reputation: 100630
Let's parse Chemical.ChemicalName[IndexNumber]
:
IndexNumber
is probably some value of one of integer types - let guess int IndexNumber
. Other options could be enum or any type as you can use indexer with any arguments.
[IndexNumber]
is indexing something. Since there is no
Static Indexers? in C# it means ChemicalName
can't be class name of static class like following
namespace Chemical {
static class ChemicalName{}
}
so it means that ChemicalName
is either property or field of Chemical
.
Now for Chemical
there are more options
it could be static class with ChemicalName
as static property:
static class Chemical{
public static string[] ChemicalName = new[] {"Food", "Poison"};
}
it could be local variable of some type that has ChemicalName
as instance property:
class ChemicalType{
public string[] ChemicalName = new[] {"Food", "Poison"};
}
...
void MyMethod()
{
// implicitly typed, same as `ChemicalType Chemical`
var Chemical = new ChemicalType();
int IndexNumber = 1;
Console.WriteLine(Chemical.ChemicalName[IndexNumber]);
}
it could be field or property of your class (with any accessibility as to get Checmical.ChemicalName
syntax to work for property it need to be used inside a method of your class)
class MyClass
{
// one of any combination:
// private field
ChemicalType Chemical = new ChemicalType();
// or protected automatic property
protected ChemicalType Chemical {get;set;}
// or public property
ChemicalType _chemical;
public ChemicalType Chemical {get {return _chemical;}}
...
}
Finally let's see what ChemicalName
could be: the only requirement is to allow indexer by some type. including int. This gives very broad set of types as many of built in types support indexing.
string[] ChemicalName
string ChemicalName
. When indexing will give single char resultList<string>
Dictionary<string,string> ChemicalName
.public string this[int i]
(or any other return type).Upvotes: 1
Reputation: 29036
Let me consider this statement from the question Chemical.ChemicalName[IndexNumber]
, We can consider Chemical
as a class or as an object of some other class. If it is a class means the ChemicalName
will be a static.
Then comes the ChemicalName
definitely it will be a collection(List/Array or something like that) or even an object of a class which having an indexer
.
Case 1: consider Chemical
is class and ChemicalName
is a List of string So the Definition will be :
public class Chemical
{
public static List<string> ChemicalNames = new List<string>(){"name1","name 2"};
}
So that you can access a single name like the following:
string someChemicalName=Chemical.ChemicalNames[0]; // will be name1
Case 2: consider Chemical
is an object of a class and ChemicalName
is a List of string So the Definition will be :
public class Chemicals
{
public List<string> ChemicalNames;
}
Then you can access create the Chemical
by using the following code:
Chemicals Chemical= new Chemicals();
Chemical.ChemicalNames=new List<string>(){"name1","name 2"};
Here also you can workout your statement like this
string someChemicalName=Chemical.ChemicalNames[0]; // will be name1
Upvotes: 2
Reputation: 222720
Assuming it's a property, not an array , so you cannot access using an index,
public class Chemical
{
// Field
public string ChemicalName;
...etc
}
if chemical is an array , then you can declare like this,
Chemical[] Chemicals = new Chemical[200];
Then you can access the particular element using the index,
Chemicals[IndexNumber].ChemicalName
EDIT
If you want to have ChemicalName as a array inside the class,
public class Chemical{
public ChemicalName[] ChemicalNames = new ChemicalName[5];
]
you can access like this,
Chemical[] Chemicals = new Chemical[200];
c[index].ChemicalNames[index];
Upvotes: 3
Reputation: 479
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];
Upvotes: 2
Reputation: 6418
It is a class property that implements an indexer. Usually this is an array, but it can be something else as long as it implements this[int index]
.
You can declare one by declaring it as a class property. For example,
class Book
{
public Book(int numPages)
{
Pages = new Page[numPages];
}
public Page[] Pages {get;}
}
You can then instantiate an instance and access a page.
Book myBook = new Book(100);
myBook.Pages[50]=new Page("Hi, welcome to Page 50");
Console.Write(myBook.Pages[50].GetText());
Upvotes: 2