क्या C # के ??
ऑपरेटर के लिए VB.NET बराबर है ?
क्या C # के ??
ऑपरेटर के लिए VB.NET बराबर है ?
जवाबों:
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()
VB में कथन if...?...:
C # के समान है, ??
ऑपरेटर नहीं
??
(इस प्रश्न का एक और उत्तर देखें: stackoverflow.com/a/20686360/1474939 )
If
तीन मापदंडों के साथ वीबी का उपयोग कैसे करें । यह C # के ऑपरेटर के समान नहीं है??
। दो तर्कों के साथ बेहतर जवाब है कोड मैवरिक इफ । (निक का एक समान उत्तर था, वर्षों पहले, लेकिन MSDN से स्पष्टीकरण शामिल नहीं करता है।)
IF()
ऑपरेटर आप के लिए चाल करना चाहिए:
value = If(nullable, defaultValueIfNull)
स्वीकृत जवाब में कोई स्पष्टीकरण नहीं है और यह केवल एक लिंक है।
इसलिए, मैंने सोचा कि मैं एक उत्तर छोड़ दूंगा जो बताता है कि If
ऑपरेटर MSDN से कैसे काम करता है:
सशर्त रूप से दो मानों में से एक को वापस करने के लिए शॉर्ट-सर्किट मूल्यांकन का उपयोग करता है। यदि ऑपरेटर तीन तर्क के साथ या दो तर्क के साथ कहा जा सकता है।
If( [argument1,] argument2, argument3 )
If करने के लिए पहला तर्क छोड़ा जा सकता है। यह ऑपरेटर को केवल दो तर्कों का उपयोग करके कॉल करने में सक्षम बनाता है। निम्न सूची लागू होता है जब यदि ऑपरेटर दो तर्कों कहा जाता है।
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.
जब बूलियन तर्क को छोड़ दिया जाता है, तो पहला तर्क एक संदर्भ या अशक्त प्रकार होना चाहिए। यदि पहला तर्क कुछ भी नहीं का मूल्यांकन करता है , तो दूसरे तर्क का मान वापस आ जाता है। अन्य सभी मामलों में, पहले तर्क का मान लौटाया जाता है। निम्न उदाहरण दिखाता है कि यह मूल्यांकन कैसे काम करता है।
' 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))
दो से अधिक मानों को कैसे प्रबंधित किया जाए (nested 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)))
आप एक एक्सटेंशन विधि का उपयोग कर सकते हैं। यह एक एसक्यूएल की तरह काम करता है 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)
केवल दो अशक्त विकल्प को संभाल सकता है । यहाँ, एक Coalesce
वांछित के रूप में कई मापदंडों हो सकता है । पहले गैर-शून्य से एक वापस कर दी जाएगी, और पैरामीटर के बाकी उसके बाद (शॉर्ट सर्किट, जैसे मूल्यांकन नहीं किया जाता AndAlso
/ &&
और OrElse
/ ||
)
Return args.FirstOrDefault(Function(arg) arg IsNot Nothing)
:-)
इनमें से अधिकांश समाधानों की एक महत्वपूर्ण सीमा यह है कि वे शॉर्ट-सर्किट नहीं करेंगे। इसलिए वे वास्तव में इसके समकक्ष नहीं हैं ??
।
अंतर्निहित If
ऑपरेटर बाद के मापदंडों का मूल्यांकन नहीं करेगा जब तक कि पहले वाला पैरामीटर कुछ भी नहीं का मूल्यांकन करता है।
निम्न कथन समतुल्य हैं:
सी#
var value = expression1 ?? expression2 ?? expression3 ?? expression4;
वीबी
dim value = if(expression1,if(expression2,if(expression3,expression4)))
यह उन सभी मामलों में काम करेगा जहां ??
काम करता है। किसी भी अन्य समाधान को अत्यधिक सावधानी के साथ उपयोग करना होगा, क्योंकि वे आसानी से रन-टाइम बग का परिचय दे सकते हैं।
यदि ऑपरेटर (विजुअल बेसिक) के बारे में 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"))