Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

c# nested dictionarys

Discussion in 'Scripting' started by rainbow_design, Aug 20, 2016.

  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Last edited: Aug 20, 2016
  2. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    344
    I can't really write it for you without knowing what you need (and to be honest, I usually don't write code for others anyway) but this should hopefully get you on the right path:
    https://unity3d.com/learn/tutorials/topics/scripting/extension-methods

    Calling an Extension Method (SomeClass.cs)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SomeClass : MonoBehaviour
    5. {
    6.     void Start () {
    7.         //Notice how you pass no parameter into this
    8.         //extension method even though you had one in the
    9.         //method declaration. The transform object that
    10.         //this method is called from automatically gets
    11.         //passed in as the first parameter.
    12.         transform.ResetTransformation();
    13.     }
    14. }
    Creating an Extension Method (ExtensionMethod.cs)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //It is common to create a class to contain all of your
    5. //extension methods. This class must be static.
    6. public static class ExtensionMethods
    7. {
    8.     //Even though they are used like normal methods, extension
    9.     //methods must be declared static. Notice that the first
    10.     //parameter has the 'this' keyword followed by a Transform
    11.     //variable. This variable denotes which class the extension
    12.     //method becomes a part of.
    13.     public static void ResetTransformation(this Transform trans)
    14.     {
    15.         trans.position = Vector3.zero;
    16.         trans.localRotation = Quaternion.identity;
    17.         trans.localScale = new Vector3(1, 1, 1);
    18.     }
    19. }
    The trick is to pay attention to the part (this Transform trans). That is what makes it available to Transforms (and the .transform reference by extended extension).
     
  3. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Hello this is a c# question: i want to do:


    mydictionary[key1][key2][key3] = something

    right now i have to add two lines more which i want to avoid somehow:

    mydictionary.Add(key1, new something)

    mydictionary[key1].Add(key2, new something)

    mydictionary[key1][key2][key3] = something
     
  4. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    344
    That is covered in the original link

    Code (CSharp):
    1. var data = new Dictionary<string, new Dictionary<string, new Dictionary<string, int>>>();
    2.  
    3. data["bob"]["jim"]["mark"] = 20;
    4. Debug.Log(name+"::Dictionary result: "+ data["bob"]["jim"]["mark"];
    Or create an extension method for Dictionary to do that first line for you as a custom type.
     
  5. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    i have right now a construction like this i did use it quite extensive already and don't want to have to edit my file too much::


    public class d1 : Dictionary<string, string> {
    }
    public class d2 : Dictionary<string, d1> {
    }
    public class d3 : Dictionary<string, d2> {
    }

    I know it should be pretty easy but i am just not savy enough with classes /custom types to figure out how.
     
  6. NoBrainer-David

    NoBrainer-David

    Joined:
    Jan 5, 2014
    Posts:
    34
    I would strongly advise the adaption of your code to create and access the nested dictionaries to the way it is done in Kerm_Ed's post.

    Since you are storing strings, in your case the code would look like this:

    Code (csharp):
    1.  
    2. // Nested dictionary instantiation
    3. Dictionary mydictionary = new Dictionary<string, new Dictionary<string, new Dictionary<string, string>>>();
    4.  
    5. // Nested dictionary access
    6. string key1;
    7. string key2;
    8. string key3;
    9. string newValue;
    10.  
    11. mydictionary[key1][key2][key3] = newValue;
     
    lloydsummers likes this.
  7. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Code (CSharp):
    1. public Dictionary<string,  Dictionary<string,  Dictionary<string, string>>> globaldict = new Dictionary<string, new  Dictionary<string, new  Dictionary<string, string>>>();
    The Line gives me syntax errors "> expected" on the second and third "new". I dont think it can be done this way.
     
    Last edited: Aug 12, 2017
  8. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Can someone please tell me why and how to work around this problem.
     
  9. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    You can't pass an instance as a generic type parameter. That would make no sense.

    This should create the new dictionary.
    Code (csharp):
    1.  
    2. public Dictionary<string,  Dictionary<string,  Dictionary<string, string>>> globaldict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
    3.  
    Here is a nested dictionary with an example of how extension methods can be used to simplify adding new items. Similar extension methods could be made for removing or finding items.
    Code (csharp):
    1.  
    2.  
    3. public class DictionaryTest : MonoBehaviour {
    4.  
    5.     public Dictionary<string, Dictionary<string, Dictionary<string, string>>> dictionary
    6.         = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
    7.  
    8.     void Awake()
    9.     {
    10.         dictionary.Add("A", "B", "C", "Value");
    11.  
    12.         Debug.Log(dictionary["A"]["B"]["C"]);
    13.     }
    14. }
    15.  
    16. public static class DictionaryExtensions
    17. {  
    18.     public static void Add(
    19.         this Dictionary<string, Dictionary<string, Dictionary<string, string>>> dictionary,
    20.         string key1,
    21.         string key2,
    22.         string key3,
    23.         string value
    24.         )
    25.     {
    26.         Dictionary<string, Dictionary<string, string>> nested;
    27.         if (dictionary.TryGetValue(key1, out nested))
    28.             nested.Add(key2, key3, value);      
    29.         else
    30.         {
    31.             nested = new Dictionary<string, Dictionary<string, string>>();
    32.             nested.Add(key2, key3, value);
    33.             dictionary.Add(key1, nested);
    34.         }
    35.     }
    36.  
    37.     public static void Add(
    38.         this Dictionary<string, Dictionary<string, string>> dictionary,
    39.         string key1,
    40.         string key2,
    41.         string value
    42.         )
    43.     {
    44.         Dictionary<string, string> nested;
    45.         if (dictionary.TryGetValue(key1, out nested))
    46.             nested.Add(key2, value);
    47.         else
    48.         {
    49.             nested = new Dictionary<string, string>();
    50.             nested.Add(key2, value);
    51.             dictionary.Add(key1, nested);
    52.         }
    53.     }
    54. }
    55.