Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using inline IF statement vb.net

Tags:

Brief info on the code is as follows. The code takes a bunch of strings and concants them as follows with a if statement in the middle that decides whether to concant or not on one of them. The problem is the If(Evaluation, "", "") is complaining saying that it must not be nullable or must be a resource.. How do I work around this when the Evaluation simply checks an object to make sure it IsNot Nothing and also that a property in the object is checked as follows:

Dim R as string = stringA & " * sample text" & _     stringB & " * sample text2" & _     stringC & " * sameple text3" & _     If(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox Then ,StringD & " * sample text4" & _     , NOTHING) stringE & " * sample text5" 

VS is complaining about the applyValue. Any Ideas?

Should be noted that I have tried the following just to see if it would work and VS is rejecting it:

Dim y As Double Dim d As String = "string1 *" & _     "string2 *" & _     If(y IsNot Nothing, " * sample text4", "") & _     "string4 *" 

This is what it is flagging the y with:

  'IsNot' requires operands that have reference types, but this operand has the value type 'Double'.    C:\Users\Skindeep\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 13  16  WindowsApplication1 
like image 825
Skindeep2366 Avatar asked Dec 01 '12 16:12

Skindeep2366


People also ask

How to write If condition in VB?

Following is the If-Then-Else statement syntax in VB.NET as follows: Syntax: If (Boolean_expression) Then. 'This statement will execute if the Boolean condition is true.

What is an example of an if/then else statement?

The if / then statement is a conditional statement that executes its sub-statement, which follows the then keyword, only if the provided condition evaluates to true: if x < 10 then x := x+1; In the above example, the condition is x < 10 , and the statement to execute is x := x+1 .

What is the purpose of the If-Then-Else statement?

The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false . You could use an if-then-else statement in the applyBrakes method to take some action if the brakes are applied when the bicycle is not in motion.

How do you end an if statement?

IF statements must begin with the keyword IF and terminate with the keyword END. Components within IF statements can be connected with the keywords AND or OR. IF statements can have three forms: a simple IF statement, an IF statement with an ELSE condition, or an IF statement with an ELSEIF condition.


1 Answers

Use the IIF ternary expression evaluator

Dim R as string = stringA & " * sample text" & _                   stringB & " * sample text2" & _                   stringC & " * sameple text3" & _                   IIf(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox, StringD & " * sample text4", "") & _                   stringE & " * sample text5" 

EDIT: If you use VB.NET from ver 2008 onward you could use also the

IF(expression,truepart,falsepart) 

and this is even better because it provides the short-circuit functionality.

Dim R as string = stringA & " * sample text" & _                   stringB & " * sample text2" & _                   stringC & " * sameple text3" & _                   If(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox, StringD & " * sample text4", "") & _                   stringE & " * sample text5" 
like image 57
Steve Avatar answered Sep 27 '22 23:09

Steve