Re: Function Warning - Null Reference



Hi Stephany,

Excellent explanation, this makes sense to me;

If value Then
Return "Hello World!"
Else
Return String.Empty
End If

Thanks, regards
Terry

"Stephany Young" <noone@localhost> wrote in message
news:en2ivfeNHHA.5012@xxxxxxxxxxxxxxxxxxxxxxx
First of all, let's get some terminology straight. The warning states:

Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. A
null reference exception could occur at run time when the result is used.

Nowhere does it mention 'null pointer', it mentions 'null reference' and
that is a completely different animal.

For the sake of your own sanity it is important to remember that, for the
purpose of your learning curve, 'VB doesn't do pointers'.

Without going into the why's and wherefore's, some types in .NET are
called value types. These include String, Integer, DateTime and Boolean
among others. These are usually implemented as a Structure rather than as
Class. A type implemented as a Class is usually called a reference type.

In general, you can assing Nothing to a variable of a reference type and
that variable is considered to be unassigned.

When you assign Nothing to a variable of a value type, then, behind the
scenes, the default value for that particular value type is substituted.
The default value for a String is String.Empty, the default value for an
Integer is 0, the default value for a DateTime is 01/01/0001, the default
value for a Boolean is False and so on and so forth.

In VBA, you would have written a function something like:

Function Test() As String

Test = "Hello World!"

End Function

and you called it using something like:

x = Test()

The single statement in the function assigned a string as the return value
of the function. It used a syntax where you assigned the value to the name
of the function.


If you had coded the function as:

Function Test(ByVal flag As Boolean) As String

If value Then Test = "Hello World!"

End Function

Then the value "Hello World!" would be returned only if the parameter
(flag) had a value of True. If it had a value of false then, the function
'fell through the logic' or 'took another path' and, because the type of
the the return value was declared as string, an empty string was
automaticallty returned.

In VB.NET, exactly the same thing applies, BUT, at compile time, you get a
warning that, at least one of the paths through the logic does not
explicity set a return value.

To avoid this warning, I recommend that you get into the habit of always
explicity setting return values from functions, like:

Function Test(ByVal flag As Boolean) As String

If value Then
Return "Hello World!"
Else
Return String.Empty
End If

End Function

Note that in Vb.Net, we assign the return value via the keyword 'Return'
rather than assigning to the function name. It is functionally equivalient
to, but probably more efficient:

Test = "Hello World!"
Exit Sub

The real difference is that 'Return <value>' assigns the return value and
returns immediately, all with a single statement.


"Terry" <new-grps@xxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23eA2w$dNHHA.4604@xxxxxxxxxxxxxxxxxxxxxxx
Thanks Stephany,

I understand your explanation and what the warning is indicating. In
order to remove the warning I would need to ensure the function does not
return a null pointer. Is there a better syntax available giving a more
complete solution?

My experience comes from VBA and I have just started out in VB and .NET.
The structure and syntax is what I am trying to understand. The best way
for me is to have some good books, try converting some VBA code and visit
these newsgroups. Just wished I started 3 years ago.

Regards

<lord.zoltar@xxxxxxxxx> wrote in message
news:1168556090.645918.315190@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Stephany Young wrote:
Return hh & "h " & mm & "m " & ss & "s"


I don't know how much background you have in VB. I'll assume not much.
To clarify:
"Function 'Dec2hms' doesn't return a value on all code paths." means
that a function might not be returning a value along one of its
possible paths of execution, and that a line of code that calls the
function could get a Null Pointer from the function. In VB and VB.NET,
A Sub (Subroutine) is a procedure which never returns a value. A
Function is a procedure that MUST return a value.
To return a value from a function, use the keyword Return followed by
the value or variable that is being returned.
Placing
Return hh & "h " & mm & "m " & ss & "s"
at the end of the function (right above "end function") should fix the
problem.







.



Relevant Pages

  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... null reference exception could occur at run time when the result is used. ... default value for a String is String.Empty, the default value for an Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Visual Studio 2005 - Warning in the Error List Window
    ... I'm receiving the same warning, and although this has been brought up ... Public sBillingAddress1 As String ... Private Sub LoadAddress() ... Dim Address As New addressEntry ...
    (microsoft.public.dotnet.languages.vb)
  • Re: PROBLEM
    ... tmp.c:1: warning: initialization discards qualifiers from pointer ... The problem with C compilers is that these options are not enabled ... Compiling new C code in a mode that flags possible writes to string ... become illegal if the language were modified to make string literals ...
    (comp.std.c)
  • Re: SetFocus convert from 2003 to 2005 problem
    ... bruce barker wrote: ... Private Sub SetFocus ... Warning 2 'Public Overridable Sub RegisterStartupScript(key As String, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SetFocus convert from 2003 to 2005 problem
    ... vs2005 has this feature builtin. ... Private Sub SetFocus ... Warning 2 'Public Overridable Sub RegisterStartupScript(key As String, ...
    (microsoft.public.dotnet.framework.aspnet)