Re: Function Warning - Null Reference



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. ... These include String, Integer, DateTime and Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ref question
    ... without any warning or any indication in the code that it ... variable is a reference to an object. ... Now, that can't happen with a string, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: member variable of STL string class
    ... string foo() ... In the code above you do not return a reference to the local ... copies of local objects from a function. ... rightfully warns you with C4239 warning about this. ...
    (microsoft.public.vc.language)
  • Re: string comparison with ==
    ... > I am looking at some code which in many places performs string comparison ... > using == instead of Equals. ... This results in a reference comparison. ... The C# compiler even gives you a warning about lines YYY and ZZZ: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Linux 2.6.26-rc9
    ... annotate uv_system_init with a matching annotation. ... WARNING: vmlinux.o: Section mismatch in reference from the ...
    (Linux-Kernel)