11 Replies - 622 Views - Last Post: 21 April 2011 - 06:13 PM Rate Topic: -----

#1 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Multidimension arrays and types

Posted 20 April 2011 - 11:58 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Multidimension arrays and types

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4927
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

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.
Was This Post Helpful? 1
  • +
  • -

#3 here.to.code  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 55
  • Joined: 15-February 11

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?

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

Was This Post Helpful? 3
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3795
  • View blog
  • Posts: 6,399
  • Joined: 08-June 10

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:

public class OurType{
  public string Name {get;set;}
  public int Number {get;set;}
}

Was This Post Helpful? 3
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

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? :huh:
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3795
  • View blog
  • Posts: 6,399
  • Joined: 08-June 10

Re: Multidimension arrays and types

Posted 21 April 2011 - 07:15 AM

No, jagged arrays are basically arrays of arrays of a type.
Was This Post Helpful? 1
  • +
  • -

#7 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

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.
Was This Post Helpful? 0
  • +
  • -

#8 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

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

   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

Was This Post Helpful? 0
  • +
  • -

#9 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 743
  • Joined: 11-September 10

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.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3795
  • View blog
  • Posts: 6,399
  • Joined: 08-June 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.
Was This Post Helpful? 2
  • +
  • -

#11 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 743
  • Joined: 11-September 10

Re: Multidimension arrays and types

Posted 21 April 2011 - 04:14 PM

View PostCurtis Rutland, on 21 April 2011 - 11:29 AM, said:

You could do that. You could also gouge your eyes out with a rusty spoon. Neither would be pleasant or good practice.


I know.
Still it's possible and works even though very painful and stupid :P
Was This Post Helpful? 0
  • +
  • -

#12 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Multidimension arrays and types

Posted 21 April 2011 - 06:13 PM

View PostCurtis Rutland, on 21 April 2011 - 02:15 PM, said:

No, jagged arrays are basically arrays of arrays of a type.

Thanks for clearing that up for me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1