array with multiple datatypes, how?

  • Thread starter Thread starter Jeff
  • Start date Start date
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
 
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
 
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
 
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?
 
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!
 
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
 
Back
Top