16

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.

4 Answers 4

40

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your Quick Reply, 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 do like this...
If you want to do that, I would simply define a class to encapsulate your data and just have an array or list of instances of that class. Jon's answer suggests perhaps an anonymous type, which is also an option.
sure sir, it would be a great help to me.
5

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.

2 Comments

Thanks to give a Quick Reply, I modified the question, please have a look sir. Actually i am storing the product_id, process_id, note into array for every form post. Finally i will manipulate the array and store into database.
@user707852: That doesn't really change my answer - you could apply my answer to either case. To be honest though, I suspect you'd be better off creating a type to encapsulate these values...
2

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");

2 Comments

I think List<T> makes more sense than an array here.
Oups yeah with the edited question, same structure, it would make alot more sense too use the above answer and it is much more elegant.
1

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"}};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.