Need help/advice on dynamic arrays

I'm wondering if someone would be so nice to tell me what kinds of dynamic arrays there are in c#. As I understand Array can be somewhat dynamic, but it throws an exception if I want to add an element to it. So what should I use for the following ?

class LittleClass
{
private String s;
private int i;

// Some methods etc.
}

Now, I want to be able to make arrays of this kind, that can be made bigger if they need to be. Also, if there is some way to sort them etc would be nice. If I have understood things right with Array this can be done quite simply. But how ? I don't get the example in the doc.. An example for sorting on the int i in my example would be nice.
Third, if I'd make an Array LittleClass, then how could I gain access to s and i ? I can't seem to be doing it with SetValue, GetValue, so how can this be done ?

TIA
BlackSun
[929 byte] By [BlackSun] at [2007-11-18 13:36:39]
# 1 Re: Need help/advice on dynamic arrays
use ArrayList !!
pareshgh at 2007-11-9 1:34:57 >
# 2 Re: Need help/advice on dynamic arrays
Ok, but how ?

I can't see any way to create a list of LittleClass in that. Or can I have one list to contain for example both an String and an int ?

And if I hade an arraylist of LittleClass, then how do I access its members ?
BlackSun at 2007-11-9 1:35:57 >
# 3 Re: Need help/advice on dynamic arrays
ArrayList holds an array of Objects, and almost everything derives from Object - including your class. When you want to retreive an item from the arraylist, just typecast it to the correct class (you can get alot fancier if you want to mix multiple object types in the arraylist, too).

ArrayList MyArray;
LittleClass MyLittleClass;

MyArray = new ArrayList();
MyLittleClass = new MyLittleClass();

//Add the item to the array list.
MyArray.Add(MyLittleClass);

//Remove the item - this comes as an Object
Object MyObject = MyArray[0];

//Typecast it to LittleClass to reference the data inside
((LittleClass)MyObject).LittleClassFunction();
Linenoise at 2007-11-9 1:36:55 >
# 4 Re: Need help/advice on dynamic arrays
Ok, that sounds quite easy. Only one thing I ddn't get. The last thing, how does that help me accessing for example the 5th elements String s ?
BlackSun at 2007-11-9 1:37:53 >
# 5 Re: Need help/advice on dynamic arrays
Originally posted by BlackSun
Ok, that sounds quite easy. Only one thing I ddn't get. The last thing, how does that help me accessing for example the 5th elements String s ?

It's an array.. if you want the 5th element, you access MyArray[4] (the 1st element is index 0). MyArray.Count will tell you how many elements are in the array, as well.
Linenoise at 2007-11-9 1:39:02 >
# 6 Re: Need help/advice on dynamic arrays
Ahhhh !!
It was that easy, can't believe I didn't come up with that..

Thanks once again, probably more newbie questions coming up soon :p
BlackSun at 2007-11-9 1:39:56 >
# 7 Re: Need help/advice on dynamic arrays
But it doesn't work ...

For ex.

class MyClass
{
public void Method1()
{
// Bla bla bla
}

and in Main ...

ArrayList myList = new ArrayList();
MyClass mc = new MyClass();
myList.Add(mc);

// Now, how to do this ?
myList[0].Method1(); // Doesn't work ....
// so do I have to do this ?
myClass mc2 = myList[0];
// do what I want to do on mc2, and then change myList[0] = mc2

Is the 1st way even possible, because Method1 isn't a member of arraylist, but there should be some easier way to access classmembers ?
BlackSun at 2007-11-9 1:40:57 >
# 8 Re: Need help/advice on dynamic arrays
Originally posted by BlackSun
// Now, how to do this ?
myList[0].Method1(); // Doesn't work ....

Try something like:
((MyClass)myList[0]).Method1();

As Linenoise wrote:
When you want to retreive an item from the arraylist, just typecast it to the correct class...
AJMartin at 2007-11-9 1:42:03 >
# 9 Re: Need help/advice on dynamic arrays
Also, this only works if your array is all the same type (or at least, derived from that type). If you really wanted an array that has different, unrelated types, you can use the "is" keyword.

class MyFirstClass
{
...
}

class MySecondClass
{
...
}

.....

Object MyObj = MyArray[0];

if (MyObj is MyFirstClass)
{
((MyFirstClass)MyObj).DoSomething();
}
else if (MyObj is MyClass2)
{
((MySecondClass)MyObj).KillEveryone();
}
Linenoise at 2007-11-9 1:42:58 >
# 10 Re: Need help/advice on dynamic arrays
Ok, thanks NOW I think I get it. So on to the next question then...

How do I use the Sort() function in ArrayList using my class ??

If my ArrayList myList = new ArrayList;
an then I put in some elements of myClass looking like this
class MyClass
{
public int a;
public string s;
}

and I want the sorting to be done according to the a ?
How is this done ?
BlackSun at 2007-11-9 1:43:59 >
# 11 Re: Need help/advice on dynamic arrays
Originally posted by BlackSun How do I use the Sort() function in ArrayList using my class ??

Implement the IComparable interface in each of your classes. There's an example in the .NET Framework SDK documentation viewer at:
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemicomparableclasstopic.htm

I hope that helps!
AJMartin at 2007-11-9 1:45:05 >
# 12 Re: Need help/advice on dynamic arrays
well, I read that, and tried, and of course failed. Thats why I posted here, hoping to get an better example, or a little help ..:D
BlackSun at 2007-11-9 1:46:01 >
# 13 Re: Need help/advice on dynamic arrays
Looking at the docs, it's only got one function to implement - CompareTo. You compare the current object (this) to the object passed in (which appears will only be called if the object is of the same type as this.. so you typecast it as normal).

If this is < passed-in, return -1
if they're equal return 0
if this > passed-in, return 1

You have to decide what < and > mean. That's why you're required to implement the interface - the IComparable interface allows sort() to know which object is bigger while sorting.

[CODE]

class MyClass : IComparable
{
...
int CompareTo (object OtherObj)
{
//return -1, 0, or 1 here
}
}
Linenoise at 2007-11-9 1:47:08 >
# 14 Re: Need help/advice on dynamic arrays
Originally posted by BlackSun
well, I read that, and tried, and of course failed. Thats why I posted here, hoping to get an better example, or a little help ..:D

Perhaps you could post the code that you tried and failed with? That way we can point out, "hey! This bit is wrong!" :)
AJMartin at 2007-11-9 1:48:05 >
# 15 Re: Need help/advice on dynamic arrays
Well, I got mad and deleted it very much ......

But I think I got it with the previous answer..

Thanks ! so far....
BlackSun at 2007-11-9 1:49:09 >