views:

1142

answers:

4

hello,

I'm trying to figure out how to build a multi-dimensional "array" that is:

  • flexible size
  • use 2 keys
  • 1st key is int (flexible)
  • 2nd key is string (kind of limited)

The use will be like:

console.writelen(array[0]["firstname"]);
console.writelen(array[0]["lastname"]);
console.writelen(array[0]["phone"]);

console.writelen(array[1]["firstname"]);
console.writelen(array[1]["lastname"]);
console.writelen(array[1]["phone"]);

.....
.....

console.writelen(array[x]["firstname"]);
console.writelen(array[x]["lastname"]);
console.writelen(array[x]["phone"]);
A: 

I don't believe you can do this with an Array unless you have a single Array of KeyValuePair<int,string>, but I think you really want a Dictionary<int,string>.

var dic = new Dictionary<int,string>();
dic[0] = "zero";
dic[1] = "one";
dic[2] = "two";

foreach(KeyValuePair<int,string> kvp in dic)
{
   Console.WriteLine(String.Format("Key: {0}, Value: {1}",kvp.Key,kvp.Value);

}
panamack
Oh, sorry I didn't pick up on the 2 keys part.
panamack
+2  A: 

You can simply use this:

Dictionary<int, Dictionary<string, string>>

Use it like this:

var dd = new Dictionary<int, Dictionary<string, string>>();
dd[5] = new Dictionary<string, string>();
dd[5]["a"] = "foo";

You can also create a new class to simplify the creation of the inner dictionary:

class DDict { // optional: generic
    private readonly Dictionary<int, Dictionary<string, string>> _Inner = new ...;

    public Dictionary<string, string> this (int index) {
        Dictionary<string, string> d;
        if (!_Inner.TryGetValue(index, out d)) {
             d = new Dictionary<string, string>();
             _Inner.Add(index, d);
        }
        return d;
    }
}

var dd = new DDict();
dd[5]["a"] = "hi";

If the first index is sequential, you can of course also just use an array of dictionaries:

var dd = new Dictionary<string, string>[128];

Also, if the inner members are always the same, I suggest to create a new class and access it in an array:

class Dat {
    string name;
    string phone;
}
var list = new Dat[128]

// access:
list[5].name = "matt";

Instead of an array, you could also use a List or a Dictionary<int, Dat> in that case.

mafutrct
+1 from me for reading the question correctly lol
panamack
+5  A: 

Are you sure it wouldn't be more appropriate to create a class/struct to contain the data? For example:

class Person
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public string Phone
    {
        get;
        set;
    }
}

Then you'd just create an array of Person:

var array = new Person[1];
array[0] = new Person() { FirstName = "Joe", LastName = "Smith", Phone = "foo" };

Or, since you say "flexible size", maybe you'd want a list instead:

var list = new List<Person>();
list.Add(new Person());

Update: The syntax used to set array[0] in the first example is an object initializer; the following two snippets are roughly equivalent:

var foo = new Person();
foo.FirstName = "John";

var bar = new Person() { FirstName = "John" };

So yes, you could just call list.Add(new Person() { ... }) if you wanted to. You could also make use of collection initializers in this case:

var john = new Person() { FirstName = "John" };
var joe = new Person() { FirstName = "Joe" };
var list = new List<Person>() { john, joe };
Will Vousden
var list = new List<Person>();list.Add(new Person());very interestinghow to add the values?list.Add(new Person(){ FirstName = "Joe", LastName = "Smith", Phone = "foo" });like this?
Data-Base
@Data-Base: I've updated my answer to clarify.
Will Vousden
A: 

Actually i just see two dimensions. The first one is a row index, the second is a column index. And this sounds like a DataTable to me.

Oliver
in that case in this example http://dotnetperls.com/datatable-foreachhow to show the item label{name,phone} instead of "item:"cheers
Data-Base