【问题标题】:C# - Use of unassigned local variable 'stringnamehere'C# - 使用未分配的局部变量'stringnamehere'
【发布时间】:2014-08-14 14:02:38
【问题描述】:

这是我的代码:

        private void startButton_Click(object sender, EventArgs e)
    {
        DriveInfo[] drives = DriveInfo.GetDrives();
        string drivenames;
        for (int i = 0; i < drives.Count(); i++)
        drivenames = drives[i].Name;
        MessageBox.Show(drivenames); // --> For debug purposes only
        strCmdText = "del /f /s " + drivenames + "*.sfk";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    }

我正在尝试将计算机上的驱动器号转换为字符串,但是,当我尝试使用该字符串时,它显示使用未分配的局部变量“驱动器名”。这里有什么问题?

【问题讨论】:

  • 这就是为什么你总是使用{}括号。
  • 完全同意@JeroenVannevel - 在我看来,摆脱它们永远不值得,即使它只是简单的if下的单行声明
  • 我认为他也想要循环中的消息框,所以他肯定需要括号。
  • @DeeMac 同样的想法。我已经编码多年,从来没有写过没有{}的单个块@
  • 我不同意。单语句 ifs 和 while 循环完全没问题。只是不要将语句放在下一行,或者使用可以修复缩进错误的编码工具。

标签: c#


【解决方案1】:

如果drives.Count() 为0,则不会进入循环。然后变量仍然未分配。这就是编译器不喜欢MessageBox.Show(drivenames) 的原因。

您可以指定""null

string drivenames = "";
// ...

编译器只是想帮你避免错误。

如果您想为每个驱动器显示MessageBox,则必须将其移动到循环中,如David 所示。你也可以使用这个不需要显式循环的 LINQ 查询:

private void startButton_Click(object sender, EventArgs e)
{
    DriveInfo[] drives = DriveInfo.GetDrives();
    string drivenames = string.Join(Environment.NewLine, drives.Select(d => d.Name));
    MessageBox.Show(drivenames); 
}

【讨论】:

  • 如果我将 'string drivenames' 编辑为 'string drivenames = null;',MessageBox 会显示 G:\ ,但实际上我什至没有名为 G:\ 的驱动器。跨度>
  • @user3669879:你用过调试器吗?
  • 当我必须在命令提示符中输入驱动器号以获取命令时,情况如何?例如,我想运行“del /f /s DRIVELETTERSHERE*.filetypehere”?
【解决方案2】:

我假设您想为每个驱动器显示一个消息框。如果是这样的话,你想要更多这样的东西:

private void startButton_Click(object sender, EventArgs e)
{
    DriveInfo[] drives = DriveInfo.GetDrives();
    string drivenames = string.Empty;
    for (int i = 0; i < drives.Count(); i++)
    {
        drivenames = drives[i].Name;
        MessageBox.Show(drivenames); // --> For debug purposes only
    }
}

编辑基于 cmets,这可以大大简化为:

private void startButton_Click(object sender, EventArgs e)
{
    foreach (var drive in DriveInfo.GetDrives())
    {
        // todo: write some code here
        MessageBox.Show(drive);
    }
}

这不是更容易阅读吗?

【讨论】:

  • 我个人更喜欢这个,使用一个空字符串,而不是分配一个空值。它将防止与空值相关的任何意外行为。
  • 当我必须在命令提示符中输入驱动器号以获取命令时,情况如何?例如,我想运行“del /f /s DRIVELETTERSHERE*.filetypehere”?
  • @Tyress 首先,变量不需要存在于循环范围之外。当变量没有有效值而不是有一些“首选”默认值时,它根本不应该存在。适当地确定变量的范围,或者完全删除它,要安全得多。
  • 另外,尽可能使用foreach。这里不需要for 循环。使用Array.Length 而不是Count() 扩展方法。
  • @Tyress:出乎意料的是,在循环之后它仍然是null?那么很高兴知道,不是吗?空字符串可能会静默失败,而 null 值可能会大声失败(异常),这是一件好事。 (在这种情况下MessageBox.Show 不会抛出异常)
【解决方案3】:

您的代码无法编译,您可以按照 Tim 的建议通过分配 string drivenames = null 来编译它。

然后,您的程序将在每次迭代时显示 last 驱动器名称,先前的值将被先前的值覆盖,如果您没有任何驱动器,则 null 将传递给 @987654322 @ 最多会显示一个空的文本框,最坏的情况会引发运行时异常。

假设您只希望显示一个消息框,我建议您对代码进行以下更改:

DriveInfo[] drives = DriveInfo.GetDrives();

IEnumerable<string> driveNames = drives.Select(drive => drive.Name);

string output = String.Join(", ", driveNames);

MessageBox.Show(output); // will display "C, D, E" or similar

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2014-05-13
    • 2015-03-18
    相关资源
    最近更新 更多