Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vb Function returning null

Tags:

null

vb.net

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

like image 888
mat690 Avatar asked Apr 03 '09 08:04

mat690


People also ask

What does returning NULL do?

Returning null Creates More Work A function that returns a null reference achieves neither goal. Returning null is like throwing a time bomb into the software. Other code must a guard against null with if and else statements. These extra statements add more complexity to the software.

Is returning NULL good practice?

Returning Null is Bad Practice The FirstOrDefault method silently returns null if no order is found in the database. There are a couple of problems here: Callers of GetOrder method must implement null reference checking to avoid getting a NullReferenceException when accessing Order class members.

Is Null in visual basic?

The Null value indicates that the Variant contains no valid data. Null is not the same as Empty, which indicates that a variable has not yet been initialized. It's also not the same as a zero-length string (""), which is sometimes referred to as a null string.

Is null in VB net?

What is VB.NET null ? A null value is a value that doesnt refer to any object. Strings are reference types and can be equal to the null value like any other reference type. VB.NET uses the keyword Nothing for null values.


2 Answers

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
like image 120
JaredPar Avatar answered Nov 16 '22 04:11

JaredPar


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

like image 22
dommer Avatar answered Nov 16 '22 03:11

dommer