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

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

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)

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.

(oops, missed that line) :)

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 =)

>>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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.