Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select case or combobox misunderstanding?

Status
Not open for further replies.

finglem

Technical User
Nov 14, 2003
66
0
0
GB
Hi Guys I hope there is someone out there that can point me in the right direction to clear up a bit of an issue I'm experiencing.

I have a select case statement that is based on a value in a combobox but it doesn't appear to be working as I'd expect

Here is the code:

dim selectedtext as string = Me.cboYear.Text
dim version as string = ""


Select Case selectedtext
Case selectedText = "2009"
version = "V1"

Case selectedtext = "2010"
version = "V2"

Case selectedtext = "2011"
version = "V3"

Case selectedtext = "2012"
version = "V4"

Case Else
version = "Unknown Version"

End Select

The code skips all the cases and sets the value of the version to "Unknown Version" despite selecting any of the other values in the combobox. I can't see anything glaringly obvious in the select case statement (although that I'll admit that it could be wrong) so I'm wondering if the combobox.text property is something I have misunderstood (also likely)

I'm using VS2010 on a vistabox

Any pointers would be much appreciated.

Thanks
 
One other thing further to the above

If I use the logic wrapped inside if statements the program does set the correct value ie

If selectedtext = "2010"
version = "V2"
End If

sets the version to V2 if it has been selected in the combo.

I guess that's a way round it but would rather understand why the case statement fails.

Any ideas?
 
I would try:
Code:
dim version as string = ""
Select Case Me.cboYear.Text
  Case "2009"
    version = "V1"
  Case "2010"
    version = "V2"
  Case "2011"
    version = "V3"
  Case "2012"
    version = "V4"
  Case Else
    version = "Unknown Version"
End Select

Have fun.

---- Andy
 
Thanks Andy, that works.

Still a little confused as to why the original version didn't work but I guess I've got to change the old mindset a bit more than I realised with dot net.

Thanks again
 
The original didn't work, because this:

Case selectedText = "2009"

The selectedText = "2009" part produces a value of True when selectedtext is "2009" and a value of False when selected text is not "2009". The values of True and False will never match the value of selectedtext, so the Case statements will never get hit.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jebenson

Thank you for the explanation. Much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top