Variable Context Question - c#

In the following code snippet, if I leave out the line of code that is surrounded by the /////'s, I get an error that reads: "Use of unassigned local variable CurrentDate". It seems a bit silly for me to just give CurrentDate an arbitrary value, is there a better way around this?
DateTime CurrentDate;
///////////////////////////
CurrentDate = DateTime.Now;
///////////////////////////
if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
if(CurrentDate == DateTime.Now)
{
...
}

Don't do if (1 == 1)?
Seriously though if the compiler is giving you this error it's usually either because your code is wrong or it's because it's too complex and could be better expressed in another way where you don't need to access possibly unassigned variables.
Can you come up with a real world example where you get this error where there isn't an obvious solution by making a simple refactoring? This would make your question more answerable.
Having said that if you do run into one of these situations there are a few other approaches you could use:
DateTime CurrentDate = DateTime.MaxValue;
DateTime CurrentDate = default(DateTime);
DateTime? CurrentDate = null;
I like the last option because it expresses what you mean - that you don't know the value. It makes the code a little more verbose though as you have an extra level of redirection every time you wish to access a value. You can use the time spent typing .Value to consider whether or not you have correctly handled the situation where it could be null.
Also: Have you considered that the value of DateTime.Now could change between the first and second calls? That final if statement looks like it won't do what you intended.

You could do this:
DateTime CurrentDate;
if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
else
{
///////////////////////////
CurrentDate = DateTime.Now;
///////////////////////////
}
if(CurrentDate == DateTime.Now)
{
...
}
This will eliminate the compiler error.
NOTE: in VS2008 this will compile:
if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
//else
//{
/////////////////////////////
//CurrentDate = DateTime.Now;
/////////////////////////////
//}
if(CurrentDate == DateTime.Now)
{
//
}

why dont just assigned directly
DateTime CurrentDate = DateTime.Now;

The example is a bit convoluted, but, no, there isn't a better way around that.
You have to assign a value to a variable heading into a conditional block, even if you are sure that the block will always execute (the 1=1 in your example)
However, I would recommend against DateTime.Now as the initialization value, because it by some mirracle, the block does not execute, the situation should be easily detectable, and DateTime.Now sound way more real than DateTime.MinValue

I always set my objects to null so that it throws a null reference exception if a value is not created. However as Hans pointed out null doesn't work in this situation because in their infinite wisdom Microsoft decided to make DateTime not nullable. As such I normally use DateTime.MinValue since it gives me a specific value to check for and other than time traveling will always be in the past unlike DateTime.MaxValue which is coming up here for some of you archaic 32bit peeps.
Example
DateTime CurrentDate;
///////////////////////////
CurrentDate = DateTime.MinValue;
///////////////////////////
if(1==1)
{
CurrentDate = DateTime.Now.AddDays(1);
}
if(CurrentDate == DateTime.Now)
{
...
}

Related

Problems with date time and infinite loops

I know there probably is a really an obvious answer to this question but why am I getting an infinite loop with this code?
(laterDate1 is later date than dateTime1 and laterDate2 is a later date than dateTime2)
while (dateTime1.CompareTo(laterDate1) <= 0)
{
DateTime dateTime2 = otherDateTime;
while (dateTime2.CompareTo(laterDate2) <= 0)
{
dateTime2.AddDays(1);
}
dateTime1.AddDays(1);
}
Thanks in advance for your help. (My brain is not working today)
dateTime1.AddDays(1); does not modify dateTime1. It returns new DateTime instance. You have to assign it back to your variable:
dateTime1 = dateTime1.AddDays(1);
The same applies to dateTime2.AddDays(2):
dateTime2 = dateTime2.AddDays(1);
btw, DateTime is a struct and is immutable, so every state-changing method returns new instance, instead of modifying the one you're calling it on. You should remember about that while working with DateTime.

Using ToString on a DateTime object

In my database the date is stored like datetime but when I want to perform a search/filtering I want it to be based only on the date ignoring the exact time. I spend a lot of time figuring out how to do it and finally I got a working solution on my own :
string val = rule.Data;
if (!string.IsNullOrEmpty(val))
{
switch (rule.Field)
{
case "Date": {
DateTime parsedDate = DateTime.ParseExact(
val,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);
var pYear = parsedDate.Year;
var pMonth = parsedDate.Month;
var pDay = parsedDate.Day;
rows = rows.Where(o => o.Date >= parsedDate && o.Date <= new DateTime(pYear, pMonth, pDay, 12, 59, 40)); break;
}
}
}
This is working Ok. It needs a little change but I think I can use the code above. However today a college of mine pass me a solution which is from a previous project being developed here, and this solution is a lot shorter and I would prefer to use it if possble. It looks like this:
string val = rule.Data;
if (!string.IsNullOrEmpty(val))
{
switch (rule.Field)
{
case "Date": { rows = rows.Where(o => o.Date.ToString("dd/MM/yyyy") == val); break; }
}
}
The code doesn't break when I try this but it's not filtering data too. I always get empty result. I guess that o.Date.ToString("dd/MM/yyyy") is where the problem lies. I don't know is it ok to use ToString() like this for DateTime object. In the example I'm using ToString() also get a format type like the one I'm providing here - ToString("dd/MM/yyyy") - but in my case ToString() is not overloaded anywhere. Is this a standard way to manipulate DateTime objects or I just can't find the place where ToStrin() is predefined. And finally, can you provide me with a working example based on the code above.
Depending on what culture o.Date is, Try:
string val = rule.Data;
if (!string.IsNullOrEmpty(val))
{
switch (rule.Field)
{
case "Date":
{
rows = rows.Where(o => o.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) ==
DateTime.ParseExact(val,
"dd/MM/yyyy",
CultureInfo.InvariantCulture));
break;
}
}
}
Or you could set the culture of the current thread instead:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
Edit: It should work if you avoid using strings:
e.g.
DateTime maxDate = new DateTime(2020, 11, 17);
if (DateTime.Now.Date > maxDate)
{
// this will just work regardless of setting culture
}
I think if you need to compare dates then you can just get a Date component of a DateTime and compare it to your predefined value. This should be faster as there won't be a need to transform date to string every time as well. So you can first get your reference value like that DateTime.ParseExact(value, "dd/MM/yyyy", CultureInfo.InvarianCulture). You can just use a constructor of a DateTime to compose it as well.
You shouldn't have to use strings at all. If it is a datetime (or similar) in the database, and a DateTime in your c# code, then there is never a good reason to use a string as an intermediate step.
Also, you should pay close attention to the .Kind property of your DateTime values. And you should never be comparing local times against DateTime.Now. If you do, you may introduce errors during daylight saving time transitions. Instead, you should use UTC DateTime values, or use DateTimeOffset values instead. Read more here.

Casting an object which could be null

DateTime? testDate = (DateTime?)arrayOfObjects[dateObject];
Does that code look ok? I attempted to use the as operator but I got the 'non-nullable' error. What I'm trying to say is that the object I'm choosing from the array is either DateTime or a null DateTime but either can be assigned to testDate.
Doesn't feel right doing it this way, I think I'm missing something obvious.
EDIT: I suppose it's the same as the way I could've adapted the as in the following way:
DateTime? testDate = arrayOfObjects[dateObject] as DateTime?;
Is either line of code the best way of handling potential nulls?
Is either line of code the best way of handling potential nulls?
The second form will silently result in null when the array contains something other than a DateTime. That seems a good reason to use the first.
To the basic question:
am I missing something or is this the typical (or at least an acceptable) approach
It is acceptable but a little obscure maybe, because it is 'hiding' an unboxing operation.
You could use:
DateTime? testDate = null;
if (arrayOfObjects[dateObject] != null)
testDate = (DateTime) arrayOfObjects[dateObject]; // no '?'
But that's verbose. And this particular problem doesn't lend itself well to the conditional operator (?:)
So I would stick with your first version.
DateTime? is a shorter form for another struct
Nullable<DateTime> {
bool HasValue;
DateTime Value;
}
You will never get this type from your DB, so the first line will never cast correctly. The database will provide you with a DateTime value stored in an object variable. Or a null (untyped).
DateTime is a struct, so "as" operator won't work for it. So, simply check for null as follows:
DateTime? testDate = arrayOfObjects[dateObject] == null ? (DateTime?) null : (DateTime)arrayOfObjects[dateObject];
A better approach would be to do something like:
if(arrayOfObjects[dateObject] != null && arrayOfObjects[dateObject] is DateTime)
{
DateTime testDate = (DateTime)arrayOfObjects[dateObject];
// logic here
}
or something like:
DateTime? testDate = null;
if(arrayOfObjects[dateObject] is DateTime)
{
testDate = (DateTime)arrayOfObjects[dateObject];
}
else if (arrayOfObjects[dateObject] is Nullable<DateTime>)
{
testDate = (Nullable<DateTime>)arrayOfObjects[dateObject];
}
Try using Array.ConvertAll method. Below is roughly how it is implemented:
DateTime?[] dates = Array.ConvertAll<object, DateTime?>arrayOfObjects,DateTime);
Note: This is just a rough idea. you can correct it to suit your self.

Set an empty DateTime variable

I would declare an empty String variable like this:
string myString = string.Empty;
Is there an equivalent for a 'DateTime' variable ?
Update :
The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL.
E.g:
DateTime? someDate = null;
myCommand.Parameters.AddWithValue("#SurgeryDate", someDate);
When I run this code an exception is catched telling me the 'StoredProcedure' expected a '#SurgeryDate' parameter.
But i provided it.
Any idea why?
Since DateTime is a value type you cannot assign null to it, but exactly for these cases (absence of a value) Nullable<T> was introduced - use a nullable DateTime instead:
DateTime? myTime = null;
No. You have 2 options:
DateTime date = DateTime.MinValue;
This works when you need to do something every X amount of time (since you will always be over MinValue) but can actually cause subtle errors (such as using some operators w/o first checking if you are not MinValue) if you are not careful.
And you can use Nullable:
DateTime? date = null;
Which is nice and avoids most issues while introducing only 1 or 2.
It really depends on what you are trying to achieve.
You can set a DateTime variable to be '1/1/0001 00:00:00' but the variable itself cannot be null. To get this MinTime use:
DateTime variableName = DateTime.MinValue;
You may want to use a nullable datetime. Datetime? someDate = null;
You may find instances of people using DateTime.Max or DateTime.Min in such instances, but I highly doubt you want to do that. It leads to bugs with edge cases, code that's harder to read, etc.
The method you used (AddWithValue) doesn't convert null values to database nulls. You should use DBNull.Value instead:
myCommand.Parameters.AddWithValue(
"#SurgeryDate",
someDate == null ? DBNull.Value : (object)someDate
);
This will pass the someDate value if it is not null, or DBNull.Value otherwise. In this case correct value will be passed to the database.
Either:
DateTime dt = new DateTime();
or
DateTime dt = default(DateTime);
If you set the date to
DateTime dNewDate = new DateTime();
The value is set to {1/1/0001 12:00:00 AM}
Option 1: Use a nullable DateTime?
Option 2: Use DateTime.MinValue
Personally, I'd prefer option 1.
A string is a sequence of characters. So it makes sense to have an empty string, which is just an empty sequence of characters.
But DateTime is just a single value, so it's doesn't make sense to talk about an “empty” DateTime.
If you want to represent the concept of “no value”, that's represented as null in .Net. And if you want to use that with value types, you need to explicitly make them nullable. That means either using Nullable<DateTime>, or the equivalent DateTime?.
DateTime (just like all value types) also has a default value, that's assigned to uninitialized fields and you can also get it by new DateTime() or default(DateTime). But you probably don't want to use it, since it represents valid date: 1.1.0001 0:00:00.
There's no such thing as an empty date per se, do you mean something like:
DateTime? myDateTime = null;
The .addwithvalue needs dbnull.
You could do something like this:
DateTime? someDate = null;
//...
if (someDate == null)
myCommand.Parameters.AddWithValue("#SurgeryDate", DBnull.value);
or use a method extension...
public static class Extensions
{
public static SqlParameter AddWithNullValue(this SqlParameterCollection collection, string parameterName, object value)
{
if (value == null)
return collection.AddWithValue(parameterName, DBNull.Value);
else
return collection.AddWithValue(parameterName, value);
}
}
This will work for null able dateTime parameter
. .
SearchUsingDate(DateTime? StartDate, DateTime? EndDate){
DateTime LastDate;
if (EndDate != null)
{
LastDate = (DateTime)EndDate;
LastDate = LastDate.AddDays(1);
EndDate = LastDate;
}
}

C# DateTime evaluation problem

So I'm trying to figure out what I'm doing wrong with this logic. It seems straightforward and my breakpoints indicate that the evaulation in the 'if' statement is resolving as True, but sum.ppStart et al aren't getting 14 days added to them.
It's probably something simple, but any help would be appreciated.
//Determine the start/end days of each week of the pay period and retrieve a list of those entries
DateTime[] weeks = timeTools.calcPP(0);
DateTime today = DateTime.Now.Date;
if (today > weeks[3])
{
weeks[0].AddDays(14);
weeks[3].AddDays(14);
weeks[4].AddDays(14);
}
sum.ppStart = weeks[0];
sum.ppEnd = weeks[3];
sum.payDate = weeks[4];
AddDays returns a new instance of DateTime, the existing value is not changed, it is an immutable structure. When using the function, capture the result
DateTime myDate = ...
myDate = myDate.AddDays(14);
That is because you're not using the result of the AddDays method. The signature is
public DateTime AddDays(double days)
or so (see link). You need to do this:
weeks[0] = weeks[0].AddDays(14);
You need to assign the values:
if (today > weeks[3])
{
weeks[0] = weeks[0].AddDays(14);
weeks[3] = weeks[3].AddDays(14);
weeks[4] = weeks[4].AddDays(14);
}

Categories