Re: Function Warning - Null Reference

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 2007-01-12, Terry <news-grps@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I am getting the following warning for the below function. I understand what
it means but how do I handle a null reference? Then how do I pass the
resulting value?

Regards



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.
G:\Project Development\Visual Studio
2005\Projects\Ascension\Ascension\SwephConversions.vb 64 3 Ascension


' Convert decimal hours to hours/minutes/seconds

Public Function Dec2hms(ByVal x As Decimal) As String

Dim hh As Int32, mm As Int32, ss As Decimal, remainder As Decimal

'Dim x a decimal, hh as integer

hh = CType(x, Integer)

remainder = (x - hh)

mm = CType((remainder * 60), Integer)

remainder = ((remainder * 60) - mm)

ss = Int(remainder * 60)

remainder = ((remainder * 60) - ss)

If remainder >= 0.5 Then

ss = ss + 1

Else

ss = ss

End If

hms = hh & "h " & mm & "m " & ss & "s"

End Function

The problem is that you are not returning a value at all that I can see.
Which means that the value of the function will always be a null reference.
There are two, ways to return a value from a function in vb.net. One, is the
same as the old vb way - and that is to assign the value to the function name:


Public Function Dec2hms As String
....

Dec2hms = hh & "h " & mm & "m " & ss & "s"
End Function

The other, prefered way is to use the Return statment:

Public Function Dec2hms As String
....

Return hh & "h " & mm & "m " & ss & "s"
End Function

Now, to avoid these kind of warnings, you just need to make sure that there is
a return along all normal code paths.... I personally like to structure code
so that there is only one exit point, so that often leads to code that looks
something like:

Public Function SomeFunc As Boolean
' i chose to default to a failure, but that
' can change based on what is easier and makes
' more sense for the method
Dim success As Boolean = False

' do cool stuff that might change the value of success

Return success

End Function

HTH,

--
Tom Shelton
.



Relevant Pages

  • Re: Writing Access functions
    ... passing values by Value or by Reference. ... Public Function NameFromIDAs String ... If you pass a control reference: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Dynamically load functions
    ... $includeMap should contain 1 entry for each function I wish to reference, ... Is the function 'call_user_func_array' the actual system function calling routine? ... public function __call{ ...
    (comp.lang.php)
  • RE: Run-time Error 2186
    ... I should also point out that there are other buttons that also reference the ... When I obtain the "Run-time Error 2186" and hit debug, ... Public Function Manufacturer() As String ...
    (microsoft.public.access.forms)
  • Re: Call Private Function from ThisWorkbook module
    ... The way you describe is how you reference a Public function in a class ... Bob Phillips ... >> Beto, ...
    (microsoft.public.excel.programming)