Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 i'll 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)

Posted (edited)

Indexers are only ways to pass arguments to your properties(and mantain that interface).

 

Having the code passing an array index won't 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.

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

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

Edited by BlackStone
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
Indexers are only ways to pass arguments to your properties(and mantain that interface).

 

Having the code passing an array index won't 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.

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

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

}

Posted

? and : are called ternary operators. They operate like an if...else....

An example:

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 ':'

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
? and : are called ternary operators. They operate like an if...else....

An example:

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 !!!!!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...