1

I am newbie in asp.net and I want to insert data from my dynamic form in the database. To identify fields I am also saving the labels of the fields in the database.

For this purpose I want to use a multi dimensional array. The problem is that I want to declare the key of the array when I assig the value, like we do below in php:

myarray['key_label'] = 'key_value';

I am not sure how can I do this in C#.net. I searched and found dictionary for this purpose, so I used the below code, but in this case I can view key or value any one at once:

 var dictionary = new Dictionary<string, object>();
 dictionary.Add("username", rpu);
 dictionary.Add("sec_username", rcu);
 dictionary.Add("co_type", ct);

Kindly guide me for below

  • how can I assign the value of an array like I mentioned above?

  • I found a way to display both key and values together, mentioned below but is it possible that i can make dictionary multi dimensional like we do for arrays?

    Dictionary<int, string> plants = new Dictionary<int, string>() {    
    {1,"Speckled Alder"},    
    {2,"Apple of Sodom"},    
    {3,"Hairy Bittercress"},    
    {4,"Pennsylvania Blackberry"},    
    {5,"Apple of Sodom"},    
    {6,"Water Birch"},    
    {7,"Meadow Cabbage"},    
    {8,"Water Birch"}    
    };
    
    Response.Write("<b>dictionary elements........</b><br />");
    
    //loop dictionary all elements   
    foreach (KeyValuePair<int, string> pair in plants)
    {
        Response.Write(pair.Key + "....." + pair.Value + "<br />");
    }
    
    //find dictionary duplicate values.  
    var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1);
    
    Response.Write("<br /><b>dictionary duplicate values..........</b><br />");
    
    //loop dictionary duplicate values only            
    foreach (var item in duplicateValues)
    {
        Response.Write(item.Key + "<br />");
    }   
    

    }

pixelmeow
  • 657
  • 1
  • 11
  • 30
mobi001
  • 487
  • 1
  • 8
  • 19
  • 4
    Firstly, "array" in C# doesn't mean what it does in PHP. I suspect you don't actually want an array in C#. Next, if you're hard-coding the names, is there any reason you don't want to just create a type with the relevant properties? – Jon Skeet Jun 30 '14 at 10:21
  • [You might want to check this out](http://stackoverflow.com/questions/14719955/looping-through-dictionary-object) – musefan Jun 30 '14 at 10:23
  • @JonSkeet no actually i don't want to hard code the names i have mentioned it just for an example, actually i just want to use some thing which can behave like multi dimensional array so i can fetch it up because i have nested forms, kindly guide me can i do this with the dictionary?? – mobi001 Jun 30 '14 at 10:27
  • How are you generating your form ? Can you post the code ? – Amin Sayed Jun 30 '14 at 10:29
  • What do you meand by multidimensional? Do you need like 5 properties? Name, age, size, id and so forth? – Marco Jun 30 '14 at 10:31
  • You're just trying to write PHP in C# (ASP.NET). That's pretty much guaranteed to backfire. If you're considering using ASP.NET for something serious, you should pretty much ignore everything you learned in PHP. Now, if you really want to go without strongly typing your data (usually a bad idea), have a look at the `dynamic` keyword (and data type). It works pretty much like JavaScript's objects, which is a lot closer to what PHP uses. But it'd be much better if you simply went native, instead of just trying to write PHP in C# :) – Luaan Jun 30 '14 at 10:48
  • Why don't you just use a `DataTable`? Then you can name the columns and have type safety throughout. – Michael McGriff Jun 30 '14 at 16:58

2 Answers2

1
   Dictionary<string,object> dic = new Dictionary<string,object>(){
            {"key1", "value1"},
            {"key2", 100 },
            {"key3", "value3"},            
        };

for multi diementional:

   Dictionary<string,Dictionary<string,object>> dic = new Dictionary<string,Dictionary<string,object>>(){
            {"key1", new Dictionary<string,object>() { 
                        {"sub-key1", "value1" },
                        {"sub-key1-2", "value1-2" } 
            },
            {"key2",  new Dictionary<string,object>() {
                        {"sub-key2", "value2" }
            },
            {"key3",  new Dictionary<string,object>() {
                      {"sub-key3", "value3" } 
            }            
        };

each value of dic["key"] would be a dictionary itself so you can access the values like:

dic["key1"]["sub-key1"];

Ashkan Mobayen Khiabani
  • 32,319
  • 30
  • 98
  • 161
0

how can I assign the value of an array like I mentioned above?

Yes, you can access a dictionary entry in C# like you would an array in PHP. Something like myDictionary["keyOne"] = varOne;

I found a way to display both key and values together, mentioned below but is it possible that i can make dictionary multi dimensional like we do for arrays?

Yes, like in PHP where you make an array of arrays, you would in C# create a dictionary of dictionaries:

Dictionary<string,Dictionary<string,object>> multiDict = new Dictionary<string,Dictionary<string,object>>();
multiDict["dictionary1"]["keyOne"] = varOne;
cjcurrie
  • 624
  • 1
  • 4
  • 15