Microsoft 开发人员网络 > 论坛主页 > Visual C# General > Using properties with dictionary
提出问题提出问题
 

已答复Using properties with dictionary

  • 2009年11月7日 19:39mupersan82 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    Hi there,

    I'm trying to retrieve an objects content based on a key in a dictionary, but don't know how to use the properties in combination with the dictionary. First of all, I create the dictionary and add 5 keys. Each keys value is an object of another class I've created: the Person class. Two parametres (username and password respectively) are passed and stored using properties. How do I retrieve one object and its content based on its key?

    Program class:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace Lagerprojekt_final
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("kunde1", new Person("kunde1", "pass1"));
                d.Add("kunde2", new Person("kunde2", "pass2"));
                d.Add("kunde3", new Person("kunde3", "pass3"));
                d.Add("kunde4", new Person("kunde4", "pass4"));
                d.Add("admin", new Person("admin", "adminpass"));
    
                Person kunde1 = new Person();
    
                // See if Dictionary contains this string
                if (d.ContainsKey("kunde1")) // True
                {
    
                    string v = Convert.ToString(d["kunde1"]);
                    Console.WriteLine("Kunde1 Password: {0}", kunde1.Password);
                }
    
                Console.ReadLine();                                        
              
            }
        }
    }
    
    

    Person class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace Lagerprojekt_final
    {
        class Person
        {
            protected string brugernavn;
            protected string password;
    
    
            //Default constructor
            public Person()
            {
            }
    
            //Overload method which takes two parameters
            public Person(string brugernavn, string password)
            {
                this.brugernavn = brugernavn;
                this.password = password;
            }
    
            public string Brugernavn
            {
                set
                { brugernavn = value; }
                get
                { return brugernavn; }
    
            }
    
            public string Password
            {
                set
                { password = value; }
                get
                { return password; }
    
            }
    
        }
    }
    
    

    Regards,
    Anders

答案

  • 2009年11月7日 21:20Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    By using Person you can directly access Person object's properties. Because by passing Person type as generic you say the dictionary that field holds Person type.

    And if you type object Dictionary does not know whats the type and it can not cast it. Since object does not have property like password if you want to use properties of person you have to cast it to person manually.

    For example

    If you use object you can add Person for the first value, and string for the second, and int for the third. That means it holds multiple types and does not know what's the type. However if you use Person, you can only add person object. So it knows the value is person and you can access it's properties.

全部回复

  • 2009年11月7日 19:41Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi,

    Declare your dictionary like below. Use Person as value type instead of object.

    That might solve your problem.

    Dictionary<string, Person> d = new Dictionary<string, Person>();

    Then you can access password by d["kunde1"].Password
  • 2009年11月7日 19:50mupersan82 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Tamer Oz,

    Thanks for your quick answer. However, I'm not sure if I understand it? Isn't it possible to do like I did with some small additions? If not, can you then elaborate a bit on your code?

    Thank you,
    Anders

  • 2009年11月7日 19:53Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复包含代码

    Hi,

    Change your code like below.

    namespace Lagerprojekt_final
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, Person> d = new Dictionary<string, Person>();//this line changed
                d.Add("kunde1", new Person("kunde1", "pass1"));
                d.Add("kunde2", new Person("kunde2", "pass2"));
                d.Add("kunde3", new Person("kunde3", "pass3"));
                d.Add("kunde4", new Person("kunde4", "pass4"));
                d.Add("admin", new Person("admin", "adminpass"));
    
                Person kunde1 = new Person();
    
                // See if Dictionary contains this string
                if (d.ContainsKey("kunde1")) // True
                {
    
                    string v = Convert.ToString(d["kunde1"]);
                    Console.WriteLine("Kunde1 Password: {0}", d["kunde1"].Password);//this line changed
                }
    
                Console.ReadLine();                                        
              
            }
        }
    }
    
    
  • 2009年11月7日 21:14mupersan82 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Works like a charm, thanks!

    But I'm not sure why it works ;-) Can you explain the difference between using the Person object as a value type instead of an object?

    Regards,
    Anders
  • 2009年11月7日 21:20Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    By using Person you can directly access Person object's properties. Because by passing Person type as generic you say the dictionary that field holds Person type.

    And if you type object Dictionary does not know whats the type and it can not cast it. Since object does not have property like password if you want to use properties of person you have to cast it to person manually.

    For example

    If you use object you can add Person for the first value, and string for the second, and int for the third. That means it holds multiple types and does not know what's the type. However if you use Person, you can only add person object. So it knows the value is person and you can access it's properties.

  • 2009年11月7日 21:30mupersan82 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Aha, the missing link for me was generic types. When would it be an advantage to use object instead of generic type?
  • 2009年11月7日 21:32Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    When if you are not sure about the type or you want to store different types. Example. If you use object you can add Person for the first value, and string for the second, and int for the third. That means it holds multiple types and does not know what's the type.
  • 2009年11月7日 21:37mupersan82 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I mean, it seems like generic types are a much better than object, so when would it be better to use object?
  • 2009年11月7日 21:46Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    This is a sample to tell when using object is better.

    I don't prefeer to use object as generic.

    The advantages and disadvantages is similar to use object intstead a type.

    Here is a sample. You can't store all these types in a generic list other than object.

     

    List<object> o = new List<object>();

    o.Add(1);

    o.Add(

    "Sample");

    o.Add(

    Image.FromFile(""));