Avatar of beef137
beef137
 asked on

How can you set the value of a variable to Null?

How can you set the value of a variable to Null?

Dim WORKORDERNUM
WORKORDERNUM = Nothing

This doesn't seem to be making the value null. I'm using Visual Studio 2008. = Null isn't doing it either.
.NET ProgrammingVisual Basic Classic

Avatar of undefined
Last Comment
beef137

8/22/2022 - Mon
silemone

If its a built in class, then use intellisense and get the null value.
Otherwise
DIM x = NULL
If its a string,
Dim x = string.empty
CSLEEDS

try:
WORKORDERNUM =vbnullchar or
WORKORDERNUM =vbnullstring
ee_rlee

hi, try this

WORKORDERNUM = DBNull.Value
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
silemone

I use it in Vb2008...how do you know its not working?  are you still getting information after settting it?  if so is the logic correct, i.e. is that point in code being hit.  maybe you should post your code.
Guy Hengel [angelIII / a3]

what kind of "null"?

it might be DBNull.Value you are looking for...
francrl

What is the variable type?
Have you tried WORKORDERNUM = ""  ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
beef137

ASKER
What if it is an integer?
beef137

ASKER
It says DBNull.Value cannot be used for an integer
silemone

not for ints...have to use zero instead.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ee_rlee

try

Dim WORKORDERNUM As Nullable(Of Integer)
WORKORDERNUM = Nothing
beef137

ASKER

Public Class SearchForm
    Public PHONENUM
    Public WORKORDERNUMSEARCH
    Public SCSERVICENUM
 
    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        If Me.PhoneTextBox.Text <> "" Then
            PHONENUM = Me.PhoneTextBox.Text
        Else
            PHONENUM = 0
        End If
 
        If Me.WOTextBox.Text <> "" Then
            WORKORDERNUMSEARCH = Me.WOTextBox.Text
        Else
            WORKORDERNUMSEARCH = DBNull.Value
        End If
 
        If Me.SCServiceTextBox.Text <> "" Then
            SCSERVICENUM = Me.SCServiceTextBox.Text
        Else
            SCSERVICENUM = DBNull.Value
        End If
 
        Me.WOTableAdapter.FillBySEARCH(Me.SearchDataSet.WO, WORKORDERNUMSEARCH, SCSERVICENUM)
    End Sub
 
End Class

Open in new window

silemone

or use a negative number as a flag, like say -1...that way if you need zeros, you can move on with your project if you using unsigned values.  are you getting data from a DB?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
silemone

why aren't you using these values as strings anyway (just for i can understand).  are you doing arithmetic on them in the program?
beef137

ASKER
ee_rlee,

When I try

Dim WORKORDERNUM As Nullable(Of Integer)
WORKORDERNUM = Nothing

it says Nullable Object must have value
silemone

if you're not doing math with them, they should be strings or it can cause confusion in the future, say you someone tries to concatenate them and add them together instead...just a thought...just learned something new from er-lee cool
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
silemone

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CSLEEDS

so, the value assigned will going for further processing, then what process/ comparison u wanna do with these parameter?
beef137

ASKER
silemone,

Here is my SQL:

SELECT WORKORDER_NUM, STATUS, LAST_NAME, FIRST_NAME, ADDRESS, CITY, STATE, ZIP
FROM   WO
WHERE ((@WORKORDERNUMSEARCH = 0 AND 1=1) OR (WORKORDER_NUM = @WORKORDERNUMSEARCH)) AND
      ((@SCSERVICENUM = 0 AND 1=1) OR (SERVICE_NUM = @SCSERVICENUM))

and my VB was:

If Me.WOTextBox.Text <> "" Then
            WORKORDERNUMSEARCH = Me.WOTextBox.Text
        Else
            WORKORDERNUMSEARCH = 0
        End If
 
        If Me.SCServiceTextBox.Text <> "" Then
            SCSERVICENUM = Me.SCServiceTextBox.Text
        Else
            SCSERVICENUM = 0
        End If

This is all for a Search Form. This was working fine... till you start putting in large numbers in the Textboxes to search. Then it was error "Integer Column Overflow"... the math was too large or something. So Now I am trying to set the variables null and change the SQL a little to see if it can handle the math better