dcsimg
Results 1 to 6 of 6
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    446

    Resolved [RESOLVED] Select Case issue

    I am working to get a better understanding of some of the workings of VBasic. So in keeping with that, I tried the code below, instead of the method I might normally use. I have three radio buttons, with one of them checked as default.

    This didn't work and goes to Case Else, no matter which of the radio buttons I check.

    It was my belief that I had set the cases so that the case would be carried out if that particular radio button was checked. Obviously I was wrong. So where did I go wrong?
    Code:
                    Select Case CheckState.Checked
                        Case radAutograph.Checked = True
                            intPrice = CInt(cintAutograph)
                        Case radSuperhero.Checked = True
                            intPrice = CInt(cintHero)
                        Case radConvention.Checked = True
                            intPrice = CInt(cintConvention)
                        Case Else
                            intPrice = CInt(cintConvention)
                    End Select

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

    Re: Select Case issue

    I think you went wrong on the first line, but I'd have to say that I'm slightly surprised at the outcome. Consider this: What is CheckedState.Checked, and what type do you expect it to be?

    I believe the answer is that it is an integer with a nearly arbitrary value, or else it is simply a single item in the CheckedState enum. After all, CheckedState is an enum, and every enum can be cast to an integer, though whether or not this will happen in this case I'm not clear on. All of your cases evaluate to either True or False. If those implicitly converted to an integer, then there would be a chance that one would match CheckedState.Checked, if that also implicitly converted to an integer, but there's certainly no guarantee that this will be the case.

    So, what you have written might also be written like this:

    Select Case n
    Case SomethingIsTrue
    'Do something
    Case SomethingElseIsTrue
    'Do something
    Case AnotherThingIsTrue
    'Do something
    Case Else
    'Do something
    End Select

    It seems like there would be a chance that n would happen to match one of the items being true, but no guarantee, and almost certainly not if Option Strict is ON...though if Option Strict is ON, I'm a bit surprised that it would even compile. In any case, if n<>True, then the Else statement would be reached.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    446

    Re: Select Case issue

    It occurred to me that the first line might be where the problem was/is. I did notice that the value for CheckedState.Checked = 1, but thought that merely was equivalent to being True. Option Strict is not on, because this is just a few things I am noodling in an effort to have a better idea what all is going on with a code set and to try doing some things in different ways.

    So what is n? I am not clear on that.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,703

    Re: Select Case issue

    Code:
    Select Case True
        Case radAutograph.Checked
            intPrice = CInt(cintAutograph)
        Case radSuperhero.Checked
            intPrice = CInt(cintHero)
        Case radConvention.Checked
            intPrice = CInt(cintConvention)
        Case Else
            intPrice = CInt(cintConvention)
    End Select

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    446

    Re: Select Case issue

    Ahhh, Bach. Thanks Paul.

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

    Re: [RESOLVED] Select Case issue

    An enum can always be cast to an integer. You can state what integer each value is, but you don't HAVE to. So you can do either of these:
    Code:
    Public Enum MySetOfThings
     FirstThing
     SecondThing
     Third Thing
    End Enum
    
    Public Enum MySetOfThings
     FirstThing = 0
     SecondThing = 400
     Third Thing = 7
    End Enum
    If you don't specify a value for each member of the enum, it gets one anyways. So, when you saw that CheckedState.Checked was 1, that just means that the Checked member of the enum was given a value of 1. It didn't have to be, it could have been anything. However, since you know that it is 1, then n would be 1 in that example:
    Code:
    Select Case 1
     Case SomethingOtherThan1
      'This won't get hit.
     Case SomethingElseOtherThan1
      'This won't get hit.
     Case Else
      'Since the others aren't hit, this will be.
    End Select
    If 1 was True, then it might work, but True doesn't have to be anything in particular, let alone 1. Option Strict ON would have prevented implicit conversions. What you were doing was exploring implicit conversions in a way that isn't safe. Had the code worked, it would have worked because CheckedState.Checked happened to convert to something that True also could be converted to. Had you then learned that you could use CheckedState.Checked to mean True, then you would be depending upon something that could easily change. I'm pretty sure that would be technically undefined behavior that you'd be relying on. In coding, undefined behavior means: This may be true now, but only by accident, so it is free to change without warning.
    My usual boring signature: Nothing

Tags for this Thread

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


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.