“小问题”选择案例“用法VB 2010 [英] Small Problem in " Select case" Usage VB 2010

查看:87
本文介绍了“小问题”选择案例“用法VB 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据案例在第三个文本框中显示结果所以我创建了10个案例



当我调试它时每次只显示第一个案例虽然输出它1, 2,3,4,5,6,7,8,9,10;



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





Rtn将根据指定的值获得1,2,3,4,5,6,7,8,9,10;

等值案件应该出来,但它总是显示第一个案例姐姐做什么不请帮助我>圣诞快乐到所有



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

推荐答案

这是原因之一我讨厌自动投射,而更喜欢在C#中工作,这会引发错误。



当你在rtn中有字符串1,2,3时,尝试将它与一组整数值进行比较,VB自动解析字符串并将其转换为整数值。由于字符串包含一个,,它不是整数的一部分,它会停止解析,并使用它到目前为止已解析的值:1

所以它总是只执行第一个选项。



但是这里有一个更广泛的问题:你假设Select Case将对你的字符串中的每个子元素起作用:它不会,除非你自己提供一个循环:

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


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


这篇关于“小问题”选择案例“用法VB 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆