11

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

1
  • Please post the function declaration Commented Apr 3, 2009 at 9:02

3 Answers 3

22

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
Sign up to request clarification or add additional context in comments.

2 Comments

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, you are correct. There is discernible difference between return 0 and return Nothing.
8

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

Comments

2

Only if it is defined as returning a nullable integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.