In VB.NET I have a if else block like this
If a is null
Callb()
Else
Callc()
Both methods doesn't return any thing void I want to remove the if else block here
Is there easy way to write in one single liner code for this
In VB.NET I have a if else block like this
If a is null
Callb()
Else
Callc()
Both methods doesn't return any thing void I want to remove the if else block here
Is there easy way to write in one single liner code for this
Just use a specific operation that does that:
public void CallFunction(object a)
{
if (a == null)
Callb()
else
Callc()
}
Then you can call this in you validation routines:
CallFunction(s);
Also, have a look here, which is exactly about what you are asking here and where some answers are provided. But to me they all seem overkill and so much trouble. There is nothing wrong with if/else
, keep it simple.
EDIT: I see you changed the tag from C# to VB.NET, but I'm sure you can convert the C# code above :)
L-Three's answer is a good one, and the code as originally posted is fine, too. If you really wanted to get cute with it and make it a one-liner, you can do this:
First, make CallA
and CallB
return something. Literally anything (you don't have to actually use what it returns).
Then, you can have a call like this:
Dim foo As Object = If(someCondition, CallA(), CallB())
Again, you don't have to actually do anything with this variable.
I agree with the other posts about the intent being clearer when you just keep the If/Else
block. The code I posted is shorter, but your coworkers will hate you after you write enough stuff like this. I wouldn't recommend making a habit of it.