用于未分配的变量错误

时间:2014-12-09 03:07:00

标签: c#

我一直收到未分配变量的错误,低于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();
}

2 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分:2)

您只是为try/catch块中的那些变量分配值,并且编译器不知道try块中的代码是否会在您到达之前成功分配值稍后的代码部分,您将这些值相乘。

定义它们时为它们指定默认值。

decimal UnitPrice = 0, TotalQuantity = 0;

这至少可以让你的程序编译。如果try块由于某种意外原因而失败,则您将结束两个零并且您的程序将继续前进。根据您的情况,这可能是也可能不可取。