473,625 Members | 2,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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 3444
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******@nospa m.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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******@nospa m.com> scripsit:
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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******** ********@TK2MSF TNGP12.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******@nospa m.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date
If DRow.Item(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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)((Obj ect)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.CompilerS ervices.DateTyp e.FromObject(nu ll) to initialize the date
variable, which in thise case produces DateTime.MinVal ue, 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******** ********@TK2MSF TNGP12.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******@nospa m.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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)((Obj ect)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.CompilerS ervices.DateTyp e.FromObject(nu ll) to initialize the date
variable, which in thise case produces DateTime.MinVal ue, 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******** ********@TK2MSF TNGP12.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******@nospa m.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
'function to convert null to nothing
Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCo l) Is System.DBNull.V alue Then
Return Nothing
Else
Return DRow.Item(strCo l)
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(myDat aRow,"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******** ********@TK2MSF TNGP12.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
1646
by: jfj | last post by:
I don't understand. We can take a function and attach it to an object, and then call it as an instance method as long as it has at least one argument: ############# class A: pass def foo(x): print x
16
6224
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
13
2188
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int median_index;
6
1813
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */ main() { int c;
6
1537
by: rahul8143 | last post by:
hello, I am really confused over following for code snippets. No problem that they are working codes but how? how they are evaluated by compiler? It will be my pleasure if you explain me all following snippets. 1) { int i=3; for (i--; i<7; i=7) printf("%d",i++);
8
1580
by: Rachel Suddeth | last post by:
I'm trying to use reflection to create an object at runtime where the type is given by a string. I thought we should be able to do that, but so far it's not working. I'm trying to work on reproducing the scenario in a simpler way, but it's slow going, and maybe there is something very basic I don't know yet.... Given the following lines of code: ------------- CredLookup testLU = new CredLookup(); string sLUType =...
6
1956
by: Louise | last post by:
I use Visual Basic .NET Framework 1.1, Visual Studio. I have a Web page (aspx) referencing a control (ascx) that has a placeholder that is assigned one of 2 child controls through buttons. The ViewState of the second child control gets confused with the first child control - the first Text property in the 2nd child gets updated with the first Text property of the 1st child when the 2nd button is clicked after the 1st. For instance,...
20
6953
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
3
2163
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder for a member variable of a Python object. It has to be given a value when defined, or set within a method. But what is the difference between an Attribute of a Class, a Descriptor in a Class, and a Property in a Class?
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.