-
Dec 25th, 2012, 05:42 AM
#1
Thread Starter
Junior Member
[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
-
Dec 25th, 2012, 05:59 AM
#2
Hyperactive Member
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)
-
Dec 25th, 2012, 10:12 AM
#3
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
-
Dec 25th, 2012, 10:16 AM
#4
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.
-
Dec 26th, 2012, 10:47 AM
#5
Thread Starter
Junior Member
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
-
Dec 26th, 2012, 10:54 AM
#6
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.
-
Dec 26th, 2012, 10:55 AM
#7
Frenzied Member
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.
-
Dec 26th, 2012, 11:38 AM
#8
Thread Starter
Junior Member
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)
-
Dec 26th, 2012, 11:39 AM
#9
Thread Starter
Junior Member
Re: Small problem in "Select Case Usage" in VB 2010
The Answer is Instead of case 1 just put "case Else"
Everything working Fine !!!!!
-
Dec 26th, 2012, 11:44 AM
#10
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:
Select Case intTestVariable ' select from a number of values which TestVariable may hold
Case 0 ' what to do if value = 0
'do something
Case 1 ' what to if value = 1
'do something else
'etc
'etc
Case Else 'what to do if value = none of the above or is invalid
' do something appropriate
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|