![]() |
Array question
I'm not sure if I'm being totally thick here, or just this is really something I can't do in C#
I want to create an array that contains a string, an int and a long (say for example) and I just can't work out how I could do this, I know that string[3][] would create an array of all strings, would I have to do something like object[3][] and then object[0][] = string[] something like that? |
Do you mean you want an array that contains each of the characters form a string?
Or do you want an array where each element contains a string? |
Arrays, in C#, contain elements of a single type. You can't have an array that holds a string in one element, and integer in another element, and a buffalo in another.
|
My God I was being thick. It did not occur to me that that was what Arla ment.
Well he could just have an array of objects. It would need some casts and would need to be managed as you need to know exactly what each element really is. Its not really an ideal solution. If you could tell us the problem you are trying to solve Arla then there might be a better way. |
Tim,
Not sure I can easily explain the problem, it was more one of theories and potential ways to do something rather than "oh my word I can't possibly do this" just trying to determine optimal solutions, as such I think I've got it now, just added the required data together to make one "super" type. |
Quote:
Thanks for confirming, it's what I'd figured/feared and I can do what I want another route, just wanted to check that I wasn't overly complicating something. |
If you have an array of object you can put anything you want into the array
The problem comes when you want to get data out, you have to know the type that the data was before it went into the array and was cased to type object. C# is a statically typed langage so you cant just have a variable that is everything to everyone. It also is dynamically type checked so if you try to make a string do something that only an integer can do it will fail. :
object[] myArray = new object[10];This probably wont solve your problem as i think you want it so you can put anything in and then get it out without knowing what it was to start with. edit: If you look into reflection you may be able to tell the type for the element at runtime and so have the correct cast. |
Maybe you are looking for a structure.
:
public struct MyStruct |
I guess this is what I get for not being so active here lately, as both my suggestions have already been made.
@Arla: If you have a fixed relationship between the types, for example, a 'unit' of data is composed of a string, an int, and a long, then structs (or classes) are definitely the way to go. If you won't know the types beforehand, then you can do what Tim suggested, and stuff everything into a collection (array or otherwise) of objects. Then, you can use the GetType() method to determine the type at runtime, and process it as appropriate. To get the type at compile time, use the typeof operator. For example, assume you've stuffed in a bunch of elements into an array. You could probably do something like this: :
for(int x=0; x<array.length; ++x)If you're curious, you can also use the GetMethods(), GetMembers(), and GetProperties() methods of the System.Type class (both GetType() and typeof return a System.Type, so you can call these methods on the returned object). The last one is especially handy (it's an integral part of the mechanism through which certain data-bound controls can determine the various fields- ie, columns- to display). This page gives further details. |
| All times are GMT -5. The time now is 1:26 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC