Thread overview
Classes with indexes
Jan 02, 2008
peter
Jan 03, 2008
Daniel919
Jan 03, 2008
peter
January 02, 2008
Hello guys,

I have the following class...

class myclass
{
    private ubyte[] Array;
}

...and i want to create an instance of this class and access the array as follows:

myclass mc = new myclass();
mc[0] = 100;

I was complety sure i saw this in the newsgroup but i can´t remember where exactly.
How is this "thing" called?
In the .NET framework i see it in almost every library.

I hope you guys get the idea, please help.
January 03, 2008
> I have the following class...
> 
> class myclass
> {
>     private ubyte[] Array;
> }
> 
> ...and i want to create an instance of this class and access the array as follows:
> 
> myclass mc = new myclass();
> mc[0] = 100;

import std.stdio;

class myclass
{
	private ubyte[] Array;
	
	void opIndexAssign(ubyte value, size_t i) {
		if( Array.length <= i )
			Array.length = i+1;
		Array[i] = value;
	}
	
	ubyte opIndex(size_t i) { return Array[i]; }
}

void main()
{
	myclass mc = new myclass();
	mc[0] = 100;
	writefln( mc[0] );
}



http://www.digitalmars.com/d/operatoroverloading.html

Best regards,
Daniel
January 03, 2008
Daniel919 Wrote:

> > I have the following class...
> > 
> > class myclass
> > {
> >     private ubyte[] Array;
> > }
> > 
> > ...and i want to create an instance of this class and access the array as follows:
> > 
> > myclass mc = new myclass();
> > mc[0] = 100;
> 
> import std.stdio;
> 
> class myclass
> {
> 	private ubyte[] Array;
> 
> 	void opIndexAssign(ubyte value, size_t i) {
> 		if( Array.length <= i )
> 			Array.length = i+1;
> 		Array[i] = value;
> 	}
> 
> 	ubyte opIndex(size_t i) { return Array[i]; }
> }
> 
> void main()
> {
> 	myclass mc = new myclass();
> 	mc[0] = 100;
> 	writefln( mc[0] );
> }
> 
> 
> 
> http://www.digitalmars.com/d/operatoroverloading.html
> 
> Best regards,
> Daniel

Hahaha, cool man thanks, that´s exactly what i needed.

"Array Operator Overloading: Overloading Indexing" That´s the name for it. I´ve learned something new today, thanks again.