Results 1 to 6 of 6

Thread: Need help assigning variable if value is equal to this/that.

  1. #1

    Thread Starter
    Hyperactive Member simpleonline1234's Avatar
    Join Date
    Mar 2010
    Posts
    322

    Need help assigning variable if value is equal to this/that.

    Hey guys I know this is far too silly of a question but I just can't seem to figure it out.

    I have 10 checkboxes on my screen.

    I have a variable setup called "case" as a string

    I want to know how I can say if checkbox 1 is checked then case would be equal to "this"

    If checkbox2 is checked then case would = "that"

    Example of something I tried to cook up but didn't work because it tells me that I can't only use this in a "select case"

    Code:
    Dim Case as string
    
    If checkbox1.checked = true
    case = test1
    end if
    
    If checkbox2.checked = true
    case = test2
    end if
    
    If checkbox2.checked = true
    case = test2
    end if
    
    If checkbox3.checked = true
    case = test3
    end if
    
    If checkbox4.checked = true
    case = test4
    end if
    
    If checkbox5.checked = true
    case = test5
    end if
    Are you down with OOP?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Need help assigning variable if value is equal to this/that.

    Case is a reserved keyword. this works:

    vb Code:
    1. Dim [Case] As String
    2.  
    3. If checkbox1.checked = True Then
    4.     [Case] = test1
    5. End If
    6.  
    7. If checkbox2.checked = True Then
    8.     [Case] = test2
    9. End If
    10.  
    11. If checkbox2.checked = True Then
    12.     [Case] = test2
    13. End If
    14.  
    15. If checkbox3.checked = True Then
    16.     [Case] = test3
    17. End If
    18.  
    19. If checkbox4.checked = True Then
    20.     [Case] = test4
    21. End If
    22.  
    23. If checkbox5.checked = True Then
    24.     [Case] = test5
    25. End If

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Need help assigning variable if value is equal to this/that.

    Yeah, Case is a reserved word. Just name it something else and it will be fine. However, I would also point out that there can always be multiple checkboxes selected, so the way your code runs, the variable (whatever you name it to) will hold the value for the last checkbox that was checked. That is unlikely to be the way you want it to work. RadioButtons would be exlusive, or else you could &= things together to build all the checked items, but right now you pretty much have a strange race condition going on.
    My usual boring signature: Nothing

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Need help assigning variable if value is equal to this/that.

    *edit flood of replys in the few seconds the tab was open

    The ide is telling you exactly what it is. "case" is a keyword. You can not name your string "case"

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Need help assigning variable if value is equal to this/that.

    Three of the same replies in short order. That was quick.

    Take a look at the rest of my post, though, because that point is still valid.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member simpleonline1234's Avatar
    Join Date
    Mar 2010
    Posts
    322

    Re: Need help assigning variable if value is equal to this/that.

    Oh wow..never occured to me...thanks guys...
    Are you down with OOP?

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