-2

という関数がfirstRun()あり、その中に 2 つのブール値filesDeletedとが定義されていdirsDeletedます。

また、私が持っている関数内でも、if (filesDeleted == true && dirsDeleted == true) {
アプリケーションをデバッグしようとするとエラーが発生し、さまざまなソリューションを試してみましたが、まったく機能しませんでしたUse of unassigned local variable 'filesDeleted'Use of unassigned local variable 'dirsDeleted'

コードは次のとおりです。

private void firstRun(bool forceDelete) {
  string Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myLauncher");
  string[] Files = Directory.GetFiles(Path);
  string[] Dirs = Directory.GetDirectories(Path);
  bool filesDeleted; 
  bool dirsDeleted;

  if (forceDelete == true)
  {
      if (Directory.Exists(Path))
      {
          string lastFile = Files[Files.Length - 1];
          foreach (string file in Files)
          {
              if (file == lastFile)
              {
                  filesDeleted = true;
                  MessageBox.Show("test");
              }
              File.Delete(file);
          }
          string lastDir = Dirs[Dirs.Length - 1];
          foreach (string dir in Dirs)
          {
              if (dir == lastDir)
              {
                  dirsDeleted = true;
                  MessageBox.Show("test2");
              }
              Directory.Delete(dir, true);

          }
          if (filesDeleted == true && dirsDeleted == true)
          {
            //code when everything deleted
          }
      }
      else
      {
          Directory.CreateDirectory(Path);
      }
  }
4

2 に答える 2

1

あなたの

bool filesDeleted;
bool dirsDeleted;

bool filesDeleted = false;
bool dirsDeleted = false;

これらはローカル変数であり、使用する前に割り当てる必要があります。

から5.1.7 Local variables

ローカル変数は自動的に初期化されないため、デフォルト値はありません。明確な代入チェックの目的で、ローカル変数は最初は未代入と見なされます。

于 2013-08-24T13:56:54.673 に答える