1

I have the following problem: i have a string variable, which has to store a filepath. In a foreach loop i go through all files in a certain directory and im looking for the oldest, which is saved in that string variable. When the loop is finished i try to delete that file, but i get an error: Use of an unassigned local variable.

Here is the code:

DateTime min = DateTime.Now;
string[] fileNames = Directory.GetFiles(somePath);
string fileDelete;
int countFiles = fileNames.Length;
if (countfiles > 5)
{
    foreach (string someFile in fileNames)
    {
        FileInfo infoFile = new FileInfo(someFile);
        if (infoFile.CreationTime <= min)
        {
            min = infoFile.CreationTime;
            fileDelete = someFile;
        }   
    }
    File.Delete(fileDelete);
}   

it says that the string fileDelete in File.Delete(fileDelete) has no value, but the fun thin is, when i give it a value at the beginning just like that:

string fileDelete = "blabla";

it works perfectly fine. This is just a snipped of the method in case you are wondering

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
sebastian
  • 13
  • 5
  • 1
    When you do `string fileDelete = "blabla";`, the variable is no longer unassigned. Is `countFiles` bigger than `5`? Does at least one file have a creation time before `DateTime.Now`? Your code does not guarantee that `fileDelete` is assigned, so it's going to go wrong at some point. – Bart van Nierop Jan 20 '16 at 12:04
  • As others said, compiler can only work with variable when it is initialized explicitly. So just use `string fileDelete = "";` – Fabjan Jan 20 '16 at 13:00
  • Thank you all for your awnsers. I understand the problem now. I just forgot that the compiler can not know that condition 2 is always met when condition 1 is (in my programm), and the compiler saw a case, where File.Delete will be executed without a value, even though this can not happen in my code. – sebastian Jan 20 '16 at 13:44

5 Answers5

3

in C# you have to initialise variables, otherwise you could pass garbage values to File.Delete.

I recommend using

string fileDelete = null; // or ""

and later check for that.

if (!string.IsNullOrEmpty(fileDelete))
{
  File.Delete(fileDelete);
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
1

It works exactly as intended.

In C# local variables are not initialised automatically at declaration.

You're not assinging any value to the fileDelete when declaring it and assigning it only under some condition in your loop.

But you're trying to use its value outside this condition in loop, thus compiler can't deduce - will fileDelete has some value at runtime or not (if code under condition will not be executed - then fileDelete will has no value).

Thus compiler generates this error.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • thats right thank you. I just forgot that the compiler can not know that condition 2 is always met when condition 1 is (in my programm). – sebastian Jan 20 '16 at 13:36
0

Imagine this scenario:

  • countFiles > 5 is True
  • infoFile for someFile is bigger than min

What value will fileDelete have when passed to File.Delete?

Answer: In such scenario fileDelete will be uninitialized, thus the error message.

Kapol
  • 6,383
  • 3
  • 21
  • 46
  • Thats 100% right. I just didn't see that case because it can not happen in my program, but of course the compiler thinks it could happen. – sebastian Jan 20 '16 at 13:48
0

You must initialize your variable. see at Why compile error "Use of unassigned local variable"?

Community
  • 1
  • 1
0

Use this

DateTime min = DateTime.Now;
string[] fileNames = Directory.GetFiles(somePath);
string fileDelete;
int countFiles = fileNames.Length;
if (countfiles > 5)
{
    foreach (string someFile in fileNames)
    {
        FileInfo infoFile = new FileInfo(someFile);
        if (infoFile.CreationTime <= min)
        {
            min = infoFile.CreationTime;
            fileDelete = someFile;
            File.Delete(fileDelete);
        }   
    }

}   
Suvidha
  • 72
  • 4