468,768 Members | 1,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

confused about assigning nothing..

'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why ? The CheckDate function gets to the Return Nothing line,
but that is not what gets assigned to myDate

--
Adrian Parker
Nov 20 '05 #1
7 3193
I'd almost consider this a VB.NET bug, but that's just me. C# wouldn't have
let you compile this at all.

Date is actually a VB.NET language wrapper around System.DateTime.
System.DateTime is not a class, it's a structure - which is a value type.
What that means to you is you can't assign nothing to a value type, so you
cant assign Nothing to a Date.

VB.NET "helpfully" changes your
[myDate = nothing] line (which is basically what is hapenning) into
[myDate = Date.MinValue] (which is the the same value VB.NET will
initialize Date types into by default if you don't initalize them
explicitly.

That's why you're seeing confusing behavior.

"Adrian Parker" <ap******@nospam.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why ? The CheckDate function gets to the Return Nothing line, but that is not what gets assigned to myDate

--
Adrian Parker

Nov 20 '05 #2
* "Adrian Parker" <ap******@nospam.com> scripsit:
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why ? The CheckDate function gets to the Return Nothing line,
but that is not what gets assigned to myDate


'DateTime' is a value type, assigning 'Nothing' to it will set it to its
default value.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
ah, ok, makes sense. thanks guys.

"Philip Rieck" <st***@mckraken.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
I'd almost consider this a VB.NET bug, but that's just me. C# wouldn't have
let you compile this at all.

Date is actually a VB.NET language wrapper around System.DateTime.
System.DateTime is not a class, it's a structure - which is a value type.
What that means to you is you can't assign nothing to a value type, so you
cant assign Nothing to a Date.

VB.NET "helpfully" changes your
[myDate = nothing] line (which is basically what is hapenning) into
[myDate = Date.MinValue] (which is the the same value VB.NET will
initialize Date types into by default if you don't initalize them
explicitly.

That's why you're seeing confusing behavior.

"Adrian Parker" <ap******@nospam.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why ?

The CheckDate function gets to the Return Nothing line,
but that is not what gets assigned to myDate

--
Adrian Parker


Nov 20 '05 #4
Close, but only 1/2 a cigar :-)

Date is not a wrapper. It is an alias to System.DateTime. The compiler sees
this, and writes System.DateTime in the background. Just like string
(lowercase) is a C# alias for System.String.

However, you are correct in that System.DateTime is a value type (in this
case, structure), and not a reference type. Where the line becomes fuzzy
about "right/wrong/bug/feature" is that while you cannot directly assign
null to a value type, you can box value types and treat them like reference
types. Adrian's example in C#, using more explicit boxing, would produce:
return (DateTime)((Object)null);
which C# does in fact let you compile, although this produces a runtime null
exception.
And though you cannot explicitly and directly assign null to a value type in
C#, it is perfectly valid with reguards to the C# compiler to assign a
reference type value which *MIGHT* be null, though again, you would get a
runtime null exception.
In VB's case, as you state, VB "helpfully" invokes
MS.VB.CompilerServices.DateType.FromObject(null) to initialize the date
variable, which in thise case produces DateTime.MinValue, partly because
it's a VB language feature that all local variables of value types are
initialized to a default value. They are never in an "unassigned" state. In
C#, you are forced to initialize them explicitly before you use them.

-Rob Teixeira [MVP]
"Philip Rieck" <st***@mckraken.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'd almost consider this a VB.NET bug, but that's just me. C# wouldn't have let you compile this at all.

Date is actually a VB.NET language wrapper around System.DateTime.
System.DateTime is not a class, it's a structure - which is a value type.
What that means to you is you can't assign nothing to a value type, so you
cant assign Nothing to a Date.

VB.NET "helpfully" changes your
[myDate = nothing] line (which is basically what is hapenning) into
[myDate = Date.MinValue] (which is the the same value VB.NET will
initialize Date types into by default if you don't initalize them
explicitly.

That's why you're seeing confusing behavior.

"Adrian Parker" <ap******@nospam.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why ?

The CheckDate function gets to the Return Nothing line,
but that is not what gets assigned to myDate

--
Adrian Parker


Nov 20 '05 #5
Heh.. you definately get more points for detail.. I did simplify when I
called it a "language wrapper" (as in the language (compiler) just wraps
something for you) -- that's what I call Integer as well (and yes, string in
c#), even though it's the same concept. Perhaps I'll try to force myself to
say "alias" from now on. Nope, now I'm thinking about cute secret agents.

However, I really do like c#'s behavior on this better -- it would not have
compiled the function Adrian wrote at all, giving an error that would have
sent him down the right path. And while I can see the language design
value ("make it simple for people") in setting value types to default values
so that they can be used without initialization, I really can't see the
value in allowing "valuetype = nothing" and turning it into the same thing
with nary a warning. People (well, me) expect nothing to be nothing, and
Date.MinValue to be a date value.

Perhaps I go too far saying I would consider it a VB.NET "bug"... but I
really don't like it either way.
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Close, but only 1/2 a cigar :-)

Date is not a wrapper. It is an alias to System.DateTime. The compiler sees this, and writes System.DateTime in the background. Just like string
(lowercase) is a C# alias for System.String.

However, you are correct in that System.DateTime is a value type (in this
case, structure), and not a reference type. Where the line becomes fuzzy
about "right/wrong/bug/feature" is that while you cannot directly assign
null to a value type, you can box value types and treat them like reference types. Adrian's example in C#, using more explicit boxing, would produce:
return (DateTime)((Object)null);
which C# does in fact let you compile, although this produces a runtime null exception.
And though you cannot explicitly and directly assign null to a value type in C#, it is perfectly valid with reguards to the C# compiler to assign a
reference type value which *MIGHT* be null, though again, you would get a
runtime null exception.
In VB's case, as you state, VB "helpfully" invokes
MS.VB.CompilerServices.DateType.FromObject(null) to initialize the date
variable, which in thise case produces DateTime.MinValue, partly because
it's a VB language feature that all local variables of value types are
initialized to a default value. They are never in an "unassigned" state. In C#, you are forced to initialize them explicitly before you use them.

-Rob Teixeira [MVP]
"Philip Rieck" <st***@mckraken.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'd almost consider this a VB.NET bug, but that's just me. C# wouldn't

have
let you compile this at all.

Date is actually a VB.NET language wrapper around System.DateTime.
System.DateTime is not a class, it's a structure - which is a value type.
What that means to you is you can't assign nothing to a value type, so you cant assign Nothing to a Date.

VB.NET "helpfully" changes your
[myDate = nothing] line (which is basically what is hapenning) into
[myDate = Date.MinValue] (which is the the same value VB.NET will
initialize Date types into by default if you don't initalize them
explicitly.

That's why you're seeing confusing behavior.

"Adrian Parker" <ap******@nospam.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCol) Is System.DBNull.Value Then
Return Nothing
Else
Return DRow.Item(strCol)
End If
End Function
Dim DRow as DataRow
Dim myDate as Date

' do some sql to pupulate DRow with data (column EndDate is null)
' then..
myDate = CheckDate(myDataRow,"EndDate")

if I look at myDate now, it contains 12:00am instead of Nothing.. why

? The CheckDate function gets to the Return Nothing line,
but that is not what gets assigned to myDate

--
Adrian Parker



Nov 20 '05 #6

"Philip Rieck" <st***@mckraken.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Heh.. you definately get more points for detail.. I did simplify when I
called it a "language wrapper" (as in the language (compiler) just wraps
something for you) -- that's what I call Integer as well (and yes, string in c#), even though it's the same concept. Perhaps I'll try to force myself to say "alias" from now on. Nope, now I'm thinking about cute secret

agents.

I guess the only reason i make the distinction is because there are tons of
actual wrappers in .NET, mostly for unamanged code constructs (like COM
components). Seems to imply "extra" code wrapped around another construct. I
had to get in the habit of saying Alias too, but since that's what they use
all over the Specifications, it stuck - though admitadly not as cute as the
agent.

As for the null assignment, I can see part of why the VB compiler would do
that, but I agree in so much as using Option Strict On should at the very
least provide a warning.

-Rob Teixeira [MVP]

Nov 20 '05 #7
Although it's a fudge, you can still use datevar = nothing and then compare it to nothing to see if it's set to the autocasted
default var.

But what would be the prefered method ? create an object that encapsulated the datevar and a boolean test property ?

-Adrian


Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by jfj | last post: by
13 posts views Thread by agentxx04 | last post: by
6 posts views Thread by m_a_t_t | last post: by
6 posts views Thread by rahul8143 | last post: by
8 posts views Thread by Rachel Suddeth | last post: by
3 posts views Thread by redefined.horizons | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.