-3

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

Migol
  • 8,161
  • 8
  • 47
  • 69
web dunia
  • 9,381
  • 18
  • 52
  • 64
  • 7
    Why do you want to remove the if/else? – Ant P Apr 07 '14 at 12:28
  • I want to avoid if else condition since I have two much validations like this for code cleaner I want to avoid – web dunia Apr 07 '14 at 12:31
  • 2
    This doesn't look like C# – Thomas Weller Apr 07 '14 at 12:32
  • Then write this in a function and send `a` value to it – Batu.Khan Apr 07 '14 at 12:32
  • @webdunia: Most likely then you would have to do some redesign. Sometimes you can avoid checks like these by employing various patterns or polymorphism. But really impossible to say without more context. Maybe you could create an extension method (`static void IfNull(this object obj, Action doThis, Action elseDoThis)`) so your calling code might look like: `a.IfNull(Callb, Callc);` but I'm not sure if that's an improvement or not for you. – Chris Sinclair Apr 07 '14 at 12:34
  • I know to have write in function but to avoid if else and function I need some other one liner alternative – web dunia Apr 07 '14 at 12:34
  • 1
    We can't be specific without knowing more about the code, but in general this structure is clear and acceptable. If you want to remove a lot of duplication of this structure, refactoring it into a single component would be how you accomplish that. But that refactoring depends on more information regarding your design than you've presented here. – David Apr 07 '14 at 12:35
  • 4
    @webdunia: Don't "avoid" conditional blocks. They're a basic component of the language and are used to express, well, conditional behavior. Write code clearly and expressively, instead of trying to be really clever just to minimize keystrokes. You'll find that the latter is *much* more difficult to support. – David Apr 07 '14 at 12:36
  • You haven't `accepted answers` to previous questions in a couple of months. Please go back and accept the answers that solved your problem. That helps other users of the community, and google searchers -- anyone searching for a solution, to questions like yours -- to find solution(s) that **SOLVE** their issue. Thank you. – Doug_Ivison May 15 '14 at 14:32

2 Answers2

4

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 :)

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
0

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.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34