I want to declare a string array, I was using this way of doing it:
string[] matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
which worked perfectly.
Now I want to enclose the Directory.GetFiles call in a try/catch block, but I can't also have the declaration of the string array in there because then it won't be in the right scope to use it outside of the try block. But if I try this:
string[] matchingActiveLogFiles;
try
{
matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
}
catch (Exception ex)
{
//log error
}
I have not initialized the string array so I have an error. So I am wondering what is best practise in this situation, should I declare the string array outside the try block? And if so how?
-
1Or just set it to null. The C# compiler wants you to initialize all variables before using them (unless passed as an out parameter). Initialization to null is sufficient in this case.Dr. Andrew Burnett-Thompson– Dr. Andrew Burnett-Thompson2012年01月04日 17:33:40 +00:00Commented Jan 4, 2012 at 17:33
-
2None of these are your issue. The issue is your naming. You're defining matchingActiveLogFiles but assigning to matchingFiles. .NET will automatically default your string array to null if you don't explicitly initialize it during definition.Josh– Josh2012年01月04日 17:35:36 +00:00Commented Jan 4, 2012 at 17:35
-
@Josh but C# definite assigment rules require locals to be initialized.phoog– phoog2012年01月04日 18:09:36 +00:00Commented Jan 4, 2012 at 18:09
-
@phoog, yeah I saw the mismatched names and forgot all about him needing to use it after the try/catch. My bad.Josh– Josh2012年01月04日 18:33:35 +00:00Commented Jan 4, 2012 at 18:33
7 Answers 7
The name is different for the string arrays, one is matchingActiveLogFiles the other is matchingFiles:
string[] matchingActiveLogFiles;
try
{
matchingActiveLogFiles = Directory.GetFiles(FilePath, FileNamePattern);
}
catch (Exception ex)
{
//log error
}
4 Comments
matchingActiveLogFiles -- either when you initialise it or in the catch block -- otherwise you'll get a "use of unassigned local variable" compile error when you try to use it.This will initialize your array:
string[] matchingActiveLogFiles = {};
try
{
matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
}
catch (Exception ex)
{
//log error
}
But I'm wondering, what error are you getting?
Even with an uninitialized array, the above code should work.
I also noticed that you have "matchingActiveLogFiles" on line 1 and "matchingFiles" on line 4. Perhaps that's your problem?
1 Comment
null before the try block and the compiler seems okay with it.The issue is your naming. You're defining matchingActiveLogFiles but assigning matchingFiles.
Comments
You should declare the variable in the scope in which that variable is needed.
- If you only need the variable in the try block, put it in there!
- If you need it outside of the try block, what do you want the value to be if your code can't get the file contents? Set it to that on error.
Comments
you do not have to know the exact number of Items in order to initialize an array like
string[] matchingActiveLogFiles = {};
this is what is called a dynamic array it's totally functional and I've had ZERO issues with declaring arrays this way..
List<string> matchingFiles= new List<string>();
try
{
matchingFiles.Add(Directory.GetFiles(FilePath, FileNamePattern));
matchingActiveLogFiles = matchingFiles.ToArray();
}
catch (Exception ex)
{
//logerror
}
Comments
Initialize it first:
string[] matchingActiveLogFiles = new string[0];
1 Comment
Though I generally dislike methods with out params, this seems like a good candidate for a Try method:
bool TryGetMatchingLogFiles(out string[] matchingFiles )
{
matchingFiles = null;
try
{
matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
return true;
}
catch (Exception ex)
{
//logerror
return false;
}
}
Usage:
string[] matchingActiveLogFiles;
if (TryGetMatchingLogFiles(out matchingActiveLogFiles))
{
// Use matchingActiveLogFiles here
}
Or alternatively, just initialise your variable to null:
string[] matchingActiveLogFiles = null;
try ...
Comments
Explore related questions
See similar questions with these tags.