Reputation: 63
How can I turn multiple If statements into a single array to check file datetime of several files with the same name?
Currently I have
if (location1)
{
loc = 1;
DateTime dateDailyFile1 = Convert.ToDateTime(File.GetLastWriteTime(ediFile1));
if (Convert.ToDateTime(dateDailyFile1.ToShortDateString()) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
{
return false;
}
else
{
return true;
}
}...
I do this about 20 times. Which as you may gather is quite grueling. I wanted to put it in an array and just loop through it but the DateTime assignment on the array keeps throwing an error. The following is the code I'm trying to change it to. **qualifier - location[] is a bool assigned by clicking a checkbox.
DateTime[] dateDailyFile;
string[] ediFile = { "file1", "file2", "file3", "file4" ... };
string[] loc = { "loc1", "loc2", "loc3", "loc4" ... };
int x;
for (x = 0; x < loc.length; x++)
{
if (location[x])
{
dateDailyFile[x] = Convert.ToDateTime(File.GetLastWriteTime(ediFile[x]));
}
}...
The last line dateDailyFile[x] = Convert.ToDateTime(File.GetLastWriteTime(ediFile[x])); is my problem. I am getting a "Use of unassigned local variable dateDailyFile".
What exactly did I do wrong and what is the best method to solve this issue?
Upvotes: 2
Views: 1310
Reputation: 63
It was a combination that helped me work out the correct process. D.Stanley's explanation of my error along with ispiro's correction helped me work out the correct process - listed below.
var ediFile = new[] {loc1, loc2, loc3, loc4 ...}
DateTime[] dateDailyFile = new DateTime[20];
int x;
for (x = 0; x < bLocation.Length; x++)
{
if (bLocation[x])
{
....
dateDailyFile[x] = Convert.ToDateTime(File.GetLastWriteTime(ediFile[x]));
return false;
}
else
{
return true;
}
}...
D. Stanley correctly identified the root cause coupled with ispiro's suggestion I managed to get it working.
String[] loc was not needed in the correction.
Upvotes: 0
Reputation: 4808
Arrays need a length defined at declaration.
You can instead use a List
, which doesn't need a size specified at declaration, making it more flexible:
List<DateTime> dateDailyFile;
string[] ediFile = { "file1", "file2", "file3", "file4" ... };
string[] loc = { "loc1", "loc2", "loc3", "loc4" ... };
int x;
for (x = 0; x < loc.length; x++)
{
if (location[x])
{
dateDailyFile.Add(Convert.ToDateTime(File.GetLastWriteTime(ediFile[x])));
}
}...
Upvotes: 2
Reputation: 152566
It seems like you are trying to access variables by their string name, which is not possible with that syntax. However, you could store the values in an array like so:
var ediFile = new []{ file1, file2, file3, file4 ... };
var loc = new [] { loc1, loc2, loc3, loc4 ... };
Then access then using array indexers:
int x;
for (x = 0; x < loc.length; x++)
{
if (location[x])
{
dateDailyFile[x] = Convert.ToDateTime(File.GetLastWriteTime(ediFile[x]));
}
}...
Upvotes: 2
Reputation: 27673
Change the first line to:
DateTime[] dateDailyFile = new DateTime[whateverIsTheExpectedLength];//20?
Upvotes: 4