I've looked at various examples of multidimensional arrays on the internet, but in every example, both dimensions of the array are the same type. I was wondering if it's possible to create a multidimensional array that contains two types, for example an array that stores an int and a string. Is this possible?
Multidimension arrays and types
Page 1 of 111 Replies - 622 Views - Last Post: 21 April 2011 - 06:13 PM
Replies To: Multidimension arrays and types
#2
Re: Multidimension arrays and types
Posted 20 April 2011 - 12:05 PM
Would the Dictionary<> type work for you?
It can use two different types, but the key type has to be unique for each entry.
1, "Bob Smith"
2, "Fred Flintsone"
You couldn't have
1, "Bob"
1, "Fred"
1, "Barney"
2, "Wilma"
and so on. Also, it is strictly a 2d array.
It can use two different types, but the key type has to be unique for each entry.
1, "Bob Smith"
2, "Fred Flintsone"
You couldn't have
1, "Bob"
1, "Fred"
1, "Barney"
2, "Wilma"
and so on. Also, it is strictly a 2d array.
#3
Re: Multidimension arrays and types
Posted 20 April 2011 - 12:08 PM
I'm not sure if this would work in your case but what if you just make an array or list of a class/structure you define that holds these different values?
What you could also try is looking up Tuples.
List<OurType> OurList = new List<OurType>();
class OurType
{
string name;
int number;
}
What you could also try is looking up Tuples.
This post has been edited by here.to.code: 20 April 2011 - 12:13 PM
#4
Re: Multidimension arrays and types
Posted 20 April 2011 - 12:17 PM
One important thing for you to understand is, a multidimensional array is not the same as a parallel array.
Multidimensional arrays have to be of the same type, since instead of just being a list, they're a matrix, or a cube, or some other theoretical structure.
Parallel arrays are probably what you're thinking about. A list of strings corresponding to a list of ints. The theory behind these are just two separate arrays, and you make sure you always use the same index.
Of course, we're beyond needing parallel arrays. In modern, Object Oriented programming, there's no reason to use them anymore.
We can create classes to group multiple data blocks into discrete data points. here.to.code has shown you an example of this, though it wouldn't work as is (members are private by default).
Here's a working example:
Multidimensional arrays have to be of the same type, since instead of just being a list, they're a matrix, or a cube, or some other theoretical structure.
Parallel arrays are probably what you're thinking about. A list of strings corresponding to a list of ints. The theory behind these are just two separate arrays, and you make sure you always use the same index.
Of course, we're beyond needing parallel arrays. In modern, Object Oriented programming, there's no reason to use them anymore.
We can create classes to group multiple data blocks into discrete data points. here.to.code has shown you an example of this, though it wouldn't work as is (members are private by default).
Here's a working example:
public class OurType{
public string Name {get;set;}
public int Number {get;set;}
}
#5
Re: Multidimension arrays and types
Posted 21 April 2011 - 06:28 AM
This is more of a question really, but would Jagged Arrays allow the OP to do what they need?
#6
Re: Multidimension arrays and types
Posted 21 April 2011 - 07:15 AM
No, jagged arrays are basically arrays of arrays of a type.
#7
Re: Multidimension arrays and types
Posted 21 April 2011 - 10:15 AM
Thanks for the info, I'll definitely use a class like you had posted.
#8
Re: Multidimension arrays and types
Posted 21 April 2011 - 11:12 AM
@here.to.code
I didint know you could do that hmmmmm....
That is very interesting..
i tested that theory , on this
Results ...
t[0] ---> [{test}]
---> [{test}] = ( i = 2;) , (s = "wtf"; )
Great results ..
And it works like a charm , rep for you dude ...
I didint know you could do that hmmmmm....
That is very interesting..
i tested that theory , on this
public void theShit()
{
List<test> t = new List<test>();
test it = new test();
t.Add(it);
}
}
}
class test
{
public string s = "wtf";
public int i = 2;
}
Results ...
t[0] ---> [{test}]
---> [{test}] = ( i = 2;) , (s = "wtf"; )
Great results ..
And it works like a charm , rep for you dude ...
This post has been edited by marinus: 21 April 2011 - 11:15 AM
#9
Re: Multidimension arrays and types
Posted 21 April 2011 - 11:23 AM
Well you could do a multi-dimensional array of objects. Then each dimension could be a different type and all you would have to do is make a cast when you are retrieving the value.
#10
Re: Multidimension arrays and types
Posted 21 April 2011 - 11:29 AM
You could do that. You could also gouge your eyes out with a rusty spoon. Neither would be pleasant or good practice.
#11
Re: Multidimension arrays and types
Posted 21 April 2011 - 04:14 PM
#12
Re: Multidimension arrays and types
Posted 21 April 2011 - 06:13 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|