VB.NET 类似于 SQL 中可用的“ISNULL”函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24152757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:41:06  来源:igfitidea点击:

VB.NET similar function to "ISNULL" available in SQL?

.netvb.netvisual-studio

提问by D. Caan

I have this in my VB.NET code:

我的 VB.NET 代码中有这个:

Dim myValue as String
myValue = ""
Dim myDblValue as Double
myDblValue CDbl(Val(myValue))

If myValuestring is null[like in this example], I'd like to set the value to a predefined value for example 999. I know in SQL I can use ISNULLfor that. Is there any equivalent in VB.NET? If not, how can it be done with available functions?

如果myValuestring 是null[like in this example],我想将该值设置为预定义的值,例如999。我知道在 SQL 中我可以使用ISNULL它。VB.NET 中是否有任何等价物?如果没有,如何使用可用功能来完成?

回答by Gridly

To directly answer your question, IF operator is the nearest to ISNULL when used with the String.IsNull method.

为了直接回答您的问题,IF 运算符与 String.IsNull 方法一起使用时最接近 ISNULL。

Like this

像这样

    Dim myValue As String
    myValue = ""
    Dim myDblValue As Double

    Dim myDefaultValue As Double = 10.1 ' what ever you need

    myDblValue = If(String.IsNullOrEmpty(myValue), myDefaultValue, CDbl(myValue))

If you need to worry about invalid text in myValue then you can use the Double.TryParse method like this.

如果您需要担心 myValue 中的无效文本,那么您可以像这样使用 Double.TryParse 方法。

    Dim myValue As String
    myValue = ""
    Dim myDblValue As Double

    Dim myDefaultValue As Double = 10.1 ' what ever you need

    If String.IsNullOrEmpty(myValue) OrElse Not Double.TryParse(myValue, myDblValue) Then
        myDblValue = myDefaultValue
    End If

回答by Mark C.

If YourVariable.IsNullOrEmpty Then
     myValue = DoWhateverYouWant
Else
     DoOtherStuff
End If

回答by Mark Hall

You can use the String Methods IsNullOrEmptyor IsNullOrWhiteSpaceto create you own function something like this.

您可以使用字符串方法IsNullOrEmptyIsNullOrWhiteSpace像这样创建自己的函数。

Public Function TestNull(value As String, defaultValue As String) as String
    If String.IsNullOrEmpty(value) Then
        Return defaultValue
    Else
        Return value
    End If
End Function

Usuage:

用法:

myDblValue = CDbl(Val(TestNull(myValue, "999")))

Or since you are converting to a double you can just use the Double.TryParse Method it will fail if it can't be converted for any reason

或者,由于您要转换为双精度,因此您可以使用 Double.TryParse 方法,如果由于任何原因无法转换,它将失败

Public Function TestValidDecimal(value As String, defaultValue As Double) As Double
    Dim temp As Double
    If Double.TryParse(value, temp) Then
        Return temp
    Else
        Return defaultValue
    End If
End Function

回答by Tom

myDblValue = If(myValue = Nothing, 999, CDbl(Val(myValue)))

myDblValue = If(myValue = Nothing, 999, CDbl(Val(myValue)))

OR

或者

myDblValue = If(myValue = "", 999, CDbl(Val(myValue)))

myDblValue = If(myValue = "", 999, CDbl(Val(myValue)))

SEE:

看:

http://msdn.microsoft.com/en-us/library/bb513985(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/bb513985(v=vs.90).aspx