简体   繁体   English

使用未分配的局部变量错误#2

[英]Use of unassigned local variable error #2

I've looked on google and other stackoverflow Use of unassigned local variable errors and I still cannot find the answer. 我看过google和其他stackoverflow Use of unassigned local variable错误,但仍然找不到答案。 I think that maybe my error is cause be misusing the scopes of ExtractionCtrl. 我认为我的错误可能是由于滥用了ExtractionCtrl的作用域。 I tried this code to test the scope and it works. 我尝试使用此代码来测试范围,并且可以正常工作。 So I don't know where is my mistake now. 所以我现在不知道我的错误在哪里。

Testing the scope 测试范围

namespace RandomTesting
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int x = 5;

            switch (x)
            {
                case 2:
                    System.Console.WriteLine("Your # is 2");
                    break;

                case 5:
                    System.Console.WriteLine("Your # is :{0}", x);
                    x = x + 2;
                    System.Console.WriteLine("Your # is :{0}", x);
                    break;
            }

            System.Console.WriteLine("Your # is :{0}", x);
            Console.ReadLine();
        }
    }
}

PART OF THE MAIN CODE 主要代码的一部分

switch (arg)
{
    case "AR":
        ExtractionCtrl = new ARExtractionController();

        // add new mapping here
        break;

    case "ICN":
        ExtractionCtrl = new IcnExtractionController();

        // add new mapping here
        break;
}

int ticketID;
if (int.TryParse(arg, out ticketID))
{
    string returnedFilePath = ExtractionController.GetStartupPath();
    ExtractionCtrl.Extract(ticketID, returnedFilePath, AR_TEMPLATE_PATH, MAPPING_PATH);
}

Your switch to set ExtractionCtrl does not have a default-case, so it is possible that ExtractionCtrl is not initalized after the switch. 您用于设置ExtractionCtrl的开关没有默认大小写,因此在开关之后可能未初始化ExtractionCtrl。 Since you do not show the declaration of ExtractionCtrl, I'm assuming it is declared without initialization: 由于您没有显示ExtractionCtrl的声明,因此我假设它的声明没有初始化:

SomeExtractionCtrlType ExtractionCtrl;

Hence the error. 因此,错误。

You most likely need a default block in your switch . 您很可能需要在switch使用default块。

switch (arg)
{
    case "AR":
        ExtractionCtrl = new ARExtractionController();

        // add new mapping here
        break;

    case "ICN":
        ExtractionCtrl = new IcnExtractionController();

        // add new mapping here
        break;
    default:
        ExtractionCtrl = new DefaultExtractionController();
        break;
}

Or you could initialize ExtractionCtrl when you define it. 或者,您可以在定义它时初始化ExtractionCtrl I like the default option more, though. 不过,我更喜欢default选项。

In Visual Studio, this is a pretty common compile-time error, it means that the compiler thinks there's a chance the variable won't be initialized before it is called. 在Visual Studio中,这是一个非常常见的编译时错误,这意味着编译器认为该变量有可能在调用之前不会初始化。 What you should do is add a default case, as other answers say, but also initialize your variable when you declare it, either to null , or to the value you set it to in your default . 正如其他答案所说,您应该做的是添加一个default大小写,但是在声明变量时还要初始化它,可以为null ,也可以初始化为您在default设置的default Visual Studio is unfortunately very picky, and it doesn't see that all potential paths do end up granting a value. 不幸的是,Visual Studio非常挑剔,它没有看到所有潜在的路径最终都会赋予值。

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

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