annoying null problems
-
why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment:
DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text);
and, in another class, the test:if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino));
VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;) -
why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment:
DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text);
and, in another class, the test:if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino));
VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;) -
before using the datetime variable you must create it. DateTime dataTermino = new DateTime() will do the job for you
-
why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment:
DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text);
and, in another class, the test:if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino));
VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)The
DateTime
class is a struct and so can't be null. It's just like int, double, or one of the other value types, they can't be null. -
ok, but after i want test if it is null. with a object reference i cant test it, and i dont want reference the actual date.
There are 2 types of objects in .NET: reference types and value types. All classes are reference types. All structs are value types. All reference types can be assigned null. All value types cannot. The reason for these 2 is for lightweight, fast stack-allocated values. For instance,
System.Int32
, also aliased asint
is a value type: you can pass it to a function which will actually pass a copy of the value. Value types always must have a value; you can't assign them null, as that's not how value types work, nor should it be. Assigning an integer to null doesn't make sense, for instance. And since DateTime is a value type, you can't assign null to it. Now, if you were using .NET 2.0, there is a new wrapper class called System.Nullable, where you can wrap a value type inside an object that can either be null or the value. Here's what that looks like:Nullable<DateTime> nullableDateTime = new Nullable<DateTime>;
nullableDateTime = null;
nullableDateTime = DateTime.Now;C# 2.0 has short-hand syntax for this, using the question mark:
DateTime? nullableDateTime = new DateTime?();
nullableDateTime = null;
nullableDateTime = DateTime.Now;Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment:
DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text);
and, in another class, the test:if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino));
VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)pontonet wrote:
WTF!
Why not learn more about the environment in which you are developing before exclaiming "WTF!". The difference between value and reference types is in any good beginners guide to .NET and/or C#.
-
There are 2 types of objects in .NET: reference types and value types. All classes are reference types. All structs are value types. All reference types can be assigned null. All value types cannot. The reason for these 2 is for lightweight, fast stack-allocated values. For instance,
System.Int32
, also aliased asint
is a value type: you can pass it to a function which will actually pass a copy of the value. Value types always must have a value; you can't assign them null, as that's not how value types work, nor should it be. Assigning an integer to null doesn't make sense, for instance. And since DateTime is a value type, you can't assign null to it. Now, if you were using .NET 2.0, there is a new wrapper class called System.Nullable, where you can wrap a value type inside an object that can either be null or the value. Here's what that looks like:Nullable<DateTime> nullableDateTime = new Nullable<DateTime>;
nullableDateTime = null;
nullableDateTime = DateTime.Now;C# 2.0 has short-hand syntax for this, using the question mark:
DateTime? nullableDateTime = new DateTime?();
nullableDateTime = null;
nullableDateTime = DateTime.Now;Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
pontonet wrote:
WTF!
Why not learn more about the environment in which you are developing before exclaiming "WTF!". The difference between value and reference types is in any good beginners guide to .NET and/or C#.
sometimes we forgot the basic, even after read a lot of stuffs. sometimes we are kinda influencied by other languages, like java. a DateTime tends to be a real object instead of a value type - it has properties, methods, constructors, etc. so the difference isn`t so obvious. :-D
-
why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment:
DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text);
and, in another class, the test:if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino));
VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)If you're using C# 2.0 you can declare DateTime as nullable with the following: DateTime? dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); (note the "?" after DateTime)would work. If you're not using 2.0 & need to pass null date to SQL , have a look at: http://www.c-sharpcorner.com/Code/2003/Sept/EnterNullValuesForDateTime.asp[^] Hope this helps
-
before using the datetime variable you must create it. DateTime dataTermino = new DateTime() will do the job for you
-
tcss wrote:
before using the datetime variable you must create it.
Not at all. DateTime is a value type, not a reference type.
--- b { font-weight: normal; }