Vb Function returning null

28,814

Solution 1

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

Solution 2

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

Solution 3

Only if it is defined as returning a nullable integer.

Share:
28,814
Admin
Author by

Admin

Updated on May 28, 2020

Comments

  • Admin
    Admin about 4 years

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

  • MarkJ
    MarkJ about 15 years
    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?
  • JaredPar
    JaredPar about 15 years
    @MarkJ, you are correct. There is discernible difference between return 0 and return Nothing.