Overriding an indexer
-
I'm trying to override an indexer in a Dictionary but I can't figure out what the syntax is (or even if this is possible). I tried overloading the operator but it simply complains that you cannot have both a default property indexer and an operator. There seems to be a lot of examples for C#, can anyone help me out? Regards, JP
Did I post well? Rate it! Did I post badly? Rate that too!
-
I'm trying to override an indexer in a Dictionary but I can't figure out what the syntax is (or even if this is possible). I tried overloading the operator but it simply complains that you cannot have both a default property indexer and an operator. There seems to be a lot of examples for C#, can anyone help me out? Regards, JP
Did I post well? Rate it! Did I post badly? Rate that too!
Esmo2000 wrote:
I'm trying to override an indexer in a Dictionary
Dictionary
classes indexer is not virtual. So you can't override it. If you are looking for writing virtual indexer and overriding in a derived class, here you go.ref class BaseClass
{
public:
property int Index[int]
{
virtual int get(int index)
{
Console::WriteLine("Base class get()");
return 0;
}
}
};ref class DerivedClass : public BaseClass
{
public:
property int Index[int]
{
virtual int get(int index) override
{
Console::WriteLine("Derived class get()");
return 0;
}
}
};int main()
{
BaseClass^ b = gcnew DerivedClass();
int a = b->Index[10];
return 0;
}Like in standard C++, implicit overriding is not supported in C++/CLI. You have to explicitly specify it with
override
keyword. :)Navaneeth How to use google | Ask smart questions