问题描述
我收到以下警告以下功能。我理解
这意味着什么,但我如何处理空引用?然后我如何传递
结果值?
问候
警告1功能''Dec2hms''并没有在所有代码路径上返回值。使用结果时,可能会在运行时发生
null引用异常。
G:\ Project Development \ Visual Studio $
2005 \Projects\Ascension\Ascension\SwephConversions .vb 64 3 Ascension
''将小数小时转换为小时/分钟/秒
公共职能Dec2hms(ByVal x As Decimal)As String
Dim hh As Int32,mm As Int32,ss As Decimal,余数为十进制
' 'Dim xa十进制,hh为整数
hh = CType(x,整数)
余数=(x - hh)
mm = CType((余数* 60),整数)
余数=((余数* 60) - mm)
ss = Int(余数* 60)
余数=((余数* 60) - ss)
如果余数> = 0.5然后
ss = ss + 1
否则
ss = ss
结束如果
hms = hh& h &安培; mm& m &安培; ss& " s"
结束功能
我不知道你在VB中有多少背景。我会假设不多。
澄清:
功能''Dec2hms''并不会在所有代码路径上返回值。意味着
一个函数可能没有沿着其中一个可执行的执行路径返回一个值,而是一行调用
$ b的代码$ b函数可以从函数中获取空指针。在VB和VB.NET中,
Sub(子例程)是一个永远不会返回值的过程。 A
函数是一个必须返回一个值的过程。
要从函数返回一个值,请使用关键字Return后跟
正在退回的价值或变量。
配售
返回hh& h &安培; mm& m &安培; ss&函数结尾处的's"
(右上方的结束函数)应解决
问题。
我不知道你在VB中有多少背景。我会假设不多。
澄清:
功能''Dec2hms''并不会在所有代码路径上返回值。意味着
一个函数可能没有沿着其中一个可执行的执行路径返回一个值,而是一行调用
$ b的代码$ b函数可以从函数中获取空指针。在VB和VB.NET中,
Sub(子例程)是一个永远不会返回值的过程。 A
函数是一个必须返回一个值的过程。
要从函数返回一个值,请使用关键字Return后跟
正在退回的价值或变量。
配售
返回hh& h &安培; mm& m &安培; ss&函数结尾处的's"
(右上方的结束函数)应解决
问题。
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
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.
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.
这篇关于功能警告 - 空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!