简体   繁体   English

C#:在(哈希表)中存储对象的实例

[英]C#: Storing Instance of Objects in (Hashtable)

Hi I tried filling a Hashtable in the following way: 嗨,我尝试通过以下方式填充Hashtable:

      ResearchCourse resCourse= new ResearchCourse();//Class Instance
      resCourse.CID="RC1000";
      resCourse.CName="Rocket Science";

      TaughtCourse tauCourse= new TaughtCourse();//Class Instance
      tauCourse.CID="TC1000";
      tauCourse.CName="Marketing";

      Hashtable catalog = new Hashtable();

        catalog.Add("1", "resCourse.CID");
        catalog.Add("2", "tauCourse.CID");


        foreach (DictionaryEntry de in catalog)
        {
            Console.WriteLine("{0}, {1}", de.Key, de.Value);
        }

Output Result to Console was: 输出到控制台的结果是:

1, resCourse.CID 1,resCourse.CID
2, tauCourse.CID 2,tauCourse.CID

Expected Result to be: 预期结果为:

1, RC1000 1,RC1000
2, TC2000 2,TC2000

What am I misunderstanding about Hashtables? 我对哈希表有什么误解?
What is an easy way for the Hashtable to store the class instance and its values? Hashtable存储类实例及其值的简单方法是什么?

HashTable just maps keys to values. HashTable只是将键映射到值。 You map the string "1" to the string "resCourse.CID" so the results you get are completely normal. 您将字符串"1"映射到字符串"resCourse.CID"因此得到的结果是完全正常的。 Do it like this way: 像这样做:

catalog.Add("1", resCourse.CID);
catalog.Add("2", tauCourse.CID);

Now you will map the real values you need, not the string "resCourse.CID" . 现在,您将映射所需的实际值,而不是字符串"resCourse.CID"

You're adding explicit strings to the hash table instead of actual IDs. 您正在向哈希表添加显式字符串,而不是实际ID。

Replace the two catalog.Add lines with: 替换两个catalog.Add

catalog.Add("1", resCourse.CID);
catalog.Add("2", tauCourse.CID);

No quotation marks around the values. 值周围没有引号。

Remove the quotes around resCourse.CID and tauCourse.CID in your catalog.Add statements. 在您的catalog.Add删除resCourse.CIDtauCourse.CID周围的引号。 resCourse.CID语句。 You're adding the literal strings, not the value of the properties. 您要添加文字字符串,而不是属性的值。

I'm assuming that you'll actually want to store the entire objects: 我假设您实际上要存储整个对象:

catalog.Add("1", resCourse);
catalog.Add("2", tauCourse);

Of course, once you go there you're going to have to have a common base class (or interface) for your two courses to derive from in order to access the values: 当然,一旦您去那里,您将必须有一个公共基类(或接口)供您的两个课程派生,以便访问这些值:

public abstract class CourseBase {
   public string CID { get; set; }
   public string CName{ get; set; }
}

public class ResearchCourse : CourseBase { }
public class TaughtCourse : CourseBase { }

Then you can store/access like this: 然后您可以像这样存储/访问:

Hashtable catalog = new Hashtable();
catalog.Add("1", resCourse);
catalog.Add("2", tauCourse);

foreach (DictionaryEntry de in catalog) 
{
   Console.WriteLine("{0}, {1}", de.Key, ((CourseBase)de.Value).CID);
}

Or better yet, use a generic Dictionary: 或更妙的是,使用通用词典:

    var resCourse = new ResearchCourse() { CID = "RC1000", CName = "Rocket Science" };
    var tauCourse = new ResearchCourse() { CID = "TC1000", CName = "Marketing" };

    var catalog = new Dictionary<string, CourseBase>();
    catalog.Add("1", resCourse);
    catalog.Add("2", tauCourse);

    foreach (string key in catalog.Keys)
    {
        Console.WriteLine("{0}, {1}", key, catalog[key].CID);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM