質問

C#の ?? 演算子に相当するVB.NETはありますか?

役に立ちましたか?

解決

2つの引数を指定して If()演算子を使用します( Microsoftのドキュメント):

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again. 
Console.WriteLine(If(first, second))

first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

他のヒント

IF()演算子はあなたのためにトリックを行うべきです:

value = If(nullable, defaultValueIfNull)

http://visualstudiomagazine.com/listings/list.aspx?id=252

受け入れられた答えには説明がなく、単なるリンクです。
したがって、 If 演算子がどのように動作するかを説明する回答をMSDNから取っておくと思いました。


If演算子(Visual Basic)

  

短絡評価を使用して、条件付きで2つのうちの1つを返します   値。 If 演算子は、3つの引数または2つの引数で呼び出すことができます   引数。

     
If( [argument1,] argument2, argument3 )


2つの引数で演算子が呼び出された場合

  

If の最初の引数は省略できます。これにより、オペレータが   2つの引数のみを使用して呼び出されます。次のリストが適用されます    If 演算子が2つの引数で呼び出された場合のみ。


パーツ

Term         Definition
----         ----------

argument2    Required. Object. Must be a reference or nullable type. 
             Evaluated and returned when it evaluates to anything 
             other than Nothing.

argument3    Required. Object.
             Evaluated and returned if argument2 evaluates to Nothing.


  

ブール引数を省略する場合、最初の引数は   参照またはNULL入力可能型。最初の引数の評価が   なし、2番目の引数の値が返されます。他のすべての場合、最初の引数の値が返されます。の   次の例は、この評価の仕組みを示しています。


VB

' Variable first is a nullable type. 
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing 
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

3つ以上の値を処理する方法の例(ネストされた if s):

Dim first? As Integer = Nothing
Dim second? As Integer = Nothing
Dim third? As Integer = 6
' The LAST parameter doesn't have to be nullable.
'Alternative: Dim third As Integer = 6

' Writes "6", because the first two values are "Nothing".
Console.WriteLine(If(first, If(second, third)))

拡張メソッドを使用できます。これはSQL COALESCE のように動作し、おそらくテストしようとしているものに対してはやり過ぎですが、動作します。

    ''' <summary>
    ''' Returns the first non-null T based on a collection of the root object and the args.
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="args"></param>
    ''' <returns></returns>
    ''' <remarks>Usage
    ''' Dim val as String = "MyVal"
    ''' Dim result as String = val.Coalesce(String.Empty)
    ''' *** returns "MyVal"
    '''
    ''' val = Nothing
    ''' result = val.Coalesce(String.Empty, "MyVal", "YourVal")
    ''' *** returns String.Empty
    '''
    ''' </remarks>
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray args() As T) As T

        If obj IsNot Nothing Then
            Return obj
        End If

        Dim arg As T
        For Each arg In args
            If arg IsNot Nothing Then
                Return arg
            End If
        Next

        Return Nothing

    End Function

組み込みの If(nullable、secondChoice)は、 2つのNULL可能選択肢のみを処理できます。ここでは、必要な数のパラメーターを Coalesce できます。最初のnull以外のものが返され、残りのパラメーターはその後評価されません( AndAlso / &amp;&amp; OrElse / ||

これらのソリューションのほとんどの重要な制限の1つは、短絡しないことです。したがって、実際には??

と同等ではありません

組み込みの&quot; if&quot;演算子、以前のパラメーターが何も評価しない限り、後続のパラメーターを評価しません。

次のステートメントは同等です。

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;

VB

dim value = if(exression1,if(expression2,if(expression3,expression4)))

これは、&quot; ??&quot;のすべての場合に機能します。動作します。他のソリューションは、実行時のバグを簡単に導入する可能性があるため、細心の注意を払って使用する必要があります。

If演算子(Visual Basic)に関するMicrosoftのドキュメントをここで確認してください: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator

If( [argument1,] argument2, argument3 )

ここにいくつかの例があります(VB.Net)

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top