Jump to content

C# Multidimensional Add

Olias

Hey guys 

i want do add something to a Multidimensional Dictionary.

But it doesn`t work.....

Would be rly nice if you could help me.

 

        private Dictionary<int, Dictionary<String, Boolean>> test = new Dictionary<int, Dictionary<string, bool>>(); 

// ins is a String

                test.Add(1, ins, false);

Link to comment
Share on other sites

Link to post
Share on other sites

A few things really: Foremost, you should be using code tags to present code here really. Secondly 'ins' is not a string. Finally you are attempting to add a discrete item representing multiple dimensions of the generic collection.

 

There's a few ways of accomplishing this; here's a simple console application demonstrating Initializer Syntax and adding at runtime:

using System;using System.Collections.Generic;namespace ConsoleApplication1{    class Program    {               static void Main(string[] args)        {            var initializerSyntaxTest = new Dictionary<int, Dictionary<string, bool>>                        {                            {                                1,                                new Dictionary<string, bool>                                    {                                        {                                            "Some string",                                            false                                        },                                        {                                            "Some other string",                                            true                                        }                                    }                            },                            {                                2,                                new Dictionary<string, bool>                                    {                                        {                                            "Another string",                                            true                                        },                                        {                                            "Another different string",                                            false                                        }                                    }                            }                        };            Console.WriteLine("Initializer Syntax Test:");            OutputStructure(initializerSyntaxTest);            Console.WriteLine();            Console.WriteLine();            var runtimeTest = new Dictionary<int, Dictionary<string, bool>>();            for (var i = 0; i < 10; i++)            {                var subDimension = new Dictionary<string, bool>();                for (var j = 0; j < 5; j++)                {                    subDimension.Add(Guid.NewGuid().ToString(), j % 2 == 0);                }                runtimeTest.Add(i, subDimension);            }            Console.WriteLine("Runtime Test:");            OutputStructure(runtimeTest);            Console.ReadKey();        }        private static void OutputStructure(Dictionary<int, Dictionary<string, bool>> input)        {            foreach (var kvp in input)            {                foreach (var subKvp in kvp.Value)                {                    Console.WriteLine("First dimension Key: {0}, second dimension Key: {1}, second dimension Value: {2}", kvp.Key, subKvp.Key, subKvp.Value);                }            }        }    }}

It should be noted that if you attempt to add something to the second dimension without initializing it first you will get a NullReferenceException.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

First thing, try and put code into code tags.

 

I don't know C#, but I can guess what the issue is.

You are creating a dictionary that has String type keys, and Dictionary type values.

You correctly initialize the object:

private Dictionary<int, Dictionary<String, Boolean>> test = new Dictionary<int, Dictionary<String, Boolean>>();

But you are not correctly inserting a new entry into the dictionary:

// Can't do this, the ins String and False bool primative are not in a dictionary// test.Add(1, ins, false);//Initialize main dictionaryDictionary<int, Dictionary<String, Boolean>> main = new Dictionary<int, Dictionary<String, Boolean>>();int mainKey = 1;//Prep the Dictionary valueString subKey = "Some string key";Boolean subValue = false;Dictionary<String, Boolean> dictValue = new Dictionary<String, Boolean>();dictValue.Add(subKey,subValue);//Now Add to main dictmain.Add(mainKey,dictValue);

I hope this helps.

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to comment
Share on other sites

Link to post
Share on other sites

It should be noted that if you attempt to add something to the second dimension without initializing it first you will get a NullReferenceException.

And if you add the same item twice you will get an ArguementException.

 

So if the indices for the first dimension can't be done in a simple for loop at the start of the program, you'll need to keep checking if there is already an item with that key when you insert a new value. Something like:

private Dictionary<int, Dictionary<string, bool>> test = new Dictionary<int, Dictionary<string, bool>>();Dictionary<string, bool> subDict;if(test.TryGetValue(index, out subDict)){    subDict.Add("some string",false);}else{        subDict = new Dictionary<string,bool>();    subDict.Add("some string",false);    test.Add(index,subDict);}

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

And if you add the same item twice you will get an ArguementException.

 

So if the indices for the first dimension can't be done in a simple for loop at the start of the program, you'll need to keep checking if there is already an item with that key when you insert a new value. Something like:

 

-code snip-

 

True! Also if one doesn't care then they can simply set or add an item by using the default index property demonstrated here.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×