功能警告 - 空引用 [英] Function Warning - Null Reference

查看:59
本文介绍了功能警告 - 空引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下警告以下功能。我理解

这意味着什么,但我如何处理空引用?然后我如何传递

结果值?


问候


警告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"


结束功能

解决方案



返回hh& ; h &安培; mm& m &安培; ss& s>

" Terry" < ne ******* @ whiteHYPHENlightDOTme.ukwrote in message

news:uQ ************** @ TK2MSFTNGP02.phx.gbl ...


>我收到以下函数的以下警告。我明白这意味着什么,但我如何处理空引用?然后我如何传递
结果值?


问候


警告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 decimal,hh as integer


hh = CType(x,Integer)


余数=(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"


结束功能





Stephany Young写道:


返回hh& h &安培; mm& m &安培; ss& " s"



我不知道你在VB中有多少背景。我会假设不多。

澄清:

功能''Dec2hms''并不会在所有代码路径上返回值。意味着

一个函数可能没有沿着其中一个可执行的执行路径返回一个值,而是一行调用
$ b的代码$ b函数可以从函数中获取空指针。在VB和VB.NET中,

Sub(子例程)是一个永远不会返回值的过程。 A

函数是一个必须返回一个值的过程。

要从函数返回一个值,请使用关键字Return后跟

正在退回的价值或变量。

配售

返回hh& h &安培; mm& m &安培; ss&函数结尾处的's"

(右上方的结束函数)应解决

问题。


谢谢Stephany,


我理解你的解释以及警告的内容。为了取消警告,我需要确保函数不返回

空指针。是否有更好的语法可以提供更完整的

解决方案?


我的经验来自VBA,我刚刚开始使用VB和.NET。

结构和语法是我想要了解的。对我来说最好的方式

是要有一些好书,尝试转换一些VBA代码并访问这些

新闻组。我希望自己3年前开始。


问候


< lo ********* @ gmail.comwrote in消息

news:11 ********************** @ k58g2000hse.googlegr oups.com ...


>

Stephany Young写道:


>返回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

解决方案


Return hh & "h " & mm & "m " & ss & "s"
"Terry" <ne*******@whiteHYPHENlightDOTme.ukwrote in message
news:uQ**************@TK2MSFTNGP02.phx.gbl...

>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




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.


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

<lo*********@gmail.comwrote in message
news:11**********************@k58g2000hse.googlegr oups.com...

>
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.



这篇关于功能警告 - 空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆