Solved

Anyway to keep different value types in the same array?

Posted on 2013-12-02
4
246 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

16 Experts available now in Live!

Get 1:1 Help Now