Click to See Complete Forum and Search --> : indexer of class


FundooGuy
May 11th, 2005, 06:43 AM
HI

can anyone explain me this statement
---------------------
int[] bits;
int length;

bits = new int[((length - 1) >> 5) + 1];
---------------------

another thing why n wht is the use of indexer (class indexer)
can anyone give me one example to where we can use this thing

darwen
May 11th, 2005, 06:48 AM
Please don't talk in mobile phone speak - it's very hard to read. Wh dnt I tlk n phn spk whn I rply ?

The code creates an array of integers of the given size.

An indexer is defined for a class like this :


public class MyArrayClass
{
private ArrayList m_aList = new ArrayList(5);

// -- this is the indexer --
public object this[int nIndex]
{
get
{
return m_aList[nIndex];
}

set
{
m_aList[nIndex] = value;
}
}
}


The indexer is defined as a property whos name is 'this' which takes a parameter in square brackets (it doesn't need to be an integer, it can be anything).

You can now use the above class like this :


MyArrayClass myArray = new MyArrayClass();
myArray[0] = "Hello";
myArray[1] = "There";

System.Diagnostics.Debug.WriteLine(myArray[0]);


A good example of an indexer is on the Hashtable class so you can do things like this :


Hashtable hashTable = new Hashtable();
hashTable.Add("Hello", "There");

string sResult = hashTable["Hello"] as string;


Alternatively you could have just looked this up in MSDN you know. Besides the fact this sounds suspiciously like a homework question.

Darwen.

cilu
May 11th, 2005, 07:13 AM
int[] bits;
int length;

bits = new int[((length - 1) >> 5) + 1];

This code creates and array of ints. The number of elements in the array is ((length - 1) >> 5) + 1, that means 1+(length-1)/32. If this is the exact sequence of instructions, than length is initialized to 0 and ((length - 1) >> 5) + 1 will be 85,899,346.

Anders
May 11th, 2005, 07:29 AM
This code creates and array of ints. The number of elements in the array is ((length - 1) >> 5) + 1, that means 1+(length-1)/32. If this is the exact sequence of instructions, than length is initialized to 0 and ((length - 1) >> 5) + 1 will be 85,899,346.
Hm... My compiler wont let me use length if it is unassigned. Anyway, when I run

int length = 0;
int n = ((length - 1) >> 5) + 1;
System.Diagnostics.Trace.WriteLine("n = " + n);

my output window shows 'n = 0'. The result is the same without using n as placeholder. So what is going on? I would have thought the same as cilu...

Anders
May 11th, 2005, 07:42 AM
Naah... you fooled me cilu :-)

I Should have thought before posting...

cilu
May 11th, 2005, 08:29 AM
Naah... you fooled me cilu :-)

I Should have thought before posting...
Did I say something wrong?

Anders
May 11th, 2005, 08:44 AM
Well, yes, I think so. That is why I posted the reply saying that ((length - 1) >> 5) + 1 will *not* be 85,899,346. It will be zero if length is zero. (And, like I said, my compiler wont even allow me to use length if it is unassignged.)

But then I was convinced that you were joking. That is why I posted the 2nd reply. Now, technically, what you said is still wrong, joking or not :-)

cilu
May 11th, 2005, 08:54 AM
According to my calculations,

0 - 1 = 0xFFFFFFFF / 32 = 0x051EB851, which is of course wrong...

because

0 - 1 = 0xFFFFFFFF >> 5 is = 0xFFFFFFFF (length is signed not unsigned)
and 0xFFFFFFFF + 1 make 0 again...

there you go!

Anders
May 11th, 2005, 09:00 AM
Yep, agree :-)

FundooGuy
May 12th, 2005, 12:35 AM
Hi All

Thanx you all for your reply.
please tell me wht is the meaning of >> in this statement. (may be its a silly question 4 u all experts)

darwen
May 12th, 2005, 01:07 AM
This is a right shift by 5 bits i.e. like Cilu said equivalent to dividing by 32.

Darwen.

cilu
May 12th, 2005, 07:33 AM
This is a right shift by 5 bits i.e. like Cilu said equivalent to dividing by 32.

For unsigned types, of for signed types when the sign bit is 0... Otherwise 1 will be shift into the empty space, and that's why I was misled the first time...