6

Can I define an array such that the first element is String, the Second is an int and the third is a textbox?

It's like when we create a List we choose type of element List<string >

Update from Comment:

Sorry I couldnt explain.I need to like this List<string,int,object> Firstly i will set type and when i call the list i will not need to cast

thanks

1
  • Are the three types related? Should they be in a holder class? Have you considered using dynamic? Commented May 10, 2011 at 14:46

11 Answers 11

9

create list of objects. in C# everything is derived from object

List<object> list = new List<object> {"first", 10, new TextBox()};

EDIT(To comment):

Then you should create seperate class to hold those three items , or use Tuple

List<Tuple<string,int,TextBox>> list;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I couldnt explain.I need to like this List<string,int,object> Firstly i will set type and when i call the list i will not need to cast
6

You can declare an array of object and do that. You're talking about a mixed type array, right?

var arr = new object[] { "Hi", 42, 3.7, 'A' }

Comments

4

If you need an array that has elements without a common base-class other than object, then you're going to need an array of objects!

object[] myArray = new object[] { "Hi", 23, new TextBox() };

Note that this is not really something you should doing. If you need to associate disparate types like this, a class makes much more sense.

Comments

4

You want a Tuple<string,int,TextBox>, not an array.

Comments

3

IMHO the best way to do this is through a List<> of objects:

String  s = "hey!";
int     i = 156;
TextBox t = new TextBox();

List<object> list = new List<object>(3);

list.Add(s);
list.Add(i);
list.Add(t);

The reason this works is because (almost?) everything in C# derives from the base-class object

Comments

3

Arrays are typically homogeneous collections, which means that every object contains in the array is of the same type (or at least shares a common parent type). An array of [string, int, textbox] could be defined as an object[] but that's really misuse of arrays.

Just create a proper class which contains the 3 fields.

class MyType {
    public string myString;
    public int myInt;
    public Listbox myListbox;
}

Comments

1

If you're looking make a list of string, int, textbox, you can either create a class which has those members or look at the Tuple class in .net 4.0

List<Tuple<string,int,TextBox>

Comments

0

Define a class that contains the 3 types then define an array that contains the new type.

Comments

0

Object[] myObjects = new Object(){"myString", 42, textbox1};

Comments

0
System.Collections.Generic.Dictionary<string, object> source = new System.Collections.Generic.Dictionary<string, object>();


source.Add("A", "Hi");

source.Add("B", 10);

source.Add("C", new TextBox());

While accessing

string str = Convert.ToString(source["A"]);

int id = Convert.ToInt16(source["B"]);

TextBox t = (TextBox)source["C"];

Comments

0

I will suggest that you create a Type such as

enum ItemType { Int, String, Textbox }
class MyType {
    public object objValue;
    public ItemType itemType;
}
List<MyType> list = new List<MyType>();
.......

You can iterate through the list or extract the list by type such as below.

var intList = list.Where(e=>e.itemType == ItemType.Int);

Of course you can achieve the above with the enum and using the reflected Type info directly from the object, but I just think it is clearer this way also more explicitly list out the type your list can hold rather than just all type in the CLR

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.