none
How to pass key & value to the parent class, Dictionary<string, List<T>>, from inherited class RRS feed

  • Question

  • I made a class(MessageListDict) which is inherited from Dictionary<string, List<T>> like the following.

    public class MessageListDict : Dictionary<string, List<Message>>

    This class is a Dictionary of List.

    However I have no idea how to pass key(string) and value(List<T>) to the parent class(Dictionary<string, List<T>>)?


    • Edited by Jeff0803 Saturday, January 4, 2020 6:46 PM
    Saturday, January 4, 2020 6:43 PM

Answers

  • Hi Jeff,

    To elaborate a bit more on the reply given by EckiS, you could additionally have a second constructor to which you could pass a key and a List.

    public class MessageListDict : Dictionary<string, List<Message>>
    {
        public MessageListDict() : base()
        {
        }
        public MessageListDict(string key, List<Message> list)
        {
            this.Add(key, list);
        }
    }
    

    And use either constructor like this:

    // EckiS example, using the standard/base constructor
    var dict = new MessageListDict();
    dict.Add("test", new List<Message>());
    
    // My example, using the second constructor
    var dict2 = new MessageListDict("test2", new List<Message>());
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, January 6, 2020 2:17 AM
    Saturday, January 4, 2020 9:06 PM
    Moderator

All replies

  • what do you mean by "pass to parent class"?
    as your class MessageListDict is now, you use it exactly like the base class.
    var dict = new MessageListDict();
    dict.Add("test", new List<Message>());
    Saturday, January 4, 2020 7:20 PM
  • Hello,

    See if this will help where a base class is used.

    public class MessageListDict : BaseClass
    {
        public MessageListDict()
        {
            Messages = new Dictionary<string, List<Message>>
            {
                {"A", new List<Message>() {new Message() {Item = "A1"},new Message() {Item = "A2"},new Message() {Item = "A3"}}},
                {"B", new List<Message>() {new Message() {Item = "B1"},new Message() {Item = "B2"},new Message() {Item = "B3"}}},
                {"C", new List<Message>() {new Message() {Item = "C1"},new Message() {Item = "C2"},new Message() {Item = "C3"}}}
            };
        }
    
        public List<Message> Message(string key)
        {
            if (Messages.ContainsKey(key))
            {
                return Messages[key];
            }
            else
            {
                return null;
            }
        }
    }
    
    public class BaseClass
    {
        public Dictionary<string, List<Message>> Messages { get; set; }
    
        public BaseClass()
        {
            Messages = new Dictionary<string, List<Message>>();
        }
    }
    
    public class Message
    {
        public string Item { get; set; }
    }

    Test

    class Program
    {
        static void Main(string[] args)
        {
            var key = "B";
            var messages = new MessageListDict();
            List<Message> messageList = messages.Message(key);
            if (messageList != null)
            {
                messageList.ForEach(m => Console.WriteLine(m.Item));
            }
            else
            {
                Console.WriteLine($"'{key}' not found");
            }
    
            Console.ReadLine();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 4, 2020 8:45 PM
    Moderator
  • Hi Jeff,

    To elaborate a bit more on the reply given by EckiS, you could additionally have a second constructor to which you could pass a key and a List.

    public class MessageListDict : Dictionary<string, List<Message>>
    {
        public MessageListDict() : base()
        {
        }
        public MessageListDict(string key, List<Message> list)
        {
            this.Add(key, list);
        }
    }
    

    And use either constructor like this:

    // EckiS example, using the standard/base constructor
    var dict = new MessageListDict();
    dict.Add("test", new List<Message>());
    
    // My example, using the second constructor
    var dict2 = new MessageListDict("test2", new List<Message>());
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, January 6, 2020 2:17 AM
    Saturday, January 4, 2020 9:06 PM
    Moderator
  • In some cases, you can use ‘base’. For example:

    class MessageListDict : Dictionary<string, List<Message>>
    {
       public new void Add( string name, List<Message> messages )
       {
          // do something:
          . . .
          // call the parent class:
          base.Add( name, messages );
       }
    }

    If you write ‘Add(name, messages)’ instead of ‘base.Add(name, messages)’, this will call the current Add function, producing infinite recursion.




    • Edited by Viorel_MVP Sunday, January 5, 2020 10:55 AM
    Sunday, January 5, 2020 10:49 AM