array with multiple datatypes, how?

J

Jeff

Dear experts!

..NET 2.0

I'm trying to make an array containg multiple datatypes. This array will
consist of 3 items (string, string, integer):

my first try was this, (of course it fails)
string[] param = new string[3];
param[0] = "2007-08-01";
param[1] = "2007-08-31";
param[2] = "0";

any suggestions on how to create such an array containing items of different
datatypes are most welcome :)

Jeff
 
G

Guest

Hi Jeff

The simplest way would be to declare the type as object eg.

object[] param = new object[3];
param[0] = "2007-08-01";
param[1] = "2007-08-31";
param[2] = 0;

The array can then contain ANY .Net type e.g. Int, String, Control, Form
etc....

HTH

Ged
 
J

joachim

Do not forget to cast to the appropriate datatype when reading out the
objects

// Store
object[] param = { "abc", "def", 123 };

// Read
string s0 = (string)param[0];
string s1 = (string)param[1];
int i = (int)param[2];

Joachim
 
P

Peter K

Do not forget to cast to the appropriate datatype when reading out the
objects

// Store
object[] param = { "abc", "def", 123 };

// Read
string s0 = (string)param[0];
string s1 = (string)param[1];
int i = (int)param[2];

Hi, I've seen this sort of thing in parameters to methods before, and I was
wondering what the benefit is with using an "array of objects" instead of
defining your own type (fx a class) to hold the string/string/int values?
 
?

=?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

Hi, I've seen this sort of thing in parameters to methods before, and I was
wondering what the benefit is with using an "array of objects" instead of
defining your own type (fx a class) to hold the string/string/int values?

You can create an struct who had some fields to hold string/int and so
on.
But it will fixed to that type of fields, the objects array is
completly generic (dont in the generics** sense)
any type cast to object!
 
B

bob

Dear experts!

.NET 2.0

I'm trying to make an array containg multiple datatypes. This array will
consist of 3 items (string, string, integer):

my first try was this, (of course it fails)
string[] param = new string[3];
param[0] = "2007-08-01";
param[1] = "2007-08-31";
param[2] = "0";

any suggestions on how to create such an array containing items of different
datatypes are most welcome :)

Jeff

Hi Jeff,
you could stay with the above code and use the tryParse method of each
of the datatypes when pulling your values out. (Assuming not too many
datatypes otherwise it becomes unwieldy.)

Bob
 

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