【问题标题】:How to get dictionary values from Outside the dictionary/ or class [closed]如何从字典/或类之外获取字典值[关闭]
【发布时间】:2014-11-22 17:28:29
【问题描述】:

假设我在 1 个类中有一个字典函数:

public static class Class1
{
    public static void StatDict(int EventNumber, string EventCode)
    {
        Dictionary<int, string> Dict1 = new Dictionary<int, sting>();

        Dictionary.Add(EventNumber, EventCode);
    }
}

输入示例:EventNumber = '4', EventCode = '4TUI'// 这是从另一个类 Class2 提供的

我想比较说,Int i = 4,在类 2 中或字典的键 4 的函数中,以便我可以提取键 4 的值。(即“4TUI”)。

public static class Class2
{
    public void CompareIntToDictionary()
    {
        Int Compare= 4;

        if (Compare == Dictionary(value);????????????????????? // *This part i need help with*
        {
            Do something!!!
        }
    }
}

谁能告诉我如何引用存在于 1 个类中的字典以提取与键关联的值,如果它与我在其他地方(即另一个类)定义的整数匹配?任何帮助将不胜感激,谢谢。

编辑:感谢这个,似乎它确实可以解决问题,除了以下代码确实返回 false,即使我知道密钥在那里,它也会跳过这个(因此为 false):

int Compare = 4;
if (Class1.Dict1.ContainsKey(Compare))
{
    var eventCodecheck = Class1.Dict1[Compare];
    Debug.WriteLine("eventcode = " + eventCodecheck);
    // Do something!!!
}

但现在我面临的问题是我需要重写 GetHashCode 和 Equals 以便使用字典比较来自 2 个不同对象的键。

感谢有用的答案,这是我使用的代码,但我不太确定如何覆盖 GetHascode 和 Equals:

    public static class Class1
    {
        public static Dictionary<int, string> Dict1 { get; set; }
        public static void StatDict(int EventNumber, string EventCode)
        {
            Dict1 = new Dictionary<int, string>();
            Dict1.Add(EventNumber, EventCode);
            Debug.WriteLine("EventNumber = " + EventNumber + " EventCode = " +       EventCode);
    }

}

public static class Class2
{
    public static void CompareIntToDictionary()
    { 
        int Compare = 4;
        if (Class1.Dict1.ContainsKey(Compare))
        {
            var eventCodecheck = Class1.Dict1[Compare];
            Debug.WriteLine("eventcode = " + eventCodecheck);

            // Do something!!!
        }
    }
}

我只是在学习这些东西。

【问题讨论】:

  • 那么你的问题是什么?你的CompareIntToDictionary 方法不起作用吗?
  • 基本上,是的,它跳过了这个检查,即使我知道有一个键 '4' 和一个关联的值。所以,它看起来像 int compare = 4;与 Dict1 中 4 的键不同。似乎并非所有 Int 的创建都是一样的,因此哈希码....
  • int 将其值作为哈希码返回,因此您不需要覆盖 GetHashCode 方法(实际上您不能为 int 执行此操作)。您确定先调用StatDict 方法,然后再调用CompareIntToDictionary?此外,每次调用StatDict 时,它都会创建一个新的空字典。您需要将Dict1 = new Dictionary&lt;int, string&gt;(); 移动到另一个方法并在其他方法之前调用它一次,以确保字典只创建一次。

标签: c# class object dictionary


【解决方案1】:

公开您在Class1 中创建的字典并在Class2 中使用它:

public static class Class1
{
    public static Dictionary<int, string> Dict1 { get; set; }

    public static void StatDict(int EventNumber, string EventCode)
    {
        Dict1 = new Dictionary<int, sting>();

        Dict1.Add(EventNumber, EventCode);
    }
}

public static class Class2
{
    public void CompareIntToDictionary()
    {
        int Compare = 4;

        if (Class1.Dict1.ContainsKey(Compare))
        {
            var eventCode = Class1.Dict1[Compare];
            // Do something!!!
        }
    }
}

并且更好地使用对象而不是静态。

【讨论】:

  • “男孩”有点失礼
【解决方案2】:

感谢大家的意见,如果你能帮助我,你可以随便给我打电话!

最后,我切换到修复了问题的排序列表,但发布的代码帮助我指明了正确的方向,非常感谢。

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多