if syntax for dbnull and value - vb.net

I need to make something like :
if isdbnull(value) or value = something then
'do something
else
'do something else
end if
of course i get an error using this method , so my question is how do i rewrite it to avoid the "operator not defined for dbnull and something" error ?

There are a few approaches to this, and which you use may depend on the values you're working with. However, if you want something that catches all conditions then I'd do this:
If Value Is Nothing OrElse IsDbNull(value) Then
'do something
Else
'do something else
End If
This will check if the Value is nothing, which can sometimes happen without the value actually being DBNull.
But the most important part of this is the OrElse. OrElse is the short-circuiting operator which terminates the evaluation of the condition as soon as the runtime knows what the outcome will be. By contrast, the Or operator will execute the entire condition no matter what, and that is the reason your original code fails.
EDIT:
Now that I look at your example code again, I can see how my NotNull() function may help you:
Public Shared Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T
If Value Is Nothing OrElse IsDBNull(Value) Then
Return DefaultValue
Else
Return Value
End If
End Function
Usage:
if NotNull(value, something) = something then
'do something
else
'do something else
end if

I tend to use the OrElse, AndAlso operators
If IsDBnull(value) OrElse value = something Then
''#do something
Else
''#do something else
End If
The shortcuts operator means the rest of the If conditions won't be executed if the first is true.
Edit: Steve beat me to it while I was formatting my answer :)

value Is DBNull.Value

value is nothing

Related

Syntax error using IIf

This is a simple question I hope. I learned about IIf today and want to implement it in some cases to save a few lines.
IIF(isnumeric(inputs), resume next, call notnum)
This is in red meaning there is a syntax error, how fix this? I have scanned MSDN article on this so I am not being lazy here.
That's not how the IIf function works.
It's a function, not a statement: you use its return value like you would with any other function - using it for control flow is a bad idea.
At a glance, IIf works a little bit like the ternary operator does in other languages:
string result = (foo == 0 ? "Zero" : "NonZero");
Condition, value if true, value if false, and both possible values are of the same type.
It's for turning this:
If foo = 0
Debug.Print "Zero"
Else
Debug.Print "NonZero"
End If
Into this:
Debug.Print IIf(foo = 0, "Zero", "NonZero")
Be careful though, IIf evaluates both the true and the false parts, so you will not want to have side-effects there. For example, calling the below Foo procedure:
Public Sub Foo()
Debug.Print IIf(IsNumeric(42), A, B)
End Sub
Private Function A() As String
Debug.Print "in A"
A = "A"
End Function
Private Function B() As String
Debug.Print "in B"
B = "B"
End Function
Results in this output:
in A
in B
A
That's why using IIf for control flow isn't ideal. But when the true result and false result arguments are constant expressions, if used judiciously, it can help improve your code's readability by turning If...Else...End If blocks into a simple function call.
Like all good things, best not abuse it.
You cannot use iif like this.
Here is a possible way to solve this:
if isnumeric(input) then
do something ...
else
call notnum
end if
IIf returns a value. As Mat's Mug stated, you can use it, to hand data into another method or function, but you can't call a procedure or function, which has no return value.
IsNumeric() for the conditional is okay, though.

If statement logic - read left to right?

I have an if statement in vb.net that reads something like this:
If String.IsNullOrEmpty(someDate) Or CDate(someDate) > DateTime.Now Then
'Do stuff here
End If
When someDate = Nothing, the app barfs. I could have sworn that these statements read left to right and as soon as it found a matching condition it would enter into the If code block.
I can write another statement that just does a null check. This results in no errors. Could someone clarify this?
They do go from left to right, the problem is you're using Or, which evaluates every condition. You need to use the short circuit version, OrElse:
If String.IsNullOrEmpty(someDate) OrElse CDate(someDate) > DateTime.Now Then
'Do stuff here
End If

Prevent second half of an if statement when first half is null

I have a statement in VB.net that I thought I wrote correctly to prevent the second half from being evaluated. It looks like this:
If ((myDataSet2 IsNot Nothing) Or myDataSet2.Tables("CurData").Rows.Count > 0)
However it does not skip the second expresion "myDataSet2.Tables("CurData").Rows.Count > 0" like I want it to.
What should I change?
Use the OrElse operator.
If myDataSet2 IsNot Nothing OrElse myDataSet2.Tables("CurData").Rows.Count > 0
EDIT: See my comment on your original question. You are PROBABLY looking for:
If myDataSet2 IsNot Nothing AndAlso myDataSet2.Tables("CurData").Rows.Count > 0
That will check if myDataSet2 isn't null. Assuming that it's not, it will then check that there is at least one row. If it is null, then the second condition won't be checked.
You need to put the second statement into the first if-clause.
Like this:
If(statement1) then
If(statemtent2) then
Else
End if
Else
End If
As it is now both are evaluated and if one of them is true the content in your if-clause will be executed.

How to check null value or zero

Using VB.Net
I want to check whether the table row is null or 0
Code
sCmd = New SqlCommand("SELECT * from table1)", conObjects.myConnection)
dReader = sCmd.ExecuteReader
While dReader.Read()
If dReader.Item(11) <> "0" And dReader.Item(11) IsNot Nothing Then
msgbox ("Not NUll or 0")
End if
End while
Showing Error as "Operator '<>' is not defined for type 'DBNull' and string "0""
Why i am getting this error, How to solve this problem.
Need VB.Net Code Help
You should always use AndAlso and OrElse instead of And and Or.
With AndAlso the latter expression is only evaluted if the former returned true. On this way you can avoid exceptions and make your code more efficient.
But apart from that, you should use reader.IsDBNull(index):
If Not dReader.IsDBNull(11) AndAlso dReader.GetString(11) <> "0" Then
End If
There are two problems here: one is that you use And instead of AndAlso: the former will cause your whole line to be evaluated, where the latter will only evaluate the second argument if its first argument is already True.
Your second problem is that you check for "not zero" before you check for "Nothing", and so your first part can fail before the second part is evaluated.
Try this instead:
If dReader.Item(11) IsNot Nothing AndAlso dReader.Item(11) <> "0" Then
Also, it's been a while since I did ADO, but shouldn't you be checking IsDbNull instead of comparing to Nothing? Perhaps VB takes care of that for you, I'm not sure.
Try using AndAlso
If (dReader.Item(11) IsNot Nothing) AndAlso (cint(dReader.Item(11)) <> 0) Then
msgbox ("Not NUll or 0")
End if
basically what it does is, when the first expression is already true, the second expression is not evaluated.
If Convert.ToString(dReader.Item(11)) <> "0" AndAlso dReader.IsDBNull(11)=False

VB.NET logic order in if statement

I'm new in VB.NET, but for C, C++, C# and other languages I had some years of expericences. This problem for me is very weird because I never met it before.
I have this line of code:
If obj is Nothing Or obj.IsDisposed Then
'do some stuffs
End If
This line of code will reveal an error when obj is Nothing because obj.IsDisposed doesn't exist (no handle for it). As what I know, the first statement of Or it returns True so the result of If statement in anycase would be True.
Can anyone give me an instruction how to get rid of this (or I have to write If..Then..Else If..End If)
try OrElse, the obj.Disposed will not be evaluated when "obj is Nothing" is true
If obj is Nothing OrElse obj.IsDisposed Then
'do some stuffs
End If
You can use the OrElse Operator it will bypass the second evaluation if the first is true.
From above Link:
A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure calls.
OrElse is what you need. it will only evaluate on the first evaluation as long as it is already true
If obj is Nothing OrElse obj.IsDisposed Then
'do some stuffs
End If