472,135 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

array with multiple datatypes, how?

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
Aug 24 '07 #1
6 5418
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
"Jeff" <do***@spam.mewrote in message
news:O0****************@TK2MSFTNGP05.phx.gbl...
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
Aug 24 '07 #2
or short-hand:

object[] args = { "abc", "def", 123 };

Marc

Aug 24 '07 #3
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
Aug 24 '07 #4
"jo*****@yamagata-europe.com" <jo*****@yamagata-europe.comwrote in
news:11**********************@q4g2000prc.googlegro ups.com:
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?
Aug 24 '07 #5
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!

Aug 24 '07 #6
bob
On Fri, 24 Aug 2007 11:18:02 +0200, "Jeff" <do***@spam.mewrote:
>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
Aug 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Josué Maldonado | last post: by
3 posts views Thread by Pablo Gutierrez | last post: by
6 posts views Thread by Steve Wasser | last post: by
5 posts views Thread by Stephen3776 | last post: by
3 posts views Thread by Joe Cool | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.