Question about indexers

druknhik

Newcomer
Joined
Aug 2, 2004
Messages
12
I would like to do something like this:

Class.Property = something;

[] is the indexer for some List<> in Class.

This won't work, because this doesn't seem to return a reference to the object whose property I would like to modify.

In C++, would have been able to do something like this easily by overloading the [] operator, and making it return a reference.

How could I do the same in C#?
 
PlausiblyDamp said:
Could you post your code for how you have defined the indexer and the class it returns?

public Class this[int index]
{
get
{
return Elements[index];
}
set
{
Elements[index] = value;
}
}

Elements is a List<Class>

Did you ask me to show how Class is defined? If so, there has to something you would like to see...I won't put the whole thing, it's rather large.
 
public Class this[int index] should return an instance of Class - you should have access to all the properties and methods of Class. What properties / methods is it displaying instead?
 
PlausiblyDamp said:
public Class this[int index] should return an instance of Class - you should have access to all the properties and methods of Class. What properties / methods is it displaying instead?

In the watch, it displays everything like it should.
 
Back
Top