![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 220
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 220
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 220
Rep Power: 4
![]() |
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. |
|
|
|
|
|
|
#7 |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
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];
myArray[0] = 20;
myArray[1] = "Hello World";
myArray[2] = 3.14;
//The cast must be correct here.
int i = (int)myArray[0];
string s = (string)myArray[1];
double d = (double)myArray[2];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. |
|
|
|
|
|
#8 |
|
Programmer
|
Maybe you are looking for a structure.
public struct MyStruct
{
public int ID;
public string Name;
public double Value;
}
public MyStruct[] MyStructArray = new MyStruct[10]; // using array
or
public System.Collections.ArrayList MyStructArray = new System.Collections.ArrayList(); |
|
|
|
|
|
#9 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
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(array[x].GetType() == typeof(string))
Console.WriteLine("Type is string");
else if(array[x].GetType() == typeof(int))
Console.WriteLine("Type is int");
else if(array[x].GetType() == typeof(long))
Console.WriteLine("Type is long");
}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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multi array question | NightShade01 | Java | 17 | May 11th, 2006 10:21 PM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| array question | crazykid48x | C++ | 8 | May 27th, 2005 10:04 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| Simple question: Easiest way to search through a character array | willz99ta | C++ | 6 | Mar 26th, 2005 2:15 AM |