IsNull in VB.net

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
I am sorry I am new to vb.net I am working on some code written in C# I check for some variables if they are null. In vb I used to use variable = vbNull in C# I am using IsNull(Variable) but vb.net doesnt have the IsNull. Instead the IDE tells me to use IsDBNull is this same as IsNull? Or whats better to use?

Second question I have is does vb.net have a ternary operator?

(condition?"if true":"if false")
 
You have two options:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsDBNull(cmd.ExecuteScalar) = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]{...}
[/COLOR][/SIZE]
or
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cmd.ExecuteScalar [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] DBNull.Value = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then [COLOR=darkgreen]'you can save up to 40% of the resources in this way[/COLOR][/COLOR][/SIZE]
[COLOR=#006400]{...}[/COLOR]

Regards ;)
 
I noticed that you are not satisfied with the answer so i will try to add couple words yet. Yes isDBNull is the same with its C# equivalent isNull when you check for values fetched from DB.
i.e.

VB.NET:
VB.NET:
If not IsDBNull(dr("somefield") Then 
myValue = dr("somefield") 
End If

C# .NET:
VB.NET:
if (!dr.IsNull("somefield"){ 
myValue = dr["somefield"].ToString(); 
}

Regards ;)

btw, if you try to use isNULL with VB.NET ... vs.net intellisense will tell you that IsNull is not supported and to use DBNull instead.
 
Moved to more appropriate forum.

You can also use:
VB.NET:
If TypeOf object Is DBNull Then
VB.NET has no ternary operator. You can use the IIf function but it is a function and not an operator, so it is similar but not the same. The first difference is that it is not as efficient because it involves a function call. The second, more important difference is that both the True and False conditions are evaluated in all cases, while the C# operator does not evaluate the False condition if the expression is True. Forgetting this in VB.NET can lead to unexpected results or unhandled exceptions in some circumstances.
 
Back
Top