JL.
JL.

Reputation: 81342

VB.net function returns nothing, yet throws an error during comparison?

In our code base there is a function which should return either an object or nothing.

The exact code is this :

Return Nothing

The calling method looks something like this

myObj = theClass.function()

if myObj <> nothing then // fails

Am I missing something in C# I would use an if not null check, why would this fail in VB.net, and how can I check the return val from the func is not nothing?

Thanks in advance

Upvotes: 1

Views: 351

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 546083

In order to compare references (and Nothing) you need to use Is and IsNot, not = and <>.

That is:

If myObj IsNot Nothing Then ' …

Upvotes: 5

Related Questions