Johnny Bones
Johnny Bones

Reputation: 8404

Date are being looked at as bools?

I have this piece of C# code, and I'm trying to run part of it conditionally:

    private void GetMyFiles()
    {
        string rootDirectory = @"" + txtPathToScan.Text + "";
        string extension = "*";

        List<FileInfo> files = GetFilesRecursively(rootDirectory, extension);

        SqlCeConnection conParam = new SqlCeConnection();
        conParam.ConnectionString = FileFinder.Properties.Settings.Default.Database1ConnectionString;
        conParam.Open();

        SqlCeCommand DBParams = new SqlCeCommand("SELECT FileSize, DateLastAccessedGT, DateLastAccessedLT, DateLastModifiedGT, DateLastModifiedLT FROM FF_tblParams", conParam);
        SqlCeDataReader reader;

        reader = DBParams.ExecuteReader();
        reader.Read();

        using (SqlCeDataReader DT1 = DBParams.ExecuteReader())
        {

            double FLenP = Convert.ToDouble(reader["FileSize"]);
            DateTime FAccessGT = Convert.ToDateTime(reader["DateLastAccessedGT"]);
            DateTime FAccessLT = Convert.ToDateTime(reader["DateLastAccessedLT"]);
            DateTime FModifiedGT = Convert.ToDateTime(reader["DateLastModifiedGT"]);
            DateTime FModifiedLT = Convert.ToDateTime(reader["DateLastModifiedLT"]);


            //Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
            foreach (FileInfo file in files)
            {
                //Console.WriteLine(file);
                var info = new System.IO.FileInfo(@"" + file + "");

                string FName = info.Name;
                string FPath = file.FullName;
                double FLen = (info.Length + 1023) / 1024;
                DateTime FCreate = info.CreationTime;
                DateTime FAccess = info.LastAccessTime;
                DateTime FWrite = info.LastWriteTime;

                //**This is where my problem is**

                if (FLenP > FLen && FAccessLT <= FAccess <= FAccessGT && FModifiedLT <= FWrite <= FModifiedGT)
                {
                    AddNewRecords(FPath, FName, FLen, FCreate, FAccess, FWrite);
                }
            }
        }

    }

I first tried this by commenting out the If statement to make sure it looked OK. When I run the code, I put in a break to see what was happening. Everything looked OK in terms of dates looked like dates, etc... However, when I uncomment the If statement, I get this error:

Operator '<=' cannot be applied to operands of type 'bool' and 'System.DateTime'

Any idea why that might be happening?

Upvotes: 0

Views: 29

Answers (2)

marcieng
marcieng

Reputation: 1041

you can not compare 3 operands. when you do that, it evalutes to bool vs third operand. say

if (1 < 2 < 3) { ... }

this is equal to

if ( true < 3) { ... }

Upvotes: 0

David
David

Reputation: 219006

This isn't being interpreted the way you think:

FLenP > FLen && FAccessLT <= FAccess <= FAccessGT && FModifiedLT <= FWrite <= FModifiedGT

Operators are evaluated in order, they don't group together. So this:

FAccessLT <= FAccess <= FAccessGT

becomes this:

(FAccessLT <= FAccess) <= FAccessGT

What's inside the parentheses evaluates to a bool, which then can't be used in a <= expression with a DateTime. Hence the error.

Only one comparison can be performed at a time. You need to separate the logical conditions. In the above portion it would likely look like this:

FAccessLT <= FAccess && FAccess <= FAccessGT

Upvotes: 2

Related Questions