Indexers

Hi, i was wondering if its possibile to check over this piece of code n ensure my comments are correct as im not sure i have understood this.

using System;


class IntIndexer
{
private string[] myData; // private array of type string

public IntIndexer(int size) // public method used to run through the array
{
myData = new string[size]; // assignment of string to myData

for (int i=0; i < size; i++) // simple for loop used to go through each element of the array.
{
myData[i] = "empty"; // is this just THE standard declaration unless stated by the indexer below?
}
}

public string this[int pos] // indexer and "pos" refers to the index i.e. myInd[9] => pos =9?
{
get
{
return myData[pos]; // used to obtain the myInd value
}
set
{
myData[pos] = value; // used to write the myInd value
}
}

static void Main(string[] args)
{
int size = 10;

IntIndexer myInd = new IntIndexer(size);

myInd[9] = "Some Value";
myInd[3] = "Another Value";
myInd[5] = "Any Value";

Console.WriteLine("
Indexer Output
");

for (int i=0; i < size; i++)
{
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
}
}
}


so the program executes, Constructor is called n declares a new array called myInd of 10 strings.

program runs through the for loop declared inside the intIndexer method.

each time this is accessed "myData[i] = "empty";" it calls up the indexer to check the myInt[] variables declared in the Main() method. if one if present for the coresponding index number then the set code is run otherwise the default "empty" statement is assigned to the element.

the one thing i cant answer is how it knows myInd = myData in the code???

Thanks again

Rgds

Fraz


Comments

  • // public method used to run through the array
    Actually this is what is called a "constructor" - this IS a method of sorts - but it is only ever used to instantiate the class it belongs to. So in code outside of this class you can create a new IntIndexer for use in your code. The "Constructor" is what C# will call in order to "set up" the new object.

    What this constructor is doing is initializing the myData "instance variable" and loading each of its members with a default "empty" value.

    - I am throwing a lot of keywords at you so you can more effectively research these ideas on the internet.

    // simple for loop used to go through each element of the array.
    Yes, this is just a "for loop".

    // is this just THE standard declaration unless stated by the indexer below?
    The writer of this code wanted every new IntIndexer object that is instantiated to have the default value of "empty" in each of its items. This entire "Constructor" method is just trying to set up these default values.

    // indexer and "pos" refers to the index i.e. myInd[9] => pos =9?
    This is a little advanced for someone just learning about classes - but yes, this is an indexer property - think of it as a special kind of method that contains two methods - one for retrieving data and one for writing data.

    // used to obtain the myInd value
    // used to write the myInd value
    Yes and Yes - as long as you understand the myInd is an instantiation of this class.

    [b]the one thing i cant answer is how it knows myInd = myData in the code???[/b]
    Ok - so this is going to be the more difficult part to understand.
    That special method from above has both a get and a set functionality in it.

    When you make a statement like
    [code]
    myInd[whatever] = "some value";
    [/code]
    the "SET" function is called and is passed a special variable called "value".

    In the "SET" code (these are refered to as getter and setter) You are actually manipulating the instance variable that was declared earlier in the class "myData[pos] = value;".
    So there is where you are actually setting one of the values in the myData array.

    The "GET" code is called with a statement like so
    [code]
    string myVariable = myInd[12];
    [/code]
    and the "return statement" is the code that sends the information back to be stored in "myVariable".



    The part that may be the most confusing is what "instantiation" is. The purpose of writing a class is to have a "blueprint" of an "object". Once you have the blueprint made you can have as many of that object as you want without having to rewrite that code. However, each object will have it's own copy of the blueprint - so if you change the value of the variables in that object - the variables in all of the other copies will not be affected.

    So I could potentially have 10 different "IntIndexers" in my code:
    IntIndexer myInd1 = new IntIndexer(12);
    IntIndexer myInd2 = new IntIndexer(5);
    IntIndexer myInd3 = new IntIndexer(43);

    and so on...

    and if I did something like:
    myInd1[0] = "Value 1";
    myInd2[0] = "value 2";
    myInd3[0] = "value 3";

    Each of the different "myInd" variable would now have differing values in them. So you literally have 3 different objects that all came from the same class. Each of them has their own location in memory.
  • Actually I just noticed who posted this - didn't we already go over what constructors are?
  • FFS! yes we did, and im currently kicking myself for not noticing the method had the same name as the class n is thus a constructor. Dont think ill be posing any more questions just before i go to bed again.

    sorry




    i read through the rest of your post n it makes sence bar the getter and setter

    i just want to clarify that the setters are ONLY being called during the following lines.


    myInd[9] = "Some Value";

    myInd[3] = "Another Value";

    myInd[5] = "Any Value";


    and the getters are ONLY being called here?


    for (int i=0; i < size; i++)

    {

    Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);

    }

    Im assuming the constructor initially sets all the elements to "Empty"?

    Thanks again

    Rgds

    Fraz
  • Correct - a setter is only called when information is being written TO the property or indexer and a getter is only being called when a value is being requested.
  • cheers again DataDink

    Hope u have a nice wknd
  • Hi, been looking through the following code and i just wanted to clarify a couple lines.

    Lines 50 to 52, they are ONLY being called by line 67?

    am i correct when i say that line 67s "empty" is the "data" thats being used in the setter on lines 50 to 52 and the "no value", also on line 67, is the "value" on line52?

    Thanks again for your help


    1/using System;
    2/
    3/class OvrIndexer
    4/{
    5/ private string[] myData;
    6/ private int arrSize;
    7/
    8/ public OvrIndexer(int size)
    9/ {
    10/ arrSize = size;
    11/ myData = new string[size];
    12/
    13/ for (int i=0; i < size; i++)
    14/ {
    15/ myData[i] = "empty";
    16/ }
    17/ }
    18/
    19/ public string this[int pos]
    20/ {
    21/ get
    22/ {
    23/ return myData[pos];
    24/ }
    25/ set
    26/ {
    27/ myData[pos] = value;
    28/ }
    29/ }
    30/
    31/ public string this[string data]
    32/ {
    33/ get
    34/ {
    35/ int count = 0;
    36/
    37/ for (int i=0; i < arrSize; i++)
    38/ {
    39/ if (myData[i] == data)
    40/ {
    41/ count++;
    42/ }
    43/ }
    44/ return count.ToString();
    45/ }
    46/ set
    47/ {
    48/ for (int i=0; i < arrSize; i++)
    49/ {
    50/ if (myData[i] == data)
    51/ {
    52/ myData[i] = value;
    53/ }
    54/ }
    55/ }
    56/ }
    57/
    58/ static void Main(string[] args)
    59/ {
    60/ int size = 10;
    61/ OvrIndexer myInd = new OvrIndexer(size);
    62/
    63/ myInd[9] = "Some Value";
    64/ myInd[3] = "Another Value";
    65/ myInd[5] = "Any Value";
    66/
    67/ myInd["empty"] = "no value";
    68/
    69/ Console.WriteLine("
    Indexer Output
    ");
    70/
    71/ for (int i=0; i < size; i++)
    72/ {
    73/ Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
    74/ }
    75/
    76/ Console.WriteLine("
    Number of "no value" entries: {0}", 77/myInd["no value"]);
    78/ }
    79/}
  • That sounds about right -

    That statement there will effectively replace any item the previously had a VALUE of "empty" and set it to "no value".
    So that means the values at index 9, 3, and 5 will not be altered.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion