Results 1 to 10 of 10

Thread: [RESOLVED] Small problem in "Select Case Usage" in VB 2010

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Resolved [RESOLVED] Small problem in "Select Case Usage" in VB 2010

    i have small problem in the select case that displaying values only up to 9th case only although "Rtn" gets different values from above code but it displays output only between '1....9' cases. if the "Rtn" gets values above 9 .the third text shows empty. please help me. if you find any details insufficient rply me
    Code:
      Cnt = (fbn + Sbn).Trim
            Lnt = (Cnt.Length)
            Dim Rtn As Integer
            Rtn = Val(Lnt.Length)
            Select Case Rtn
                Case 1
                    If Lnt = 1 Then
                        TextBox3.Text = " Brother orSister"
                    Else
                        If Lnt = 2 Then
                            TextBox3.Text = "Enemy"
                        Else
                            If Lnt = 3 Then
                                TextBox3.Text = "Enemy "
                            Else
                                If Lnt = 4 Then
                                    TextBox3.Text = "Enemyes"
                                Else
                                    If Lnt = 5 Then
                                        TextBox3.Text = "Friends"
                                    Else
                                        If Lnt = 6 Then
                                            TextBox3.Text = "Marriage "
                                        Else
                                            If Lnt = 7 Then
                                                TextBox3.Text = "Enemys"
                                            Else
                                                If Lnt = 8 Then
                                                    TextBox3.Text = "Friend"
                                                Else
                                                    If Lnt = 9 Then
                                                        TextBox3.Text = "Enemys"
                                                    Else
                                                        If Lnt = 10 Then
                                                            TextBox3.Text = "Lovers "
                                                        Else
                                                            If Lnt = 11 Then
                                                                TextBox3.Text = "Lovers"
                                                            Else
                                                                If Lnt = 12 Then
                                                                    TextBox3.Text = "Friend"
                                                                Else
                                                                    If Lnt = 13 Then
                                                                        TextBox3.Text = "Brother Or Sister"
                                                                    Else
                                                                        If Lnt = 14 Then
                                                                            TextBox3.Text = "Friends"
                                                                        Else
                                                                            If Lnt = 15 Then
                                                                                TextBox3.Text = "Lovers"
                                                                            Else
                                                                                If Lnt = 16 Then
                                                                                    TextBox3.Text = "Affection"
     
                                                                                                            End If
                                                                                                        End If
                                                                                                    End If
                                                                                                End If
                                                                                            End If
                                                                                        End If
                                                                                    End If
                                                                                End If
                                                                            End If
                                                                        End If
                                                                    End If
                                                                End If
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                           
     End Select
    
        End Sub

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: Small problem in "Select Case Usage" in VB 2010

    I'm not a fan of having that many if... then...else.

    First, if you are using if..then... else try

    Code:
    IF value = 0 then
    .....
    ElseIF Value = 1 then
    ....
    ElseIF Value = 2 then
    ....
    Else
    ...
    End If
    Notice the 'ElseIF'

    Code:
         If Lnt = 1 Then
                        TextBox3.Text = " Brother orSister"
                    ElseIf Lnt = 2 Then
                            TextBox3.Text = "Enemy"
                        ElseIf Lnt = 3 Then
                                TextBox3.Text = "Enemy "
                            .......

    But if it was me I would do it like this.

    Code:
    Private _Value() As String = {"Brother or Sister", "Enemy", "Enemyes"}
    Textbox3.text = _Value(Lnt)

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Small problem in "Select Case Usage" in VB 2010

    You have only 1 case and that is for rtn=1 any other value will not execute any of your code within that case.

    If rtn =1 then it will check the value lnt and set the text in the text box if it matches in one of the If statements.

    There is no reason to use a select case the way you have this though as there is only 1 case and If will work fine. On the other hand all of those If Else statements should be a select case instead. Much easier to read that way.

    Example
    Code:
    If Rtn = 1 Then
        Select Case Lnt
            Case 1
                TextBox3.Text = " Brother orSister"
            Case 2
                TextBox3.Text = "Enemy"
            Case 3
                TextBox3.Text = "Enemy "
            Case 4
                TextBox3.Text = "Enemyes"
        End Select
    End If

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Small problem in "Select Case Usage" in VB 2010

    If rtn has multiple values and you want to test that as well then you may want to do a nested case
    Code:
    Select Case rtn
        Case 1
            Select Case Lnt
                Case 1
                    TextBox3.Text = " Brother orSister"
                Case 2
                    TextBox3.Text = "Enemy"
                Case 3
                    TextBox3.Text = "Enemy "
                Case 4
                    TextBox3.Text = "Enemyes"
            End Select
        Case 2
            'code for what to do when it equals 2
    End Select
    That said all you are doing is setting the values of a text box to a fixed value based on a number. would be better to create an array of possible values then just assign the array element to the textbox based on the index within the array.

    This could take the place of all of those lines if you were using an array
    Code:
    TextBox3.Text=MyArray(lnt)
    Last edited by DataMiser; Dec 26th, 2012 at 10:55 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: Small problem in "Select Case Usage" in VB 2010

    Dear sir see this full code although the "Rtn" gets different values it displaying only between 1 to 9 cases if "rtn" gets value above 9 the third text box remains empty please help me

    i have three textboxes and one button in first text boxe first name and in second textbox second name is entered and after performing respective the ouput is displayed in third text box

    see this code clear my problem pleaseeeeeeeeeeeeeeeeeee!!!


    Public Class Form1



    Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click

    Dim fbn As String
    Dim Sbn As String
    Dim Cnt As String
    Dim Lnt As String
    If TextBox1.Text = "" And TextBox2.Text = "" Then
    MessageBox.Show("Please Enter Names Idiot")
    End If
    If TextBox1.Text = "Charwaka" And TextBox2.Text = "Hey This is Mac" Then
    MessageBox.Show("This application Belongs To You Charwaka")
    End If

    fbn = TextBox1.Text
    Sbn = TextBox2.Text
    For Each c As Char In fbn
    If (Sbn.IndexOf(c) > -1) Then
    Sbn = Sbn.Remove(Sbn.IndexOf(c), 1)
    fbn = fbn.Remove(fbn.IndexOf(c), 1)
    End If
    Next
    Console.WriteLine("String 1: " + fbn + ", String 2:" + Sbn)
    Console.ReadLine()
    Cnt = (fbn + Sbn).Trim
    Lnt = (Cnt.Length)
    Dim Rtn As Integer
    Rtn = Val(Lnt.Length)
    Select Case Rtn
    Case 1
    If Lnt = 1 Then
    TextBox3.Text = " Brother orSister"
    ElseIf Lnt = 2 Then
    TextBox3.Text = "Enemy"
    ElseIf Lnt = 3 Then
    TextBox3.Text = "Enemy "
    ElseIf Lnt = 4 Then
    TextBox3.Text = "Enemyes"
    ElseIf Lnt = 5 Then
    TextBox3.Text = "Friends"
    ElseIf Lnt = 6 Then
    TextBox3.Text = "Marriage "
    ElseIf Lnt = 7 Then
    TextBox3.Text = "Enemys"
    ElseIf Lnt = 8 Then
    TextBox3.Text = "Friend"
    ElseIf Lnt = 9 Then
    TextBox3.Text = "Enemys"
    ElseIf Lnt = 10 Then
    TextBox3.Text = "Lovers "
    ElseIf Lnt = 11 Then
    TextBox3.Text = "Lovers"
    ElseIf Lnt = 12 Then
    TextBox3.Text = "Friend"
    ElseIf Lnt = 13 Then
    TextBox3.Text = "Brother Or Sister"
    ElseIf Lnt = 14 Then
    TextBox3.Text = "Friends"
    ElseIf Lnt = 15 Then
    TextBox3.Text = "Lovers"
    ElseIf Lnt = 16 Then
    TextBox3.Text = "Affection"
    ElseIf Lnt = 17 Then
    TextBox3.Text = "Sister"
    ElseIf Lnt = 18 Then
    TextBox3.Text = "Friend"
    End If
    End Select

    End Sub

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Small problem in "Select Case Usage" in VB 2010

    Like I said your select case only has 1 case and that is 1 so it will only execute any of that code within when rtn=1 if rtn = anything other than 1 then the code within the case will do nothing. I've already told you this and showed you how to do it properly.

  7. #7
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Small problem in "Select Case Usage" in VB 2010

    I'll say what others have said. Why do you have a Select Case with 1 Item and then 16 Nested Ifs? That is the complete backward way to use those functions, and probably the root of you error.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: Small problem in "Select Case Usage" in VB 2010

    My problem solved

    Instead of Case 1 i put case Else(That's simple all working well)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: Small problem in "Select Case Usage" in VB 2010

    The Answer is Instead of case 1 just put "case Else"

    Everything working Fine !!!!!

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Small problem in "Select Case Usage" in VB 2010

    It may have solved your problem but it doesn't mean that you've got it right!

    vb.net Code:
    1. Select Case intTestVariable ' select from a number of values which TestVariable may hold
    2.  
    3. Case 0 ' what to do if value = 0
    4. 'do something
    5. Case 1 ' what to if value = 1
    6. 'do something else
    7.  
    8. 'etc
    9.  
    10. 'etc
    11.  
    12. Case Else 'what to do if value = none of the above or is invalid
    13. ' do something appropriate
    14.  
    15. End Select
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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