tags:

views:

129

answers:

5

Hi guys.

I have just recently been doing something in C#, i would like to know how to do something like this.

Array[0] =
  Array['Value'] = 2344;
  Array['LocationX'] = 0;
  Array['LocationY'] = 0;
Array[1] =
  Array['Value'] = 2312;
  Array['LocationX'] = 2;
  Array['LocationY'] = 1;
Array[2] =
  Array['Value'] = 2334;
  Array['LocationX'] = 4;
  Array['LocationY'] = 3;

The data it self its not important, the thing is that i know how to do this in PHP. But in C# i don't, and I've tried some ways and no luck.

In PHP i could just do something like this:

$Array[0]->Value = 2344;
$Array[0]->LocationX = 0;
$Array[0]->LocationY = 0;

And those values would be added to the Array.

In C# i've tried this and doesn't work that way.

Could someone enlighten me in how to do this in C#?

Thanks.

+2  A: 

Well, you could have an array of instances of a class that you write like so:

public class DataForArray
{
    public int Value { get; set; }
    public int LocationX { get; set; }
    public int LocationY { get; set; }
}

Then something like this:

DataForArray[] array = new DataForArray[10];
array[0] = new DataForArray();
array[0].Value = 2344;
etc...
David M
Thanks. It worked just fine.
Fábio Antunes
+4  A: 

Either write a class or struct to hold Value, LocationX and LocationY.

struct Foo
{
  Foo(value, x, y)
  {
    Value = value;
    LocationX = x;
    LocationY = y;
  }

  Foo() {}

  int Value;
  int LocationX;
  int LocationY;
}

Foo[] f = new [] 
{
  new Foo(1, 2, 3), 
  new Foo(2, 3, 4)
}

or alternatively initialize the array this way:

Foo[] f = new [] 
{
  new Foo() { Value = 1, LocationX = 2, LocationY = 3 },
  new Foo() { Value = 4, LocationX = 5, LocationY = 6 },
}

Or use an Array of Dictionary<string, int>.

Dictionary<string, int>[] array = new []
  {
    new Dictionary<string, int>() {{ "Value", 1 }, {"LocationX", 2}, {"LocationY", 3 }},
    new Dictionary<string, int>() {{ "Value", 4 }, {"LocationX", 5}, {"LocationY", 6 }}
  }

Which is only recommended if it needs to be dynamic (means: you want to have different values in each element of the array or your keys are in strings, not known at compile-time.) Unless it is just hard to maintain.

Stefan Steinegger
A: 

Here is a link that details the use of Multidimensional arrays

http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx

GX
+3  A: 

in C#, you can try something like this

// initialize array
var list = new[]
               {
                   new {Value = 2344, LocationX = 0, LocationY = 0},
                   new {Value = 2312, LocationX = 2, LocationY = 4},
                   new {Value = 2323, LocationX = 3, LocationY = 1}
               }.ToList();

// iterate over array
foreach (var node in list)
{
    var theValue = node.Value;
    var thePosition = new Point(node.LocationX, node.LocationY);
}

// iterate over array with filtering ( value > 2300 )
foreach (var node in list.Where(el => el.Value > 2300))
{
    var theValue = node.Value;
    var thePosition = new Point(node.LocationX, node.LocationY);
}

// add again
list.Add(new { Value = 2399, LocationX = 9, LocationY = 9 });
Anwar Chandra
this is horrible.... things should have a clear decleared type.
Hellfrost
@Hellfrost: that's true, but it is very close to the php implementation. There is even a bit more type safety as using duck types.
Stefan Steinegger
@Hellfrost: no it's not. It's absolutely clear and absolutely defined and absolutely uses static typing. This is not dynamic typing. This is not "duck typing" either.
Dave Markle
A: 

You can use anonymous type in C# like that:

var arr = new[] {
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3} };

Only one problem is that properties in anonymous type are read-only. So You can't do something like that:

arr[1].Value = 2
Millionbonus
This is what Q8-coder already proposed.
Stefan Steinegger