简体   繁体   中英

C# Array with DateTime[]

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?

将第一行更改为:

DateTime[] dateDailyFile = new DateTime[whateverIsTheExpectedLength];//20?

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]));
        }
}...

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])));
        }
}...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM