Results 1 to 9 of 9

Thread: Shorter syntax for if statement

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Shorter syntax for if statement

    was wondering if there was a shortcut for doing a multi Or if statement

    like

    if data = "a" or data="b" or data ="c".

    I could use a select case then do case a,b,c..etc..

    but wanted to know if there was something else first.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I dont know if you can do it in VB.NET but it can be done in C#.

    Code:
    if(data == "a" || data == "b" || data == "c")
    {
       //do statements
    }
    This might work in VB.NET
    VB Code:
    1. If(data = "a" Or data = "b" Or data = "c")
    2.   'do statements
    3. EndIf
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    No. I meant i know i can do that. Thats not the problem. Its just that there are alot more of them, so I was trying to see if there was something shorter.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you know like when setting values in an array you can use that {"a","b,"c"} thing when setting the array. Just wondered if there was something similar to that for checking the value of a variable. Like I said. I can use Select Case, but just looking for other options if they exist.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I am not aware of a way to shorten the code. but you can shorten the proccess time by:
    VB Code:
    1. If data="a" OrElse data="b" OrElse data="C" Then
    2. 'something
    3. End If
    Last edited by Lunatic3; May 1st, 2003 at 09:58 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    so orElse is faster than or? Oh yeah..that s right. It stop's processing the moment it matches one right?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Yes, OrElse and AndAlso meant to be faster esp if you put the one that most probably meets the criteria first.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It depends where you want to save space or time or how things are laid out but this MAY be shorter:

    VB Code:
    1. Dim data = "a"
    2.         Dim choice As String = "abc"
    3.         If choice.IndexOf(Data) > -1 Then
    4.             MsgBox(Data)
    5.         End If

  9. #9

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well peformance is all that really matters and in comparisson to Or, orlese is a noticable improvement. And currently with my re-write of an old Unix mainframe application into .NET, it is taking 7 seconds to perform what used to take at least 5 minutes.

    So its all good!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

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



Click Here to Expand Forum to Full Width