Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Array question (http://www.programmingforums.org/showthread.php?t=12304)

Arla Jan 3rd, 2007 6:56 PM

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?

Tim Jan 3rd, 2007 7:22 PM

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?

DaWei Jan 3rd, 2007 7:39 PM

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.

Tim Jan 3rd, 2007 7:44 PM

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.

Arla Jan 3rd, 2007 7:52 PM

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.

Arla Jan 3rd, 2007 7:53 PM

Quote:

Originally Posted by DaWei (Post 122136)
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.

Dawei,

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.

Tim Jan 3rd, 2007 8:05 PM

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.

ChiHappens Jan 4th, 2007 3:57 PM

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();


lectricpharaoh Jan 4th, 2007 11:14 PM

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");
}

Obviously, instead of simply outputting a message, you'll want to do appropriate processing, but hopefully, you get the idea. This shows that you can (in a manner) have an array with elements of different types. Strictly speaking, this is not true, as every element is of type object (or whatever the base class in question is; you needn't go quite that far up the object hierarchy in all cases). Remember that an object of a subclass is also an object of the parent class (a Car is also a Vehicle), but the inverse is not necessarily true. Thus, there is the test to see which exact subclass the element is, in order to process it properly.

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