Solved

Anyway to keep different value types in the same array?

Posted on 2013-12-02
4
249 Views
Last Modified: 2013-12-02
What I would like to know is - Is there a way I can do this in C#?:

array(
   array("Joe" = true),
   array("Bob" = 3),
   array("Alex" = "Hello")
)

Having stored different value types in the array like that.
0
Comment
Question by:vituxa
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39690284
You can store an array of objects. An object is the base "object" of all objects like strings or whatever. You can create and array of objects and put anything into it, but then you'd have to evaluate what those objects are to cast them first.
Of stuff your own objects inside it, each having a tostring overriden function so you could always get the string value.
0
 
LVL 1

Author Comment

by:vituxa
ID: 39690315
Any code sample would be greatly appreciated.
0
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
ID: 39690445
Here are some samples:
http://stackoverflow.com/questions/7329178/array-or-someother-collection-where-i-can-store-multiple-types

array1[0] = new object();
array1[1] = new StringBuilder("Initialized");
array1[2] = "String literal";
array1[3] = 3;
array1[4] = null;

Open in new window

(source: http://stackoverflow.com/questions/6482331/how-to-add-different-types-of-objects-in-a-single-array-in-c)
0
 
LVL 1

Author Closing Comment

by:vituxa
ID: 39690572
Thank you! That was exactly what I needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now