【问题标题】:Validating value of a Certain key before adding it to a c# dictionary在将某个键添加到 c# 字典之前验证某个键的值
【发布时间】:2020-04-24 12:17:26
【问题描述】:

我正在尝试以这样一种方式实现一个字典,它只会在 新值 大于 当前存在的值 时将某个值添加到键中>

所以基本场景是:

  1. 如果密钥不可用,它将创建一个新密钥和一个值
  2. 如果键可用,它将检查当前值是否大于已分配给该键的值,并且仅在大于时更新。

在我解释我在这里面临的问题之前是我的代码

static IDictionary<string, string> versionStack = new Dictionary<string, string>();
foreach(var item in RequiredApps) 
{
    nameOfApp = item.Key;
    minimumVersionOfApp = item.Value.minVersion;

    if (versionStack.TryGetValue(nameOfApp, out minimumVersionOfApp)) 
    {
         if (Convert.ToInt32(minimumVersionOfApp) >= Convert.ToInt32(item.Value.minVersion)) 
            minimumVersionOfApp = item.Value.minVersion;
    }

    versionStack[nameOfApp] = minimumVersionOfApp;
}

注意:请不要担心for 循环,因为它工作正常并且在那里没有问题。只想显示给我问题的特定代码

现在我已经能够将功能实现到一定程度,但问题是当 TryGetValue 执行时,它会将我的所有值都变为 null。

我正在使用TryGetValue 来查看键是否有值,如果有则检索它。

我现在被困在这里,如果有人能帮助我告诉我我做错了什么,我将不胜感激。

编辑:-

鉴于我面临的问题非常不清楚,正如 Rufus L 所建议的那样,我正在添加一个示例虚拟应用程序,其中正是我面临的问题

希望这有助于消除任何困惑:)

class Program
    {

        static IDictionary<string, string> versionStack = new Dictionary<string, string>();

        static string appName;
        static string minVersion;
        static void Main(string[] args)
        {
            AddOnce();
            AddTwice();

        }

        public static void AddOnce()
        {
            appName = "app01";
            minVersion = "1.0";

            versionStack.Add(appName, minVersion);
        }

        public static void AddTwice()
        {
            string existingValue;

            appName = "app01";
            minVersion = "3.0";

            if (!versionStack.TryGetValue("app01", out existingValue) || Convert.ToInt32(existingValue) < Convert.ToInt32(minVersion))
            {
                versionStack[appName] = minVersion;
            }
        }

    }

【问题讨论】:

  • 您是否调试并验证字典dependentModuleStack 中存在密钥?此外,您提到您检查 Dictionary 中的值并在它与条件匹配时更新它,但是在上面给出的代码中,您正在从一个字典中读取并更新另一个?这是期望的行为吗?
  • 为什么要使用string 值来存储int?看来您应该将其定义为:static IDictionary&lt;string, int&gt; versionStack = new Dictionary&lt;string, int&gt;();
  • 你首先定义了versionStack,然后你从不使用它。然后在您的代码中引用RequiredAppsdependentModuleStack,它们没有定义,但它们显然不使用string 值类型(它没有minVersion 属性)。请发布可以复制/粘贴的示例代码(即所有变量都已定义)。
  • @AnuViswan 我在格式化问题时犯了一个错误,对此我深表歉意,请您看一下已编辑的问题 :)

标签: c# .net string dictionary generics


【解决方案1】:

根据您显示的代码,问题似乎是您使用minimumVersionOfApp 作为out 参数到TryGetValue 方法,如果@987654325 将设置为null @ 失败了,但无论如何我们都在使用它。

请记住,带有任何out 参数的合约是方法必须在返回之前为其赋值。通常(特别是在这种情况下),如果TryGetValue 返回false,则out 参数设置为类型的默认值,即null 上课。

然后,在第一个 if 语句之后,即使 TryGetValue 返回 false,您也将值分配给键,这可能就是您获得 null 值的原因。

如果TryGetValue 成功,则仅使用out 参数。

以下是执行此操作的示例代码:

foreach(var item in RequiredApps) 
{
    var appName = item.Key;
    string existingValue;    // Note that this should be the same type as item.Value

    // If the key doesn't exist (the first condition), 
    // or it does exist and our value is greater than the existing one (second condition)
    // then update the key with our new value
    if (!dependentModuleStack.TryGetValue(appName, out existingValue) ||
        Convert.ToInt32(existingValue) < Convert.ToInt32(item.Value.minVersion)) 
    {        
        versionStack[appName] = item.Value.minVersion;
    }
}

【讨论】:

  • 嗨 Rufus 谢谢你的解释和你的回答,但我仍然无法解决问题我用一个简单的虚拟应用程序编辑了这个问题,我想要实现的目标和你建议的改变希望你不介意看看它
【解决方案2】:

您的问题似乎不是Dictionary.TryGetValue,而是Convert.ToInt32

版本号是双精度数,而不是整数。你应该使用Convert.ToDouble 而不是Convert.ToInt32

 if (!versionStack.TryGetValue("app01", out existingValue) || Convert.ToDouble(existingValue) < Convert.ToDouble(minVersion))
 {
     versionStack[appName] = minVersion;
 }

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2020-09-14
    • 1970-01-01
    • 2019-11-17
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多