Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Case Statement (2K)

    I am just finishing off some notes for a lecture tomorrow and was sent some notes by another tutor
    like this ( I apologise for the content of the example)

    <pre>Sub ElseCase()
    Dim Myvalue
    Dim strMessage As String


    Myvalue = InputBox("Type in an number", "Odd or even?")

    Select Case What
    Case Int(Myvalue / 2) = Myvalue / 2
    strMessage = "Even"
    Case Else
    strMessage = "Odd"
    End Select

    MsgBox strMessage
    End Sub
    </pre>



    The tutor is stressing this will when it clearly won't when I run it. If I run it and enter 12 it skips

    <pre>Case Int(Myvalue / 2) = Myvalue / 2
    strMessage = "Even"</pre>


    and picks up strMessage="Odd"

    I have sent her this adapted code and she can't get it to work


    <pre>Sub ElseCase()
    Dim Myvalue
    Dim strMessage As String
    Dim What

    Myvalue = InputBox("Type in an number", "Odd or even?")

    What = (Myvalue Mod 2) - 1

    Select Case What
    Case -1
    strMessage = "Even"
    Case Else
    strMessage = "Odd"
    End Select

    MsgBox strMessage
    End Sub</pre>


    I am beginning to doubt myself now, which is the right syntax? <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Jerry

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Case Statement (2K)

    Don't you mean<pre>What = (Myvalue Mod 2)

    Select Case What
    Case 0
    strMessage = "Even"
    Case Else
    strMessage = "Odd"
    End Select</pre>

    or did you have something else in mind??

    HTH
    Gre

  3. #3
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    Yes I suppose your example is a bit cleaner. No I was wondering more why The top bit of code appears to work on my colleagues and not mine [Case Int(Myvalue / 2) = Myvalue / 2] I am assuming she has her wires crossed and is not checking
    Jerry

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Case Statement (2K)

    >Yes I suppose your example is a bit cleaner.

    Not sure whether she'd want the class to handle:<pre> Select Case What
    Case 0
    strMessage = "Even"
    Case 1
    strMessage = "Odd"
    Case Else 'null values etc
    strMessage = "T'ere's trubble at mill!"
    End Select</pre>

    It's good practice to reserve Case Else for some basic error handling - at least for me.
    Gre

  5. #5
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    Thanks unkamunka , I will send that code...


    BUT my question is "Is the top code right or wrong, 'cos I can't get it to work!!"
    Jerry

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Case Statement (2K)

    >"Is the top code right or wrong, 'cos I can't get it to work!!"

    How are the settings for "Require Variable Declaration"? Your example Declares <code>What</code>
    in your code, but not in hers.

    As we are talking teaching - should not <code>MyValue</code>
    and <code>What</code>
    be declared as Doubles and not as Variants (on grounds of efficiency, if nothing else)?

    HTH
    Gre

  7. #7
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> unkamunka

    It's been a long Sunday afternoon preparing this so I am beginning to miss the plot a little, I have been making changes left right and centre. I better check the rest of the code then <img src=/S/sad.gif border=0 alt=sad width=15 height=15>



    ..ultimately I am going to stick with mine (with your changes of course) as it works my end
    Jerry

  8. #8
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Case Statement (2K)

    Supplementary:

    The basic problem is that her code - as you have it - will always produce a Null value for What. (This would have been trapped when using a three part Select Case.) You could also try this:<pre>Sub ElseCase()
    Dim Myvalue As Double
    Dim strMessage As String
    Dim What As Double

    Myvalue = InputBox("Type in an number", "Odd or even?")

    Select Case What
    Case Int(Myvalue / 2) = Myvalue / 2
    strMessage = "Even"
    Case IsNumeric(Myvalue / 2)
    strMessage = "Odd"
    Case Else
    strMessage = "You have not entered a valid number"
    End Select

    MsgBox strMessage

    End Sub</pre>

    HTH
    Gre

  9. #9
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    Got it.. thanks
    Jerry

  10. #10
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    In this case is there any advantage to declaring What and MyValue as Double versus Long?
    Regards
    Don

  11. #11
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Case Statement (2K)

    Initially, the choice was Long, but then I wondered about students choosing absurd numbers......
    Gre

  12. #12
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    I think that this is a case where it is important to properly instruct the user (student), and include an error trap in case they don't pay attention to the instructions. The InputBox prompt should specify a whole number; and the error trap should reject all but integers; otherwise, for example:

    <UL><LI>17.49 will return "Odd", while
    <LI>17.51 will return "Even".[/list]
    Regards
    Don

  13. #13
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Case Statement (2K)

    Or they could be converted (which is what would happen if assigned a "Long" instead of a double) and the message not only indicating Odd/Even but the value entered:

    17.49 could give:
    "17 is Odd"

    and 17.51 could give:
    "18 is Even"

    This would give the user an indication of what he/she entered.
    Steve

  14. #14
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Case Statement (2K)

    <img src=/S/grin.gif border=0 alt=grin width=15 height=15> I wasn't expecting this thread to carry on...I am glad it has caused some conversation. This was purely an example for me to get my head around why the Case statement was having problems with Case Int(Myvalue / 2) = Myvalue / 2 but it appears the difference in the code was the fact that unkamunka used IsNumeric(Myvalue / 2) and a new case else

    This was going to be a passing demonstration that showed them the uses of Case and If else....One of the objectives of the lesson......error traps and declarations were coming later <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Jerry

  15. #15
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Case Statement (2K)

    Then this type of thing could be a good seque into the topic. Code that works perfectly if the user does as instructed, but fails when the user does not. This leads to "what to do when the user is a fool or does something foolish?"

    Steve

Page 1 of 2 12 LastLast

Posting Permissions

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