954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ComboBox Case Select Variable Assignment Question

Hello,

I am doing a simple calculator program. I have a combo box and assigned numerical values to a string variable under each case number using the same variable name. My problem is I don't know how to assign that value. When I use the current code, it pulls the value of the case select number rather than the assigned value. I suspect the problem is my string variable assignment. Any help would be greatly appreciated. Here is some of the code...

Dim strSelection As String
Dim decSelection As Decimal

'Assign value to variables

strSelection = cboConversionType.SelectedIndex.ToString()

'Check to make sure selection is made

If cboConversionType.SelectedIndex >= 0 Then

'Convert value to decimal

decSelection = Convert.ToDecimal(strSelection)

Else

'Display error message

End If

'Calculate Results
        decAnswer = decQuantityEntered * decSelection

'Assign numerical values according to which case is selected

Select Case cboConversionType.SelectedIndex
            Case 0
                strSelection = 25
            Case 1
                strSelection = 30
            Case 2
                strSelection = 35

End Select
birdlover2010
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

First, why do you have this? strSelection = boConversionType.SelectedIndex.ToString()

Second.
I can see what the code does, but I can't see what strSelection is for.
If you select something from the combobox, the SelectedIndex will be > -1.
So, the Select Case statement works as it should.
You might wanna consider adding a Case Else for a default value in case you forget to select something.

What Select Case does is that it checks the value of whatever argument you put to it.
And then each Case checks if the value is this or that.
It's just a compressed version of If...Else If...Else If...Else If...Else...End If.

'These two examples perform the EXACT same task, but the first one is bit more bulkier

If cboConverstionType.SelectedIndex = 0 Then
   strSelection = 25
Else If cboConverstionType.SelectedIndex = 1 Then
   strSelection = 30
Else If cboConverstionType.SelectedIndex = 2 Then
   strSelection = 35
End If

Select Case cboConverstionType.SelectedIndex
   Case 0
      strSelection = 25
   Case 1
      strSelection = 30
   Case 2
      strSelection = 35
End Select
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

use the SelectedItem property of the combobox to get the value. The selected item is available in the event, ComboBox1.SelectedIndexChanged. If you know that the selected item is integer you can convert it as

dim mynum as Integer = CInt(combobox1.SelectedItem)

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Main reason is your code order; and no, it's not me:D, it's your code.

You have decSelection = Convert.ToDecimal(strSelection) before your Select Case , Not after.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

(oops, missed that line) :)

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

Thanks everyone for your response. After I read Oxigen's post the day I posted my question, I realized that I didn't need the convert to string. I also changed the selected index to >-1 and moved the conversion to decimal after the case select. That seemed to do the trick. I did however, have a problem deploying the program to my website for download after I fixed all the issues. I finally gave up and used a 3rd party program which worked like a charm. Thanks again for all your help =)

birdlover2010
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

>>I finally gave up and used a 3rd party program which worked like a charm.
Care to share a charmed.link/name w/the rest of us?

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: