Search code examples
stringvb.netconcatenation

Condition Inside string Concatenation VB.NET


In PHP you can have conditions (if/else) inside a string concatenation.

$string= 'X is' . ($x >0 1 ? ' > 10 ': ' < 10 ')';

Is this same thing possible in VB.NET?


Solution

  • You can use string inpterpolation and the If-operator:

    Dim result = $"X is {If(x > 10, " > 10 ", " <= 10 ")}"
    

    Which is syntactic sugar for String.Format:

    Dim result = String.Format("X is {0}", If(x > 10, " > 10 ", " <= 10 "))