indexers...im in a trouble.

FlyBoy

Well-known member
Joined
Sep 6, 2004
Messages
106
i want to create an int indexer...this is the code:

public class Ctest
{
public Ctest(int len)
{
i=len;
}
public int this[int i2]
{
get {return i;}
set {i=value;}
}

private int i;
}

public void main()
{
Ctest tt= new Ctest(5);
}

the problem is..that its aint really creatin an array....when i assign a value to a specific index in the indexer...it applies it to all the indexer(array) for e.g:
tt[0]=10;
if ill do this:
Console.Writeline(tt[1].tostring());

it would produce the same value that is in tt[0].
any idea???

(im pretty new with indexes)
 
Indexers are only ways to pass arguments to your properties(and mantain that interface).

Having the code passing an array index wont create an array for you.

From your code it looks like you assume that the indexers will set up an array for you. That is not the case.

Here is my fix-up of the code.
Code:
public class CTest
{
    // This has to be an array:
    private int[] items;

    public CTest(int length)
    {
        items = new int[length];
    }

    // The indexer merely returns the array item at the index passed to it.
    public int this[int index]
    {
        get
        {
            return items[index];
        }
        set
        {
            items[index] = value;
        }
    }
}


A Indexer here is a cleaner way of setting a variable in an array.

Without them you would have done something like this in the class above:

Without Indexers
Code:
int GetInteger(int index)
{
   return items[index];
}
void SetInteger(int index, int value)
{
   items[index] = value;
}
 
Last edited by a moderator:
BlackStone said:
Indexers are only ways to pass arguments to your properties(and mantain that interface).

Having the code passing an array index wont create an array for you.

From your code it looks like you assume that the indexers will set up an array for you. That is not the case.

Here is my fix-up of the code.
Code:
public class CTest
{
    // This has to be an array:
    private int[] items;

    public CTest(int length)
    {
        items = new int[length];
    }

    // The indexer merely returns the array item at the index passed to it.
    public int this[int index]
    {
        get
        {
            return items[index];
        }
        set
        {
            items[index] = value;
        }
    }
}


A Indexer here is a cleaner way of setting a variable in an array.

Without them you would have done something like this in the class above:

Without Indexers
Code:
int GetInteger(int index)
{
   return items[index];
}
void SetInteger(int index, int value)
{
   items[index] = value;
}


now i see...thank u very much!!!
and i have another little question,i cant figure out what the "?" is doing there...couldnt really understand the syntax:

get
{
int i = Array.IndexOf(phoneNumbers, number);
return (i != -1) ? names : new Name();
}
 
? and : are called ternary operators. They operate like an if...else....
An example:
Code:
string GetName(string name, bool male)
{
    // If male is true, it will add the Mr. title,
    // else it will add Ms.
    return (male ? "Mr. " : "Ms. ") + name;
}
If the argument of the equation is true, it will return the result after the ? otherwise it will return the result after the :
 
BlackStone said:
? and : are called ternary operators. They operate like an if...else....
An example:
Code:
string GetName(string name, bool male)
{
    // If male is true, it will add the Mr. title,
    // else it will add Ms.
    return (male ? "Mr. " : "Ms. ") + name;
}
If the argument of the equation is true, it will return the result after the ? otherwise it will return the result after the :

oh. now i understand it...10x alot !!!!!!
 
Back
Top