How to build an (list)array of types

G

G.Esmeijer

Friends

Coming from vb I was used to do the following

private type AnyType
A as STRING
B as INTEGER
end Type

DIM anyArray() as AnyType

after giving the array a dimension like REDIM anyArray(1 to 10)
I was able to access any element as follows:

anyArray.A(1) = "MERCEDES"
anyArray.B(1) = "123"
anyArray.A(2) = "BMW"
anyArray.B(2) = "234"
etc

How can I achieve something like this in C#???
please help

Gerrit Esmeijer
 
D

Du Dang

struct AnyType {
public string A;
public int B;
}

AnyType[] anyArray = new AnyType[10];

anyArray.A[0] = "MERCEDES";
anyArray.B[0] = 123;
anyArray.A[1] = "BMW";
anyArray.B[1] = 234;

I think this would work, however I haven't test it out yet.
If it doesn't work let me know

hope this help,

Du
 
?

=?iso-8859-15?Q?Lasse_V=E5gs=E6ther_Karlsen?=

struct AnyType {
public string A;
public int B;
}

AnyType[] anyArray = new AnyType[10];

anyArray.A[0] = "MERCEDES";
anyArray.B[0] = 123;
anyArray.A[1] = "BMW";
anyArray.B[1] = 234;

I think this would work, however I haven't test it out yet.
If it doesn't work let me know

It's "anyArray" that is an array, not the A and B members of the type.
Thus to use it:

anyArray[0].A = "MERCEDES";
anyArray[0].B = 123;

etc.

I don't think VB allowed that syntax he showed either, I think the same
change as I showed here would have to be done to his code, but I'm not
100% sure.
 
D

Du Dang

anyArray.A[0] = "MERCEDES";
anyArray.B[0] = 123;
anyArray.A[1] = "BMW";
anyArray.B[1] = 234;

that supposes to look like the following

anyArray[0].A = "MERCEDES";
anyArray[0].B = 123;
anyArray[1].A = "BMW";
anyArray[1].B = 234;

copy and paste devil :)


Du Dang said:
struct AnyType {
public string A;
public int B;
}

AnyType[] anyArray = new AnyType[10];

anyArray.A[0] = "MERCEDES";
anyArray.B[0] = 123;
anyArray.A[1] = "BMW";
anyArray.B[1] = 234;

I think this would work, however I haven't test it out yet.
If it doesn't work let me know

hope this help,

Du

G.Esmeijer said:
Friends

Coming from vb I was used to do the following

private type AnyType
A as STRING
B as INTEGER
end Type

DIM anyArray() as AnyType

after giving the array a dimension like REDIM anyArray(1 to 10)
I was able to access any element as follows:

anyArray.A(1) = "MERCEDES"
anyArray.B(1) = "123"
anyArray.A(2) = "BMW"
anyArray.B(2) = "234"
etc

How can I achieve something like this in C#???
please help

Gerrit Esmeijer
 
N

Noah Coad [MVP .NET/C#]

There isn't a "redim" or such in C#, but you can create an of items like
this:

struct AnyType
{
public string A;
public int B;
}

AnyType[] anyArray = new AnyType[10];
anyArray[0].A = "Test";

- Noah Coad -
Microsoft MVP

P.S. I believe this is even easier... :)
 

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