0

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?

Julian
  • 33,915
  • 22
  • 119
  • 174
Noel ST
  • 45
  • 7
  • 2
    It's just a property of a type that implements an indexer. – Mathias R. Jessen Sep 28 '16 at 02:33
  • 1
    its just a class that has an [indexer](https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx) on it. – Jonesopolis Sep 28 '16 at 02:34
  • Instead of essentially asking "what type of code can syntactically correct for `Chemical.ChemicalName[IndexNumber]` expression" you could have shown actual definition instead... Now people will list you all options - field vs. property, array/list/dictionary/string/custom indexer... – Alexei Levenkov Sep 28 '16 at 02:39

5 Answers5

3

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];
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • in question it says, `Chemical.ChemicalName[IndexNumber]` but your answer is `Chemicals[IndexNumber].ChemicalName` both are different. you are using array of object of the class op may look for an array of properties of the class. – sujith karivelil Sep 28 '16 at 02:38
  • @un-lucky OP's question was not clear, anyway modified the answer – Sajeetharan Sep 28 '16 at 02:41
  • why do people upvote and downvote? mention the reason – Sajeetharan Sep 28 '16 at 02:46
  • I didn't downvote but your opening statement `It is a property, not an array` doesn't make sense - it can be both a property *and* an array, they're not mutually exclusive. Your example with `ChemicalName` *would infact* work, because the indexer will be selecting the nth character in the string, as well. – Rob Sep 28 '16 at 02:48
  • @Rob Op has not mentioned anything. its based on the assumption. anyway it has answers for both – Sajeetharan Sep 28 '16 at 02:49
  • @Sajeetharan : there is no downvote, Somebody removed the upvote after cast. now its retained. – sujith karivelil Sep 28 '16 at 02:53
  • @Sajeetharan You missed my point with your edit `Assuming it's a property, not an array` :) - Just because something is a property, doesn't mean its type cannot be an array. And your first example still incorrectly says you cannot use an indexer, because you *can* use an indexer on a string. – Rob Sep 28 '16 at 02:54
2

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];
deviantxdes
  • 469
  • 5
  • 17
2

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());
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
2

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
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • You've missed the case when `Chemical` is variable name and the explanation that `Chemical.ChemicalName` can't be static class name, but those are non practical cases. – Alexei Levenkov Sep 28 '16 at 03:09
1

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.

  • array is most common one string[] ChemicalName
  • just string - somewhat strange given name of variable, but possible - string ChemicalName. When indexing will give single char result
  • List<string>
  • dictionary, this option allows broader range of indexing - i.e. by strings Dictionary<string,string> ChemicalName.
  • custom type implementing similar to public string this[int i] (or any other return type).
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179