繁体   English   中英

如何在程序中进一步使用switch语句内分配的变量?

[英]How to use a variable assigned inside a switch statement further on in the program?

我正在尝试根据当前场景的索引(在Unity术语中)读取不同的文本文件。 因此,我决定使用如下switch语句:

void MyReadString()
{
    Scene currentScene = SceneManager.GetActiveScene(); // Unity command to get the current scene info
    int buildIndex = currentScene.buildIndex; // Unity command to get the current scene number
    string path = string.Empty; // Create an empty string variable to hold the path information of text files
    switch (buildIndex)
    {
        case 0:
            string path = "Assets/Scripts/some.txt"; //It says here that the variable path is assigned but never used!
            break;
        case 1:
            string path = "Assets/Scripts/another.txt"; //Same here - assigned but never used. 
            break;
        // and many other cases - atleast 6 more
    } 
    StreamReader reader = new StreamReader(path); // To read the text file
    // And further string operations here - such as using a delimiter, finding the number of values in the text etc
}

如果我将这一行注释掉:

string path = string.Empty;

然后,

StreamReader reader = new StreamReader(path); // says here the name "path" does not exist in the current context.

我知道这与switch语句的范围有关。 但是,将开关外部的字符串变量声明为“空”是行不通的。 请让我知道是否可以在Switch语句中分配字符串值并稍后使用该值。 或者,如果不可能,请向我建议解决方法。

您遇到了问题,因为您在switch语句中再次重新声明了path变量。 仅在switch语句外部声明一次,然后在switch语句中对其进行分配。

void MyReadString()
{
    Scene currentScene = SceneManager.GetActiveScene(); // Unity command to get the current scene info
    int buildIndex = currentScene.buildIndex; // Unity command to get the current scene number
    string path = null;
    // Create an empty string variable to hold the path information of text files
    switch (buildIndex)
    {
        case 0:
            path = "Assets/Scripts/some.txt"; //It says here that the variable path is assigned but never used!
            break;
        case 1:
            path = "Assets/Scripts/another.txt"; //Same here - assigned but never used. 
            break;
            // and many other cases - atleast 6 more
    }
    StreamReader reader = new StreamReader(path); // To read the text file                                               // And further string operations here - such as using a delimiter, finding the number of values in the text etc
}

无关,但请注意,这不是在Unity中读取文件的方式。构建项目时,该代码将失败。 使用资源API或使用Assetbundles

您正在尝试重新声明一个变量-但实际上您只想向在此声明的现有变量分配一个新变量:

// This line is still needed, and must be before the switch statement.
// Ideally, remove the initialization - your switch statement should cover
// all cases, I expect.
string path = string.Empty;

所以这:

case 0:
    string path = "Assets/Scripts/some.txt"; //It says here that the variable path is assigned but never used!
    break;
case 1:
    string path = "Assets/Scripts/another.txt"; //Same here - assigned but never used. 
    break;

应该:

case 0:
    path = "Assets/Scripts/some.txt";
    break;
case 1:
    path = "Assets/Scripts/another.txt";
    break;

IMO会比较干净,要么将此逻辑放在单独的方法中,要么仅具有数组或Dictionary<int, string> 例如:

string path = GetScriptPath(buildIndex);

或(其scriptPathsstring[]Dictionary<int, string>

string path = scriptPaths[buildIndex];

您对范围位是正确的。 在switch语句中声明的变量路径使其只能从该语句中访问。 为了使您可以从语句外部访问变量,必须在更大的范围内声明它。

如果要访问该变量:

  • 仅在该函数中 ,在void MyReadString()中但在switch语句之外声明它
  • 类中的其他地方,在类结构中声明它(在MyReadString()函数之外),如果您以后想在其他函数中使用该路径,则可以使用它

在当前形式下,每次switch语句遇到大小写时,您都在创建一个新字符串,通过移动声明,您将只分配一个预先声明的字符串。

暂无
暂无

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

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