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?

3
  • 2
    It's just a property of a type that implements an indexer. Commented Sep 28, 2016 at 2:33
  • 1
    its just a class that has an indexer on it. Commented Sep 28, 2016 at 2: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... Commented Sep 28, 2016 at 2:39

5 Answers 5

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];
Sign up to request clarification or add additional context in comments.

7 Comments

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.
@un-lucky OP's question was not clear, anyway modified the answer
why do people upvote and downvote? mention the reason
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 Op has not mentioned anything. its based on the assumption. anyway it has answers for both
|
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];

Comments

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());

Comments

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

1 Comment

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.
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.