Next problem, this time arrayoriented

H

H

Ok, now this newbie is dealing with arrays.
I have some questions on this. I thought I'd use the built in Array class,
since it contains all I need for sorting, copying etc. arrays. Now I'm
wondering if this is so clever. As I've understood it, one good thing with
the Array class is that it is partly dynamical, since there is a function
for adding elements to the array, although it thtows an exception...
But what I'm having the real problems with is to access the values in the
created array ...
For Example

class class1
{
private String string1;
public String string2;

public String GetString1() { return string1;}
}

// and in the Main()
Array myArray = Array.CreateInstance(typeof(class1), 50); // No
errors/problem this far ....
// Here I'd like to get/set value of String1, HOW ?
//--------------------------------------------------------------------------
--
// The way I can think of is like this
class1 myVar = new class1();
myVar.string2 = "blablabla";
myArray.SetValue(myVar, 5);
//--------------------------------------------------------------------------
--
// But how do I make a function call in class1 in the array ? I can't figure
out how to it with ex. GetValue() ...

2. If the above is solved, how do I sort the Array using Sort() ?
If I'd for example like to sort it alphabetically on string1 ...


TIA (again)
 
B

B Mermuys

Hi,
[inline]

H said:
Ok, now this newbie is dealing with arrays.
I have some questions on this. I thought I'd use the built in Array class,
since it contains all I need for sorting, copying etc. arrays. Now I'm
wondering if this is so clever. As I've understood it, one good thing with
the Array class is that it is partly dynamical, since there is a function
for adding elements to the array, although it thtows an exception...

You cannot add elements to an Array once it's instantiated.
If you want to remove & add elements dynamical use e.g. ArrayList.
But what I'm having the real problems with is to access the values in the
created array ...
For Example

class class1
{
private String string1;
public String string2;

public String GetString1() { return string1;}

A property would be perfect for this:
public string String1
{
get { return string1; }
set { string1 = value; } // remove if you want it readonly
}
}

// and in the Main()
Array myArray = Array.CreateInstance(typeof(class1), 50); // No
errors/problem this far ....

When declaring arrays, the Array class is used implicit and the syntax is
easier:
class1[] myArray = new class1[50];
myArray[0] = new class1();
myArray[0].String1 = "klk";
...
Console.WriteLine ( myArray[0].String1 );

Sort is a static method in the Array class, you can use it like:
Array.Sort( myArray );
//note that sort only works when the objects in the array implement
IComparable


HTH
greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top