rkaartikeyan
rkaartikeyan

Reputation: 2007

Array Key Value in ASP .NET with C#

I am new to asp.net with C#. Now I need a solution for one issue.

In PHP I can create an array like this:

$arr[] = array('product_id' => 12, 'process_id' => 23, 'note' => 'This is Note');

//Example
Array
(
    [0] => Array
        (
            [product_id] => 12
            [process_id] => 23
            [note] => This is Note
        )

    [1] => Array
        (
            [product_id] => 5
            [process_id] => 19
            [note] => Hello
        )

    [2] => Array
        (
            [product_id] => 8
            [process_id] => 17
            [note] => How to Solve this Issue
        )

)

I want to create the same array structure in asp.net with C#.

Please help me to solve this issue.

Upvotes: 16

Views: 86744

Answers (4)

Khan
Khan

Reputation: 516

Try this

System.Collections.Generic.Dictionary<string, object>[] map = new System.Collections.Generic.Dictionary<string, object>[10];

map[0] = new System.Collections.Generic.Dictionary<string,object>();
map[0].Add("product_id", 12);
map[0].Add("process_id", 23);
map[0].Add("note", "This is Note");

map[1] = new System.Collections.Generic.Dictionary<string,object>();
map[1].Add("product_id", 5);
map[1].Add("process_id", 19);
map[1].Add("note", "Hello");

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502286

If you're looking for a mapping from string to object:

Dictionary<string, object> map = new Dictionary<string, object> {
    { "product_id", 12 },
    { "process_id", 23 },
    { "note", "This is Note" }
};

Alternatively, perhaps you'd like an anonymous class, if this is just a way of passing data around:

var values = new {
    ProductId = 12,
    ProcessId = 23,
    Note = "This is Note"
};

It really depends on what you're trying to achieve - the bigger picture.

EDIT: If you've got the same "keys" for multiple values, I would probably create a specific type for this - it's not clear what sort of entity this is meant to represent, but you should create a class to model it, and add appropriate behaviour to it as required.

Upvotes: 5

Anand
Anand

Reputation: 14935

Associative array could be represented in C# using Dictionary. Its Enumerator.Current would return a keyValuePair.

So your array would like

var associativeArray = new Dictionary<string, string>(){ {"product_id", "12"}, {"process_id"," 23", {"note","This is Note"}};

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126932

Use a Dictionary<TKey, TValue> for quick lookups of a value (your object) based on a key (your string).

var dictionary = new Dictionary<string, object>();
dictionary.Add("product_id", 12);
// etc.

object productId = dictionary["product_id"];

To simplify the Add operation, you could use collection initialization syntax such as

var dictionary = new Dictionary<string, int> { { "product_id", 12 }, { "process_id", 23 }, /* etc */ };

Edit

With your update, I would go ahead and define a proper type to encapsulate your data

class Foo
{
    public int ProductId { get; set; }
    public int ProcessId { get; set; }
    public string Note { get; set; } 
}

And then create an array or list of that type.

var list = new List<Foo>
           {
                new Foo { ProductId = 1, ProcessId = 2, Note = "Hello" },
                new Foo { ProductId = 3, ProcessId = 4, Note = "World" },
                /* etc */
           };

And then you have a list of strongly-typed objects you can iterate over, bind to controls, etc.

var firstFoo = list[0];
someLabel.Text = firstFoo.ProductId.ToString();
anotherLabel.Text = firstFoo.Note;

Upvotes: 40

Related Questions