Dear experts,
I am stumped by the following code :
- using System;
- ///
- /// Implements overloaded indexers.
- ///
- class OvrIndexer
- {
- private string[] myData;
- private int arrSize;
- public OvrIndexer(int size)
- {
- arrSize = size;
- myData = new string[size];
- for (int i=0; i < size; i++)
- {
- myData[i] = "empty";
- }
- }
- public string this[int pos]
- {
- get
- {
- return myData[pos];
- }
- set
- {
- myData[pos] = value;
- }
- }
- public string this[string data]
- {
- get
- {
- int count = 0;
- for (int i=0; i < arrSize; i++)
- {
- if (myData[i] == data)
- {
- count++;
- }
- }
- return count.ToString();
- }
- set
- {
- for (int i=0; i < arrSize; i++)
- {
- if (myData[i] == data)
- {
- myData[i] = value;
- }
- }
- }
- }
- static void Main(string[] args)
- {
- int size = 10;
- OvrIndexer myInd = new OvrIndexer(size);
- myInd[9] = "Some Value";
- myInd[3] = "Another Value";
- myInd[5] = "Any Value";
- myInd["empty"] = "no value";
- Console.WriteLine("\nIndexer Output\n");
- for (int i=0; i < size; i++)
- {
- Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
- }
- Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]);
- }
- }
and then size can become an integer again in the for-loop that comes next...
Just can't see the logic behind.
Hope someone can help me out on that .
Tks.
ali tuncerPosted Nov 8, 2016, 10:40 AM
Tangara GPosted Nov 8, 2016, 10:10 AM
{
private string[] myData;
private int arrSize;
public OvrIndexer(int size)
{
arrSize = size;
myData = new string[size];
for (int i=0; i < size; i++)
{
myData[i] = "empty";
}
}