This question already has answers here:
Is there a conditional ternary operator in VB.NET?
(5 answers)
Closed 9 years ago.
What is the VB.NET equivalent of the C# ? operator?
For example, how would the following code be written in VB.NET?
hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP
Historically, IIf was commonly used for that - but that does not use short-circuiting so is not quite the same. However, there is now a 3-part If:
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
that does use short-circuiting, and thus is identical to the conditional operator in C#.
You can use the If operator
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
Try using the If function like so:
x = If(condition, trueValue, falseValue)
This question is a duplicate of a question that has already been asked and answered:
Is there a conditional ternary operator in VB.NET?
here:
Dim foo as String = If(bar = buz, cat, dog)
Related
This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.
This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 5 years ago.
I've encountered this operator in C# when dealing with custom events: MyEvent?.Invoke(this, new EventArgs());. What is the purpose of the ?. portion of this statement?
It's making sure it's not null. They have these in swift and their called optionals. If the variable is null then it returns null
This question already has answers here:
What does the tilde mean in an expression? [duplicate]
(6 answers)
Closed 7 years ago.
intStyle = intStyle & ~(WS_MINIMIZE);
It is the first time I see this , I am trying to learn how to hook low lvl APIs to C# and make some calls , and I do not understand what this line means. Thank you guys!
It's an operation on a flag. You need to understand bitoperations (AND, OR, NOT, XOR..) for that. This line deletes the flag WS_MINIMIZE from the intStyle flagmask. More reading: Using Bitwise operators on flags , http://www.codeproject.com/Articles/13740/The-Beginner-s-Guide-to-Using-Enum-Flags.
See this for the & operator.
And this for the ~ operator
They are bitwise operators. The first one is a bitwise AND. The second one performs a bitwise complement operation.
This is a bitwise operation.
See for example http://www.codeproject.com/Articles/544990/Understand-how-bitwise-operators-work-Csharp-and-V
This question already has answers here:
What does |= (single pipe equal) and &=(single ampersand equal) mean
(3 answers)
Closed 7 years ago.
Just walked through referencesource.microsoft.com (line 217)
and discovered "|=" operator. What does it means? I assume it some sort of Boolean operation- but can't get what exactly it means. Does it mean "or equal", short form like "a= a|b" ?
|= is to = what += is to =. Just a shortcut to avoid the writing of a = a | b
its a bitwise inclusive OR and assignment operator.
For details refer
https://msdn.microsoft.com/en-us/library/ms173224.aspx
You are correct. This operator is known as the "OR assignment" operator and a |= b is equivalent to a = a | b. Here is the
documentation.
This question already has answers here:
Method Call using Ternary Operator
(8 answers)
Closed 8 years ago.
I want to convert this if, else-if, can someone help me out please?
if (condition1)
response.Redirect(" some link");
else if (condition2)
response.Redirect("link 2");
I want convert above statement,but showing error at the end, required ":". Any other way i can use this?
LinkPurchase.PostBackUrl =((Condition)?string.Format("some link"):
(condition2)?string.Format("link 2));
You cannot rewrite that to the ?: operator.
You have an if-else if, not just an if-else.
Besides, you do not pick up the return values from the Redirect calls.
The usual case where you do want to rewrite to the ?: operator is:
if (condition)
something = Abc();
else
something = Xyz();
where it is natural to use instead:
something = condition ? Abc() : Xyz();
You cannot do this.
There are other answers here that inform you that the ?: operator needs the "else" part, so yes, the first problem is that you're missing that.
However, Response.Redirect doesn't return anything, so you cannot do this even with the else part.
?: is an expression, you cannot write statements (easily) with this.
Stick with the if-statements.