PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Syntax to do if statement in oneline-VBForums
Results 1 to 12 of 12

Thread: Syntax to do if statement in oneline

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    17

    Syntax to do if statement in oneline

    Syntax for if statement
    Last edited by mcupryk; Jan 10th, 2019 at 11:57 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,975

    Re: Syntax to do if statement in oneline

    You can use the If operator:
    vb.net Code:
    1. Model.Name = If(item.Name = Nothing, String.Empty, item.Name.ToUpper())
    On a related note, there is also null propagation if you were to use Nothing rather than String.Empty, i.e. this:
    vb.net Code:
    1. Model.Name = If(item.Name = Nothing, Nothing, item.Name.ToUpper())
    could be more succinctly be written like this:
    vb.net Code:
    1. Model.Name = item.Name?.ToUpper()
    Using null propagation, as execution moves along the expression, if any reference preceding a '?." operator is Nothing, the expression evaluates to nothing, otherwise execution continues as though the '?." was just a dot.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,975

    Re: Syntax to do if statement in oneline

    Don't delete the contents of a question. If you want the thread deleted then use the Report Post icon to send a request to the mods. Better yet, post a solution to your problem so that you might help others in future.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    17

    Re: Syntax to do if statement in oneline

    Quote Originally Posted by jmcilhinney View Post
    You can use the If operator:
    vb.net Code:
    1. Model.Name = If(item.Name = Nothing, String.Empty, item.Name.ToUpper())
    On a related note, there is also null propagation if you were to use Nothing rather than String.Empty, i.e. this:
    vb.net Code:
    1. Model.Name = If(item.Name = Nothing, Nothing, item.Name.ToUpper())
    could be more succinctly be written like this:
    vb.net Code:
    1. Model.Name = item.Name?.ToUpper()
    Using null propagation, as execution moves along the expression, if any reference preceding a '?." operator is Nothing, the expression evaluates to nothing, otherwise execution continues as though the '?." was just a dot.

    Hi when I try to do the second it fails, :

    1) If (String.IsNullOrEmpty(item.Company)) Then
    MODEL.NAME = ""
    Else
    MODEL.NAME = item.Company.ToUpper()
    End If
    This works

    but this does work. how can I make 1) in one line?
    2) MODEL.NAME = IIf(String.IsNullOrEmpty(item.Company), "", item.Company.ToUpper())

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    17

    Re: Syntax to do if statement in oneline

    Sorry I will not delete questions.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,975

    Re: Syntax to do if statement in oneline

    You do it how I told you to do it. If it doesn't work for you, you did it wrong. "It fails" is not a reasonable explanation. Show us what you actually did and tell us what actually happens and then we can tell you what you did wrong.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,006

    Re: Syntax to do if statement in oneline

    Use IF as John showed you, no IIF as you posted,.... they are two different things that work very differently.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    657

    Re: Syntax to do if statement in oneline

    Just to expand slightly on what John explained - there are a few ways to condense the 'usual' If/Else down to one line:

    Shorthand 'If' statement (combine on one line, leaving off the 'End If'):
    Code:
    If Condition Then x = FooValue Else x = BarValue
    Using the 'If' operator:
    Code:
    x = If(Condition, FooValue, BarValue)
    Using the legacy 'Iff' VisualBasic namespace method (not recommended - just for completeness):
    Code:
    x = IIf(Condition, FooValue, BarValue)
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    657

    Re: Syntax to do if statement in oneline

    Sorry - didn't mean to post the same reply twice - I'd delete it if I could find that option...
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: Syntax to do if statement in oneline

    Quote Originally Posted by David Anton View Post
    Sorry - didn't mean to post the same reply twice - I'd delete it if I could find that option...
    You didn't post it twice. Are you seeing things???







    (actually, I removed it for you)
    My usual boring signature: Nothing

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,975

    Re: Syntax to do if statement in oneline

    Quote Originally Posted by David Anton View Post
    Sorry - didn't mean to post the same reply twice - I'd delete it if I could find that option...
    Long time, no see, David. Good to know that you're still lurking.

    I'm guessing that you did post the same thing twice and then edited your second post, right? The reason for the double post was probably a forum glitch that we've all been dealing with since the last upgrade. When you submit a post, the system tries to submit it twice. If you're a mod or above, your post is successfully submitted twice. If you're a regular human, the second submission fails and you're told that you cannot post twice within 30 seconds. Most people think that that means that their post wasn't submitted at all, wait the 30 seconds and then unknowingly post a duplicate. If you get that message and you haven't actually posted something else in the last 30 seconds, just assume that your post was actually submitted and don't post again.

    If you do make a double post, you can use the Edit feature and from there choose to delete the post.
    Last edited by jmcilhinney; Jan 11th, 2019 at 09:29 PM.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,975

    Re: Syntax to do if statement in oneline

    Here are some notes on the differences between The If operator and the IIf function. Firstly, one is an operator and one is a function. That means that If is part of the language of VB while IIf is just a regular old method. That leads to the most important difference in behaviour, i.e. the If operator is implemented such that it short-circuits while, like any other method, the arguments to IIf are all evaluated before invoking the method.

    Short-circuiting occurs when not all of an expression is evaluated when what's left cannot change the result. We are most used to seeing that in Boolean expressions, where the And and Or operators do not short-circuit while the AndAlso and OrElse operators do short-circuit. Short-circuiting is good because it allows you to easily test conditions that can only be True if another condition is specifically True. For instance, this code will work in all cases:
    vb.net Code:
    1. If text IsNot Nothing AndAlso text.Length < 10 Then
    2.     'If text is provided, it must contain at least 10 characters.
    3. End If
    whereas this will throw a NullReferenceException if 'text' is Nothing:
    vb.net Code:
    1. If text IsNot Nothing And text.Length < 10 Then
    2.     'If text is provided, it must contain at least 10 characters.
    3. End If
    In the first case, the Length property of 'text' is only evaluated if 'text' is not Nothing whereas an attempt is made to get the Length property value even if 'text' is Nothing in the second case. Without a short-circuiting operator, you'd have to change the second code snippet to this:
    vb.net Code:
    1. If text IsNot Nothing
    2.     If text.Length < 10 Then
    3.         'If text is provided, it must contain at least 10 characters.
    4.     End If
    5. End If
    The difference between If and IIf is similar. In this case:
    vb.net Code:
    1. Dim textLength = If(text Is Nothing, 0, text.Length)
    you are good to go in all situations. On the other hand, this code:
    vb.net Code:
    1. Dim textLength = IIf(text Is Nothing, 0, text.Length)
    will throw a NullReferenceException if 'text' is Nothing. That because, as I said, all arguments to IIf are evaluated before the method is invoked, so while the first code snippet is equivalent to this:
    vb.net Code:
    1. Dim textLength As Integer
    2.  
    3. If text Is Nothing Then
    4.     textLength = 0
    5. Else
    6.     textLength = text.Length
    7. End If
    the second code snippet is equivalent to this:
    vb.net Code:
    1. Dim a = text Is Nothing
    2. Dim b = 0
    3. Dim c = text.Length
    4.  
    5. Dim textLength As Integer
    6.  
    7. If a Then
    8.     textLength = b
    9. Else
    10.     textLength = c
    11. End If
    As you can see, the 'text.Length' property will be evaluated regardless of whether 'text' is Nothing or not, so a NullReferenceException will be thrown if it is.

    For this reason, IIf was fine if you were using literal values or expressions that would always evaluate successfully but a lot of people would get tripped up in a situation like the one in the example above, i.e. where one of the result expressions was invalid if the condition wasn't specifically True or specifically False.

    The other difference that occasionally trips people up (I've encountered a number of experienced people who didn't realise this either) is that the If operator is effectively generic. The IIf function, on the other hand, always returns an Object reference no matter what. In the case of IIf, this means that there is no restriction on what the last two arguments can be but, with Option Strict On, you need to cast the result if you want to use it where anything more specific than an Object is expected. I glossed over that in my previous examples but, with Option Strict On, this would not even compile:
    vb.net Code:
    1. Dim textLength As Integer
    2.  
    3. '...
    4.  
    5. textLength = IIf(text Is Nothing, 0, text.Length)
    That's because it tries to assign the Object reference returned by IIf to an Integer variable. You would need to cast that result as the desired type, e.g.
    vb.net Code:
    1. Dim textLength As Integer
    2.  
    3. '...
    4.  
    5. textLength = CInt(IIf(text Is Nothing, 0, text.Length))
    The If operator, on the other hand, infers its return type from the type of the two result expressions. As a result, this is fine:
    vb.net Code:
    1. Dim textLength As Integer
    2.  
    3. '...
    4.  
    5. textLength = If(text Is Nothing, 0, text.Length)
    In that case, the second and third arguments are both type Integer so the return type of If is also Integer, so no cast is required.

    This also means that a single specific type must be able to be inferred from those last two arguments though, so there may be a need to cast one them at times. For instance, let's say that you are working with nullable dates in a database. This code:
    vb.net Code:
    1. Dim nullableDateTime As DateTime?
    2.  
    3. '...
    4.  
    5. nullableDateTime = If(myDataRow.IsNull("DateTime"), Nothing, myDataRow.Field(Of DateTime)("DateTime"))
    will never actually give you a null value. That's because the Nothing passed to the second parameter will actually be inferred to be type DateTime rather than DateTime? and thus, if the DataRow field actually is NULL, If will return the default value for the DateTime type, which is #1/01/0001#. If you want that Nothing to actually be treated as a null then you must cast it as DateTime?:
    vb.net Code:
    1. Dim nullableDateTime As DateTime?
    2.  
    3. '...
    4.  
    5. nullableDateTime = If(myDataRow.IsNull("DateTime"), DirectCast(Nothing, DateTime?), myDataRow.Field(Of DateTime)("DateTime"))
    If you hover over the If operator in the IDE in each case, you'll see that the return type of the If operator is different in each case. The converse operation also requires a cast, i.e. this:
    vb.net Code:
    1. myDataRow("DateTime") = If(nullableDateTime.HasValue, nullableDateTime.Value, DBNull.Value)
    will not compile because no common type can be inferred from the last two parameters. The most common type between the two is Object but, if you want to use type Object, you can't rely on type inference but rather you must be explicit. That means casting at least one of the arguments as that type. If you only cast one, the other will be inferred, e.g.
    vb.net Code:
    1. myDataRow("DateTime") = If(nullableDateTime.HasValue, nullableDateTime.Value, CObj(DBNull.Value))
    When using the IIf function, no cast is required because IIf always treats the last two arguments as type Object and returns type Object anyway. That's not a reason to use it though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width