Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 3rd, 2007, 6:56 PM   #1
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 302
Rep Power: 4 Arla is on a distinguished road
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?
Arla is offline   Reply With Quote
Old Jan 3rd, 2007, 7:22 PM   #2
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
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?
Tim is offline   Reply With Quote
Old Jan 3rd, 2007, 7:39 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jan 3rd, 2007, 7:44 PM   #4
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
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 is offline   Reply With Quote
Old Jan 3rd, 2007, 7:52 PM   #5
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 302
Rep Power: 4 Arla is on a distinguished road
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 is offline   Reply With Quote
Old Jan 3rd, 2007, 7:53 PM   #6
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 302
Rep Power: 4 Arla is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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.
Arla is offline   Reply With Quote
Old Jan 3rd, 2007, 8:05 PM   #7
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
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.
Tim is offline   Reply With Quote
Old Jan 4th, 2007, 3:57 PM   #8
ChiHappens
Programmer
 
ChiHappens's Avatar
 
Join Date: Sep 2005
Location: Brevard, NC
Posts: 35
Rep Power: 0 ChiHappens is an unknown quantity at this point
Send a message via MSN to ChiHappens
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();
__________________
http://www.starshipcombatsimulator.com
They mostly come at night...mostly.
ChiHappens is offline   Reply With Quote
Old Jan 4th, 2007, 11:14 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
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
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi array question NightShade01 Java 17 May 11th, 2006 11:21 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 9:19 PM
array question crazykid48x C++ 8 May 27th, 2005 11:04 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM
Simple question: Easiest way to search through a character array willz99ta C++ 6 Mar 26th, 2005 3:15 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC