【问题标题】:Used of unassigned variable error [duplicate]使用未分配的变量错误[重复]
【发布时间】:2014-12-09 03:06:59
【问题描述】:

我不断收到以下未分配变量的错误,即TotalAmount = TotalQuantity * UnitPrice。请指教。

private void button1_Click(object sender, EventArgs e)
{
    string client;
    string date;
    string telNumber;

    const decimal TaxRate = 0.43M;

    uint Quantity, Amount, Total;

    decimal TotalOrder, TaxAmount, SalesTotal, TotalQuantity;
    decimal UnitPrice, AmountTended, Difference, TotalAmount;


    Console.SetCursorPosition(10, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(10, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(58, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    date = Console.ReadLine();

    Console.SetCursorPosition(67, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //TotalQuantity
    bool test = false;
    do
    {
        try
        {
            Console.SetCursorPosition(2, 12);
            Console.Write(" ");

            Console.SetCursorPosition(2, 12);
            TotalQuantity = Convert.ToDecimal(Console.ReadLine());
            test = false;
        }
        catch
        {
            test = true;
        }
    } while (test);

    //Item Description

    Console.SetCursorPosition(18, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //Unit Price

    bool test2 = false;
    do
    {
        try
        {
            Console.SetCursorPosition(47, 12);
            Console.Write(" ");

            Console.SetCursorPosition(47, 12);
            UnitPrice = Convert.ToDecimal(Console.ReadLine());
            test2 = false;
        }
        catch
        {
            test2 = true;
        }
    } while (test2);

    //Computations

    //TotalAmount
    **TotalAmount = TotalQuantity*UnitPrice;
    **

    Console.SetCursorPosition(65, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.Write("P ");
    Console.WriteLine(TotalQuantity*UnitPrice);
    Console.ReadLine();
}

【问题讨论】:

  • 您是否在 TotalAmount 周围使用 ** 和 ** 进行编译?
  • 我不同意这是重复的。是的,这是相同的错误消息。但是另一个问题涉及一个 真正 未分配的变量,而这里只是编译器无法识别分配。此外,另一个问题中唯一合适的答案是初始化变量,而这里这些变量实际上不需要在实际分配之前进行预初始化。将此问题视为另一个问题的重复是误导性的。
  • 我在发布这个之前确实检查了答案和问题,感谢格兰特先生,我得到了答案,也谢谢彼得爵士

标签: c#


【解决方案1】:

您只是为try/catch 块中的这些变量赋值,编译器不知道try 块中的代码是否会在您稍后进入代码部分之前成功地赋值,您将值相乘的位置。

在定义它们时为它们分配一个默认值。

decimal UnitPrice = 0, TotalQuantity = 0;

这至少可以编译您的程序。如果try 块由于某种意外原因确实失败了,您将结束乘以两个零并且您的程序将继续前进。这可能是可取的,也可能不是可取的,具体取决于您的情况。

【讨论】:

  • 我在 c# 中只有一周的时间,我会尝试你的建议并将它放在 try catch 之前,对吗?谢谢楼主
  • 成功了,先生!!!非常感谢
  • 恕我直言,分配默认值应该是处理此错误消息的最后手段。通常可以对代码进行结构化,以便编译器能够正确理解实际的执行流程,并且这样做几乎总是可以使代码更具表现力和可理解性。这是双赢的。 :)
  • 我会研究它是如何工作的,先生,再次感谢,我是编程新手,我正在练习控制台应用程序,谢谢先生
【解决方案2】:

根据 C# 明确分配规则,TotalQuantityUnitPrice 都不是明确分配的。作为人类,您可以查看代码并知道它们肯定是分配的。但是编译器不会分析变量testtest2 的使用和赋值,因为它们与循环流有关。所以你会得到一个编译时错误。

解决这个问题的正确方法是创建一个帮助方法来处理这些项目的输入。这不仅可以确保您没有复制/粘贴的代码,还可以使用方法的返回值分配给每个变量,确保编译器可以判断该变量已被确定分配。

例如:

private void button1_Click(object sender, EventArgs e)
{
    string client;
    string date;
    string telNumber;

    const decimal TaxRate = 0.43M;

    uint Quantity, Amount, Total;

    decimal TotalOrder, TaxAmount, SalesTotal, TotalQuantity;
    decimal UnitPrice, AmountTended, Difference, TotalAmount;


    Console.SetCursorPosition(10, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(10, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(58, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    date = Console.ReadLine();

    Console.SetCursorPosition(67, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //TotalQuantity
    TotalQuantity = PromptDecimal(2);

    //Item Description

    Console.SetCursorPosition(18, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //Unit Price    
    UnitPrice = PromptDecimal(47);

    //Computations

    //TotalAmount
    **TotalAmount = TotalQuantity*UnitPrice;
    **

    Console.SetCursorPosition(65, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.Write("P ");
    Console.WriteLine(TotalQuantity*UnitPrice);
    Console.ReadLine();
}

地点:

decimal PromptDecimal(int promptLine)
{
    while (true)
    {
        Console.SetCursorPosition(promptLine, 12);
        Console.Write(" ");

        Console.SetCursorPosition(promptLine, 12);

        decimal result;

        if (decimal.TryParse(Console.ReadLine(), out result))
        {
            return result;
        }
    }
}

【讨论】:

  • 这个我也想试试学习,谢谢楼主
  • promptline 是什么意思,先生?
  • @Puchicha:你的意思是PromptDecimal()?这是您将添加和使用的新方法,而不是您现在拥有的循环。它与您的代码之前所做的相同,除了在可重用方法中,它返回输入的decimal 值。我将编辑答案,以便提供有关我建议的更改的更多上下文。
  • 这意味着先生,如果我使用我制作的第一个代码然后添加下一个代码,它也会以简单的方式生成相同的输出。谢谢先生。
  • @Puchicha:您将更改原始代码,使其看起来像我在答案中显示的方式(即删除循环,并替换为对这个新方法的调用),然后您将添加新方法本身。
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 2016-10-04
相关资源
最近更新 更多