Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: All-Topics
I want display result in third text box based on cases so i created 10 cases
 
when i debugged it displays only first case every time although output it 1,2,3,4,5,6,7,8,9,10;
 
Dim Rtn As String
        Select Case Rtn
            Case 1
                Lnt = 1
                TextBox3.Text = " Sister"
            Case 2
                Lnt = 2
                TextBox3.Text = "Enemy"
            Case 3
                Lnt = 3
                TextBox3.Text = "Enemy "
            Case 4
                Lnt = 4
                TextBox3.Text = "Enemy"
            Case 5
 
                Lnt = 5
                TextBox3.Text = "Friend"
            Case 6
                Lnt = 6
                TextBox3.Text = "Marrage "
            Case 7
                Lnt = 7
                TextBox3.Text = "Enemy"
            Case 8
                Lnt = 8
                TextBox3.Text = "Affection"
            Case 9
                Lnt = 9
                TextBox3.Text = "Enemy"
            Case 10
                Lnt = 10
                TextBox3.Text = "Lover "
        End Select
    End Sub
End Class
 
The Rtn will get values like 1,2,3,4,5,6,7,8,9,10;
based on this value the specified case should come as out put but it always displaying first case "Sister" What to do No Please help me>Merry Christmas To All
Posted 23-Dec-12 22:09pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is one of the reasons I hate automatic casting and prefer to work in C# where this will throw up an error.
 
When you have the string "1,2,3" in rtn, and try to compare it against a set of integer values, VB automatically parse the string and converts it to an integer value. Since the string contains a "," which is not a part of an integer, it stops parsing at that, and uses the value it has parsed so far: 1
So it will always only execute the first option.
 
But there is a wider problem here: you are assuming the Select Case will work on each sub element in your string: it won't, unless you provide a loop yourself:
Dim rtn As String = "1,2,3,4"
Dim parts As String() = rtn.Split(","C)
For Each s As String In parts
    Select Case s
        Case "1"
            Lnt = 1
            TextBox3.Text = " Sister"
            Exit Select
        ...
    End Select
Next
  Permalink  
Comments
charuwaka at 24-Dec-12 4:49am
   
Dear OriginalGriff,
 
Kindly see this i tried although it displaying "Sister
Actually Rtn gets different values like 1,2,3,4,5,6,7,8,9,10; In these numbers any one number may come .According to that nuber below cases should be come as output in thirdtextbox. But it always displaying "Sister"
 
Dim parts As String() = Rtn.Split(","c)
For Each s As String In parts
Select Case s
Case "1"
Lnt = 1
TextBox3.Text = " Sister"
Exit Select
Case "2"
Lnt = 2
TextBox3.Text = "Enemy"
Exit Select
Case "3"
Lnt = 3
TextBox3.Text = "Enemy "
Exit Select
Case "4"
Lnt = 4
TextBox3.Text = "Enemy"
Exit Select
Case "5"
Lnt = 5
TextBox3.Text = "Friend"
Exit Select
Case "6"
Lnt = 6
TextBox3.Text = "Marrage "
Exit Select
Case "7"
Lnt = 7
TextBox3.Text = "Enemy"
Exit Select
Case "8"
Lnt = 8
TextBox3.Text = "Affection"
Exit Select
Case "9"
Lnt = 9
TextBox3.Text = "Enemy"
Exit Select
Case "10"
Lnt = 10
TextBox3.Text = "Lover "
Exit Select
End Select
Next
 
End Sub
End Class
 

please give me solution according to mentioned above please sir
OriginalGriff at 24-Dec-12 5:16am
   
Try an experiment: Change
TextBox3.Text = "..."
To
TextBox3.Text += "..."
In every case.
What happens?
charuwaka at 24-Dec-12 5:54am
   
Didn't worked!!!
In second text box it Just Duplicated" Sister Sister Sister Sister Sister Sister"
 
When i press the many times
OriginalGriff at 24-Dec-12 6:03am
   
It wasn't meant to fix the problem: hence the word "experiment".
It was meant to find out more detail of what happens.
Try it again, but this time replace the line with
TextBox3.Text += rtn & "..."
It won't fix the problem, but it should give us a better idea of what is actually happening.
charuwaka at 24-Dec-12 6:21am
   
First of all thanks spending Your valuable time with me Merry Christmas
as you said it not done!
it giving "1 Sister1 Sister1 Sister1 Sister"
 

OriginalGriff at 24-Dec-12 6:27am
   
So that code is working correctly.
You are giving it a sequence of "1"s so it correctly outputting "Sister" each time
 
You need to look back at your code and work out why it is getting "1" each time, rather than different values.
I can't help with that - you haven't shown the relevant code!
charuwaka at 24-Dec-12 6:52am
   
OK To clarify that doubt i created another text box wrote a code that displays the length of entered two strings.that text box displaying correctly on each entry !!!
 
The mistake is only with in the "Select case"
OriginalGriff at 24-Dec-12 7:00am
   
Show the code fragment with the debugging code in place.
charuwaka at 24-Dec-12 8:26am
   
Dear Griff My Problem solved just i put a val() method and it worked for 1 st post
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Dim Rtn As Integer
        Rtn = Val(Lnt.Length)
        Select Case Rtn
            Case 1
                If Lnt = 1 Then
                    TextBox3.Text = " Sister"
                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 = "Affection"
                                            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 = "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 Select
    End Sub
  Permalink  
Comments
ProgramFOX at 24-Dec-12 8:38am
   
Perhaps this code block did solve your problem, but it's a really huge and hard to understand code block. There're very much IFs and the code isn't very readable.
Sergey Alexandrovich Kryukov at 3-Jun-13 13:59pm
   
Not just abuse, super abuse. Could be used in illustration of the book "How not to program".
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100