tags:

views:

1642

answers:

3

is it possible for a VB.net function with a return type of integer to return null ?

+1  A: 

Only if it is defined as returning a nullable integer.

Mitch Wheat
+3  A: 

You'll need a return type of Nullable(Of Integer).

dommer
+1  A: 

If you're strictly talking about a null reference (C#'s version of null) then the answer is No. Both dommer and Mitch have the right idea here. You would have to return a Nullable(OF Integer) in order to communicate the abscence of a value.

However, VB doesn't have a null value. Instead it uses Nothing. Nothing represents the empty value for both value and reference types. It is convertible to any value type and simply represents the equivalent of default(T) in C#. Many people say null when talking about VB but really mean Nothing. If this is the case then yes, you can return Nothing from an Integer returning function

Public Function Example() As Integer
  Return Nothing
End Function
JaredPar
So your Example() function returns the Integer 0, because VB casts Nothing to the default value for an Integer. To the caller, there's no difference between what you've written and return 0. Or am I wrong?
MarkJ
@MarkJ, you are correct. There is discernible difference between return 0 and return Nothing.
JaredPar