来自标签的 C# StreamReader 输入文件? [英] C# StreamReader input files from labels?

查看:21
本文介绍了来自标签的 C# StreamReader 输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 ListBox 中的 StreamReader inputFile 代码,效果很好,但是,我想从 .txt<输入数据/code> 文件放入 Label 框中,这可能吗?这是我试过的代码,它给了我一个错误描述,说明

I have been using a StreamReader inputFile code from a ListBox and it works great, However, I would like to input the data from the .txt file into a Label box instead, is this possible? This is the code I tried and it gives me an error description stating

Use of unassigned local variable 'total'

private void Form1_Load(object sender, EventArgs e)
{
  try
  {
      int total = 0; 
      int highScore;
      StreamReader inputFile;
      inputFile = File.OpenText("HighScore.txt");
      while (!inputFile.EndOfStream)
      {
          highScore = int.Parse(inputFile.ReadLine());
          total += highScore;
      }
      inputFile.Close();
      highscoreLabel.Text = total.ToString("c");
  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.Message);
  }
}

推荐答案

您看到的消息(Use of unassigned local variable 'total'")与确定分配"有关,这将是以下场景:

The message you are seeing ("Use of unassigned local variable 'total'") relates to "definite assignment", which would be the scenario:

int total; // note not yet assigned a value

...

total += {whatever}

但是,在您发布的代码中,它 明确分配(初始化为零).因此,我怀疑要么错误消息被错误复制,要么代码示例不是失败案例的直接副本.

However, in the code you post, it is definitely assigned (initialized to zero). Therefore, I suspect that either the error message has been mis-copied, or the code sample is not a direct copy of the failing case.

这篇关于来自标签的 C# StreamReader 输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆