If statement

vb6 Finland
  • 14 years ago

    Im using a pice of code that should read a cost from a table I ahve created. The cose is

    Private Sub Cost_Click()
    If Period = "Morning" Then
    Cost = table!Cost[Morning]
    ElseIf Period = "Afternoon" Then
    Cost = table!Cost[Afternoon]
    ElseIf Period = "Evening" Then
    Cost = table!Cost[Evening]
    ElseIf Period = "Evening" And Extension = "Yes" Then
    Cost = table!Cost[Evening Extension] else
    Cost = table!Cost[Special]
    End If









    End Sub

    What is wrong with it, it is expecting for an end of statement after each line where I have used Cost=

     

  • 14 years ago

    Sorry I got it I forgot the full stops.

  • 14 years ago

    Suggestion: If you have many IF-THEN statements, try using a SELECT CASE statement(I know this example doesn't have too many).  It makes the code much easier to read and modify.  For example, your above code would look like:


    Select Case Period
        Case "Morning"
            Cost = table!Cost[Morning]
        Case "Afternoon"
            Cost = table!Cost[Afternoon]
        Case "Evening"
            If Extension <> "Yes" Then
                Cost = table!Cost[Evening]
            Else
                Cost = table!Cost[Evening Extension] else
                Cost = table!Cost[Special]
            End If
    End Select














  • 14 years ago

    Apart from readability, I believe that I once read somewhere that the case-statment is also quicker than the If-ElseIf-.. structure.  Can anyone confirm if this indeed is the case ?

    Erwin

  • 14 years ago

    The way I was originally taught code(and that was a long time ago) was that with very small amounts of conditions, the IF statement is faster because the compiler does not have to create a hash table(table of if-then statements - select/case is converted to if/then at compile time).  However, this difference is minimal.  In some cases with larger amounts of conditions, the CASE statement pulls ahead by making use of the jump table(doesn't need to test all conditions). 

    That being said, I've never personally witnessed much performance gain in either direction.  I do think CASE statements are easier to look at and modify if there are more than 3 conditions or so.

    I did find this statement from a M$ representative from this website.

    Host: PaulHarrington (Microsoft)
    Q: I would like to know, if there is any performance gain on using different conditional structures ...like select-case or if-then-else-if...?
    A: In most cases there is no significant advantage. The only advantage would be for some "select-case" statements. The VB compiler can spot certain uses of select-case where it can use a "jump table" instead of a cascade of if-then-else statements.











  • 14 years ago

    Thanks for clarifying that.  I don't know why, but I tend to go for the if-elseif structure instead of the case structure, (Hey, I'm not a pro-developer...) and its good to know that it doesn't really make a difference.  Wink [;)]

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson