Solved

Anyway to keep different value types in the same array?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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