Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么函数将NULL视为0_Vb.net_Visual Studio - Fatal编程技术网

Vb.net 为什么函数将NULL视为0

Vb.net 为什么函数将NULL视为0,vb.net,visual-studio,Vb.net,Visual Studio,我得到了这个函数: Public Function parseSex(ByVal sex As Object) As String if Not Nothing then if sex = 1 Then Return "Male" elseif sex = 0 Then Return "Female" else Return "N/D" End if Else Return "

我得到了这个函数:

Public Function parseSex(ByVal sex As Object) As String
if Not Nothing then
      if sex = 1 Then
         Return "Male"  
      elseif sex = 0 Then
         Return "Female"
      else
         Return "N/D"
      End if
  Else
     Return "N/D"
  End If
End Function
数据库可以返回0、1或NULL。
但是,如果此函数从数据库接收空值,则输出阴性,
为什么会发生这种情况?如何解决


我真的需要将其传递为NULL,0或1

您必须将
sex
作为对象传递吗?为什么不能将其作为
整数传递?
像这样:

Public Function parseSex(ByVal sex As Integer) As String
 if Not Nothing then
 if sex = 1 Then
   Return "Male"   
 elseif sex = 0 Then
   Return "Female"
 else
  Return "N/D"
  End if
  Else
  Return "N/D"
 End If
End Function

或者数据库中该字段的数据类型是什么?

我将按如下方式重构您的方法,将参数替换为可为空的
整数(即
整数?
),而不是
对象
,并将返回类型替换为
Sex
枚举:

Public Enum Sex
    Male
    Female
    NotDefined
End Enum

Public Function ParseSex(ByVal sex As Integer?) As Sex
    If (sex.HasValue) Then
        Select Case sex.Value
            Case 1
                Return Sex.Male
            Case 0
                Return Sex.Female
            Case Else
                Return Sex.NotDefined
        End Select
    Else
        Return Sex.NotDefined
    End If
End Function
如果需要从返回值中获取字符串,只需对其调用
ToString()

在您拥有的代码中,您的行
如果不是空的话,
将始终返回true。把它放在那里是没有意义的。另外,对于方法arg使用
对象
类型也不是一个好主意,因为您可以传递任何类型的对象,但是您假设它总是一个整数,并且不进行检查;因此,可能会出现例外情况

我还想指出一些其他事情来帮助您提高:

  • 确保遵循正确的命名约定。方法名称应为大写(Pascal case),例如
    parsex
    not
    parsex
  • 始终正确缩进代码以使其更易于阅读。每个嵌套的“块”都应该相对于包含它的块缩进
  • 为你的变量使用最具体的类型;除非绝对必要,否则切勿使用
    对象类型

  • 我很确定你应该在这里检查DBNull

    Public Function parseSex(ByVal sex as integer) as string
    if not isDBNull(sex) Then
       if sex = 1 then
          return "Male"
       else
          return "Female"
       end if
       return "N/D"
    End If
    

    因此,如果传递null,则返回N/D,否则将运行if条件以查看是否需要返回Male或Male。

    它是哪一个?VBA或VB.NET。相应地贴上标签。此外,microsoft reporting标记是不相关的,它甚至不会按原样编译。修复您的代码,并正确缩进,以便我们可以阅读。@roryap为什么它不能编译?PD:我无法测试此代码,它的报告/代码选项卡部分仍然不起作用,仍然将0视为女性,并且数据库中的yea是tinyInt