Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sick of Dealing With Nulls 1

Status
Not open for further replies.

jfrost10

Programmer
Jun 3, 2001
2,004
CA
I love alot of things about .NET but I HATE how they deal with NULLS!!!!!

Please look at my code below and tell me why I can't get my code to recognize that my variable is null!!!

Background; oRow.Item("PageID") is returning a null value.

This
If IsNothing(oRow.Item("PageID")) Then
oRow.Item("PageID") = 0
End If

doesn't work.

This
Dim oNull as DBNull
If oRow.Item("PageID").GetType Is oNull Then
oRow.Item("PageID") = 0
End If

doesn't work

The only thing I've been able to get to work is this:

Try
Dim strTest As String
strTest = oRow.Item("PageID")
strTest = Nothing
Catch
oRow.Item("PageID") = 0
End Try

which I'm sure you can tell I'm not happy with.

Any ideas on how to get this darn null thing working?!

Thanks,

Jack
 
If im not mistaken, you are setting your variable to zero, which does not equal a null value it equals zero. If you want to set something to nothing I use the variable = String.Empty and have had no problems. You may also want to look into vbNullString. Hope this helps you.

SuperCyber
 
hey Super,

Ignore the setting the variable to 0. Thats not the issue.

The issue is that oRow.Item("PageID") is null, but I can't get the code to understand that so it can set the variable to 0!

Both If/Else blocks do NOT execute the code inside teh block, even though the variable is null (I can print the value in the output window and its system.dbnull).

Jack
 
Hi Jack,
Try IsDBnull()
ie if isdbnull(oRow.Item("PageId") then
oRoe.Item("pageid")= 0
else
end if
Pls let me know whether this helped.
LJS
 
isDBNull() will work, as well as

if not myVar = nothing then
it's not null
else
it is null
end if

Personally, I prefer the latter. I never liked the old vb isNull() function --

I have to say that c#'s way is way better. in c#:

if myvar = null then

I would have liked to have been able to use it that way in vb, but oh well. I guess 'nothing' isn't so bad.
penny1.gif
penny1.gif
 
LJS: Thanks! That did it! :D

Paul: I'm sure that C# is alot better at handling it. The code you suggested actually won't run in vb.net (anything with nothing or dbnull needs the Is keyword for some reason, and will yack if you use the = to compare it). There also seems to be a definate disctinction between Nothing and DBNull (which is a little confusing since they both are supposed to mean the same thing).

Thanks for the help guys,

Jack

 
Actually, I did misspeak. What I think I was trying to say was that let's say you have a class:

public class myClass
shared someInt as integer

dim answer as boolean

answer = itsNothing()
someInt = 1
answer = itsNothing()

public function itsNothing() as boolean
dim output
if someInt = nothing then
output = true
else
output = false
end if
return output
end function
end class

Then the first time, answer is True -- then we initialize it, and then it's False.

So I guess I was confusing a non-initialized variable with NULL. But you can use = nothing and it won't yack in this context. To see if something has been "newed up" or not.

I use this also when I'm using state bags to see if there is something in there for me:

If not ViewState.Item("myVar") = Nothing then
'I have something in there
end if

:)
paul
penny1.gif
penny1.gif
 
Paul,

I'll have to try that. I tried a statement exactly as you have it (even in the if statement), and the error I got was that the = sign was not a valid operator for IsDBNull or Nothing.

If you say you've used it, there must be a way though.

Jack
 
So I am curious about this. I've been digging a bit, and here's what I've found on the subject:

If you wish to use IS, then the operand must have a reference type -- I'm not exactly sure what that means, however.

In practice, it means that:

dim someBool as boolean
if someBool is nothing then

will produce the following compiler error:
'Is' requires operands that have reference types, but this operand has the value type 'Boolean'.

as will:

dim someInt as integer
~~~~~

But this:
dim someConnection as sqlConnection
if someConnection is nothing then

will return no such error... and so I guess it all depends on the context of your using it -- and whether or not the operand has a "reference type" (which I sounds like a term for a derived class of some sort, rather than something that inherits directly from System, such as integers and booleans).

seems like it'd have very good uses, though... the simple example from last post could be expanded into some empty variable declarations and you could branch based on what has and has not yet been initialized in some other procedure in the class... almost like a run-time stack trace of sorts.
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top