Using TryParse within Select Case?

Just wondering if there is a possible way to do this.

Page 1 of 1

3 Replies - 1897 Views - Last Post: 09 November 2010 - 06:07 AM Rate Topic: -----

#1 Gradientpixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 10

Using TryParse within Select Case?

Posted 08 November 2010 - 09:54 PM

I am in my programming class and we had a small program to make that would check to see if an value that was entered is a integer and convert it to a Roman numeral. I know how to properly do it by putting the TryParse into the If...Then statement. What I am trying to do is to not use an If...Then statement and go just by a Case Selection statement. If it is not possible then that is cool with me, I am just trying to think outside the box. Here is what i have done with it:
Dim intA As Integer


        Select Case Integer.TryParse(txtInteger.Text, intA)
            Case 1 = 1
                lblRnumeral.Text = "I"
            Case 2 = 2
                lblRnumeral.Text = "II"
            Case 3 = 3
                lblRnumeral.Text = "III"
            Case 4 = 4
                lblRnumeral.Text = "IV"
            Case 5 = 5
                lblRnumeral.Text = "V"
            Case 6 = 6
                lblRnumeral.Text = "VI"
            Case 7 = 7
                lblRnumeral.Text = "VII"
            Case 8 = 8
                lblRnumeral.Text = "VIII"
            Case 9 = 9
                lblRnumeral.Text = "IX"
            Case 10 = 10
                lblRnumeral.Text = "X"
            Case Else
                lblRnumeral.Text = "Please enter a correct value!"
        End Select

    End Sub


When you do say "Case 1" it says it needs to be "Case CBool(1)." Using either "Case CBool(1)" or "Case 1 = 1" provides the same result. However the code shows no errors it doesn't do the conversion, it always displays the label's value as "I"

Is This A Good Question/Topic? 0
  • +

Replies To: Using TryParse within Select Case?

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: Using TryParse within Select Case?

Posted 08 November 2010 - 10:22 PM

TryParse is a Boolean function. You need to make a variable to hold the textbox input, perform the check then if it not Nothing then run the Select Case on the variable.
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: Using TryParse within Select Case?

Posted 08 November 2010 - 10:42 PM

Also its just 'Case 1' not 'Case 1 = 1'
Was This Post Helpful? 0
  • +
  • -

#4 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Using TryParse within Select Case?

Posted 09 November 2010 - 06:07 AM

View PostGradientpixel, on 08 November 2010 - 08:54 PM, said:

I am in my programming class and we had a small program to make that would check to see if an value that was entered is a integer and convert it to a Roman numeral. I know how to properly do it by putting the TryParse into the If...Then statement. What I am trying to do is to not use an If...Then statement and go just by a Case Selection statement. If it is not possible then that is cool with me, I am just trying to think outside the box. Here is what i have done with it:

You might want to use something along other way
Try this instead
 Dim dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
        dict.Add("I", 1)
        dict.Add("II", 2)
        dict.Add("III", 3)
        dict.Add("IV", 4)
        dict.Add("V", 5)
        dict.Add("VI", 6)
        dict.Add("VII", 7)
        dict.Add("VIII", 8)
        dict.Add("IX", 9)
        dict.Add("X", 10)
        Dim strText As String = txtInteger.Text
        Dim intA As Integer

        If dict.ContainsKey(strText) Then
            intA = dict(strText)
             lblRnumeral.Text = dict.Item(strText).ToString
        Else
            lblRnumeral.Text = "Please enter a correct value!"
    
        End If

~'J'~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1