Whaddya mean "else"?



  •  Just seen this in a ex-co-worker's code:

    <font color="#000080" face="courier new,courier">    Select Case CustAge
            Case Is < 22
                MyMsg = "WARNING - Customer is under 22 years old"
            Case 22 To 60
                MyMsg = ""
            Case 60 To 65
                MyMsg = "Customer is " & CustAge & " Check length of deal."
            Case Is > 65
                MyMsg = "WARNING - Customer is over 65 years old."
            Case Else
                MyMsg = "Cannot calculate customer age. Please check details."
        End Select</font>

    Case else?! If CustAge is defined an integer, what the hell else can it be??!!



  • I just learned something new about VB case statements. And it makes me sad.



  • @MeesterTurner said:

    Case else?! If CustAge is defined an integer, what the hell else can it be??!!

     

    It can be negative :)



  • @RobIII said:

    @MeesterTurner said:

    Case else?! If CustAge is defined an integer, what the hell else can it be??!!

     

    It can be negative :)

    And Is < 22 doesn't handle that one right?



  •  Ooops. Missed that one :D My bad.



  • @MeesterTurner said:

    Case else?! If CustAge is defined an integer, what the hell else can it be??!!

    Sure it's not a Variant?



    Also:

    • The output "Customer is 62 Check length of deal." is really poor.
    • Doesn't VB have sprintf?


  • I don't know enough about VB to conclude if this is stupid or not. But unless it is logically impossible for the else to be hit I prefer to have them. Although I would much rather have them throw an exception then go on with the show and set some text to indicate there was an error.



  •  Obviously, FILE_NOT_FOUND

     



  • Does the CustAge declaration allow it to be defined as null?



  • I've seen defining else cases as a way to gracefully handle future programmer stupidity. When the next programmer has to change the age requirement to 75 instead of 65 and misses a case, at least the software will give a message you can use to track down the stupidity. It isn't foolproof, the next guy can rip it out, but it can be useful.



  • I'm just curious what this routine is trying to do. At first I thought it was checking if the customer was old enough to purchase alcohol, but that check would be less than 21, not 22 (at least for the US, in most cases). Then I thought maybe it was for a senior discount, but it seems to apply differently to people from 60 to 65 than for people over 65, which is unusual for a senior discount. The message for customers 60 to 65 is also odd. Check length of deal? What does that mean, and why would your age affect it? Just curious.



  • @dcardani said:

    I'm just curious what this routine is trying to do. At first I thought it was checking if the customer was old enough to purchase alcohol, but that check would be less than 21, not 22 (at least for the US, in most cases). Then I thought maybe it was for a senior discount, but it seems to apply differently to people from 60 to 65 than for people over 65, which is unusual for a senior discount. The message for customers 60 to 65 is also odd. Check length of deal? What does that mean, and why would your age affect it? Just curious.
     

    My thoughts were insurances of some kind. But I couldn't think of one that would fit all the cases (car, life-insurance, funeral and unemployment don't really seem to aply). My second-best guess would be loans, mortages or some other long-term agreement.



  • @derula said:

    Doesn't VB have sprintf?
    Ahahahahahahahahahahahahaha

    hahahahahahahahahahaha

    hahahahaha

    hahahaha

    hahahahahahahahahahaha

    haha

    ha



  • @dtech said:

    @dcardani said:

    I'm just curious what this routine is trying to do. At first I thought it was checking if the customer was old enough to purchase alcohol, but that check would be less than 21, not 22 (at least for the US, in most cases). Then I thought maybe it was for a senior discount, but it seems to apply differently to people from 60 to 65 than for people over 65, which is unusual for a senior discount. The message for customers 60 to 65 is also odd. Check length of deal? What does that mean, and why would your age affect it? Just curious.
     

    My thoughts were insurances of some kind. But I couldn't think of one that would fit all the cases (car, life-insurance, funeral and unemployment don't really seem to aply). My second-best guess would be loans, mortages or some other long-term agreement.

     

     Not far off guys... it's car finance! The output is used in a message box, and the company I work for don't do loans for under 22's (they're likely to wrap their turbocharged Subaru Impreza around a tree), or for those 65+ who have most likely retired and have sod-all income from a UK pension.

    In answer to everyone else... It's definitely declared as an integer, and it errors with a null (I thought of that, so I tried it to find out) Also, as you can probably tell from the phrasing, we occasionally have a sloppy grasp of English... ah, the fun of in-house development with almost no QA :-)



  • It's definitely declared as an integer

    So the ranges overlap (e.g. age 22 would fit either "< 22" or "22 to 65"), making the case statement dependent on the order of evaluations and/or the order of the code. If he wanted that why not use " < 22" and then " < 65" ?



  • @AndyCanfield said:

    So the ranges overlap (e.g. age 22 would fit either "< 22" or "22 to 65")

    Are you trying to tell me that 22 is less than 22?



  • "Saw" is a letter shorter (would have saved you some time typing) and is the correct English word you intended to use.


  • Considered Harmful

    @lolwtf said:

    @derula said:
    Doesn't VB have sprintf?
    Ahahahahahahahahahahahahaha

    hahahahahahahahahahaha

    hahahahaha

    hahahaha

    hahahahahahahahahahaha

    haha

    ha

    ' Available in all .NET languages  (OK, it didn't specify VB.NET)
    System.String.Format( "This is an {0}.  {1:#,##0.0#}", "example", 1234.567 )
    


  • @derula said:

    @MeesterTurner said:
    Case else?! If CustAge is defined an integer, what the hell else can it be??!!

    Sure it's not a Variant?



    Also:

    • The output "Customer is 62 Check length of deal." is really poor.
    • Doesn't VB have sprintf?

     

     

    There's no sprintf , but there is Format to format a number ad other functions... But there are other nice oddities such as:

     Dim s As String
    s = 0
    Debug.Print s
    s = s + 1
    Debug.Print s
    s = 0
    Debug.Print s
    s = s & "1"
    Debug.Print s

     

    result is:

    0

    1

    0

    01

    all strings...

     

    Just have to remember to use & for string concatenation...



  •  

    Are you trying to tell me that 22 is less than 22?

    My boo-boo. What I wrongly said about 22 rightly applies to 60: you can have CustAge in "Case 22 To 60" and also in "Case 60 To 65". Thus two different cases match, making the whole case statement sensitive to evaluation order etc.

    I should ALWAYS open the Reply button in another tab.

     



  • @AndyCanfield said:

    My boo-boo. What I wrongly said about 22 rightly applies to 60: you can have CustAge in "Case 22 To 60" and also in "Case 60 To 65". Thus two different cases match, making the whole case statement sensitive to evaluation order etc.

     

     Sweet Jesus! I never even spotted that myself!!! D'oh!!! :-)



  • @mariushm said:

    There's no sprintf , but there is Format to format a number ad other functions... But there are other nice oddities such as:

     Dim s As String
    s = 0
    Debug.Print s
    s = s + 1
    Debug.Print s
    s = 0
    Debug.Print s
    s = s & "1"
    Debug.Print s

    result is:
    0
    1
    0
    01
    all strings...

    Just have to remember to use & for string concatenation...

    Ah, looks like PHP:

    s = 0;      echo s;  // => 0
    s = s + 1;  echo s;  // => 1
    s = 0;      echo s;  // => 0
    s = s . 1;  echo s;  // => 01

    ... only that here, all but the last are integers, and you don't need quotes around the last 1 iirc. At least, PHP does have sprintf.



  • @AndyCanfield said:

     

    Are you trying to tell me that 22 is less than 22?

    My boo-boo. What I wrongly said about 22 rightly applies to 60: you can have CustAge in "Case 22 To 60" and also in "Case 60 To 65". Thus two different cases match, making the whole case statement sensitive to evaluation order etc.

    I should ALWAYS open the Reply button in another tab.

     

    VB Selects are indeed case sensitive. And as someone else mentioned, you could correctly write "Is < 60", "Is < 65" etc. for the successive statements. Works the same and arguably is better for maintenance (imagine someone changing the first case to a lower number but not the range in the second). Some people consider it less readable though.

    Depending on which version of VB this came from (VB.Net, VB Classic or VBScript) and how the variable is defined; it is possible that NULL and Object are possible values.



  • @BlueKnot said:

    @AndyCanfield said:

     

    Are you trying to tell me that 22 is less than 22?

    My boo-boo. What I wrongly said about 22 rightly applies to 60: you can have CustAge in "Case 22 To 60" and also in "Case 60 To 65". Thus two different cases match, making the whole case statement sensitive to evaluation order etc.

    I should ALWAYS open the Reply button in another tab.

     

    VB Selects are indeed case sensitive. And as someone else mentioned, you could correctly write "Is < 60", "Is < 65" etc. for the successive statements. Works the same and arguably is better for maintenance (imagine someone changing the first case to a lower number but not the range in the second). Some people consider it less readable though.

    Depending on which version of VB this came from (VB.Net, VB Classic or VBScript) and how the variable is defined; it is possible that NULL and Object are possible values.

     

    I see what you did there.



  • Depending on which version of VB this came from (VB.Net, VB Classic or VBScript) and how the variable is defined; it is possible that NULL and Object are possible values.

    Why no one mentioned so far that they could have complex age?



  • @MeesterTurner said:

    ! If CustAge is defined an integer, what the hell else can it be??!!

    I think you answered your own question there. The code doesn't assert what type CustAge is.



  • @MeesterTurner said:

    • The output "Customer is 62 Check length of deal." is really poor.

     

    Why do they want to check the length of my "deal"?  That's a bit rude, don't you think?

     



  • @El_Heffe said:

    @MeesterTurner said:

    • The output "Customer is 62 Check length of deal." is really poor.

     

    Why do they want to check the length of my "deal"?  That's a bit rude, don't you think?

    Well, that was worth rezzing a two month old thread for...


Log in to reply