Home > Back-end >  How to create dictionary within list
How to create dictionary within list

Time:10-19

I have a class Mobile in which my dictionary is declared. I have another class Cell through which I want to call this dictionary and add items into it. I have created a function in the Mobile class to add elements into the dictionary. My two classes are.

public class Mobile {

   public Dictionary<string, Mobile> dict;
   
   
   public Mobile(int x,int y) {
    this.x=x;
    this.y=y;
    dict = new Dictionary<string, Mobile>();
   }
   
   pulic void Add_data_to_dictionary(string a,Mobile b)
   {
   this.dict.Add(a,b);
   }
 
}

public class Cell
{
Mobile x=new Mobile():

}

I need to implement my code in Cell class. My problem is that I have to create a list of nodes of class Mobile where each node must contain a dictionary and each dictionary should have multiple key-value pairs.

Hierarchy is like this

node1->dictionary[key1,val1,key2,val2] 
node2->dictionary[key1,val1,key2,val2] 
node3->dictionary[key1,val1,key2,val2] 

can anyone help me how do I do that?

CodePudding user response:

I'm still not sure what you trying to achieve, but as you said:

I need a List of class Mobile. At each index of List, I need One Dictionary of class Mobile where each dictionary can have many entries of key,value pairs.

It could be done probably in that way:

class Program
{
    static void Main(string[] args)
    {
        // Initialize List of your Mobiles
        List<Mobile> mobiles = new List<Mobile>();

        // Fill list in some way
        for (int i = 0; i < 5; i  )
        {
            // Create new Mobile
            Mobile mobile = new Mobile();

            // Add data to its Dictionary
            mobile.AddDataToDictionary("SomeKey #"   i, mobile); // Add itself as value?!

            // Add to list of Mobiles (your nodes)
            mobiles.Add(mobile);
        }

        // Create Cell instance and pass list of Mobiles to it
        Cell cell = new Cell(mobiles);
        // Do what you want with mobiles there
        cell.DoWorkWithMobiles();

        Console.ReadKey();
    }
}


public class Mobile
{
    // Make Dictionary as public property to grant access to it for read purposes
    public Dictionary<string, Mobile> Dict { get; private set; }

    // I removed arguments x/y for this example
    public Mobile()
    {
        Dict = new Dictionary<string, Mobile>();
    }

    public void AddDataToDictionary(string key, Mobile value)
    {
        Dict.Add(key, value);
    }
}

public class Cell
{
    // To store list, which was passed to class constructor
    private List<Mobile> mobiles;

    public Cell(List<Mobile> mobiles)
    {
        this.mobiles = mobiles;
    }

    public void DoWorkWithMobiles()
    {
        // Very unclear what to do here
        for (int i = 0; i < mobiles.Count; i  )
        {
            Mobile mobile = mobiles[i];
            Mobile mobileFromDict = mobile.Dict["SomeKey #"   i];
            //or 
            mobile.Dict.TryGetValue("SomeKey #"   i, out Mobile mob);
            Mobile m = mob.Dict["SomeKey #"   i];
        }
    }
}

As you can see, an architecture and logic of your project isn't clear and example may be incorrect and stupid.

  • Related