我正在尝试创建一系列可以获取参数的命令.拿起我正在使用Select Case
的每个单独的命令这个问题是如果我使用的话,我无法感知'参数'(字符串的第二部分)Case Else
.如果我不使用Case Else
那么我无法处理不正确的命令并将它们发送到所需的程序.
例如:
Private Sub AllocateType(ByVal Command As String) Select Case Command Case "Eat" 'Call Eat procedure Case "Use" 'Call Use procedure Case "Quit" Case "Pause" Case "Go" Case Else Errors() 'Error handling procedure End Select End Sub
如果命令是'Brrrrr',它会调用Errors()
.然而,如果命令是"吃食物",它仍会调用Errors()
而不是将参数传递给Eat
程序.
编辑,因为它现在不起作用.
我已经尝试了所建议的内容,但我仍然有完全相同的问题.它似乎兼而有之Command.StartsWith
,Command.Contains
因为如果我尝试进入'吃食物',它仍然不会认为它是一个案例.
例:
Select Case Command Case Command.Contains("Eat") Output("TESTING") If Len(Command) > 4 Then Command = Mid(Command, 4, (Len(Command) - 4)) Interaction(Command) Else Output("Eat What?") End If Case "Eat" Output("Eat What?") Case Command.StartsWith("Use") If Len(Command) > 4 Then Command = Mid(Command, 4, (Len(Command) - 4)) Interaction(Command) Else Output("Use What?") End If Case "Use" Output("Use What?") 'Case Else ' Errors() End Select
Ashley Medwa.. 5
您需要使用IF
语句来执行所需的检查.
你有两个选择StartsWith
和Contains
.
StartsWith
将允许您检查您的关键字是否在字符串的开头.
吃香蕉=真
香蕉吃=假
Contains
将允许您检查您的关键字是否退出任何地方.
吃香蕉=真
香蕉吃=真
StartsWith Example
Private Sub AllocateType(ByVal Command As String) If Command.StartsWith("Eat") Then 'Call Eat procedure Else If Command.StartsWith("Use") Then 'Call Use procedure Else If Command.StartsWith("Quit") Then 'Call Quit procedure Else If Command.StartsWith("Pause") Then 'Call Pause procedure Else If Command.StartsWith("Go") Then 'Call Go procedure Else Errors() End If End Sub
包含示例
Private Sub AllocateType(ByVal Command As String) If Command.Contains("Eat") Then 'Call Eat procedure Else If Command.Contains("Use") Then 'Call Use procedure Else If Command.Contains("Quit") Then 'Call Quit procedure Else If Command.Contains("Pause") Then 'Call Pause procedure Else If Command.Contains("Go") Then 'Call Go procedure Else Errors() End If End Sub
另外,StartsWith
或者Contains
你可以这样做,StringComparison
这将允许你忽略大小写.
If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then
要么
If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then
忽略该案例将匹配"EAT","Eat","eAt"等.
您需要使用IF
语句来执行所需的检查.
你有两个选择StartsWith
和Contains
.
StartsWith
将允许您检查您的关键字是否在字符串的开头.
吃香蕉=真
香蕉吃=假
Contains
将允许您检查您的关键字是否退出任何地方.
吃香蕉=真
香蕉吃=真
StartsWith Example
Private Sub AllocateType(ByVal Command As String) If Command.StartsWith("Eat") Then 'Call Eat procedure Else If Command.StartsWith("Use") Then 'Call Use procedure Else If Command.StartsWith("Quit") Then 'Call Quit procedure Else If Command.StartsWith("Pause") Then 'Call Pause procedure Else If Command.StartsWith("Go") Then 'Call Go procedure Else Errors() End If End Sub
包含示例
Private Sub AllocateType(ByVal Command As String) If Command.Contains("Eat") Then 'Call Eat procedure Else If Command.Contains("Use") Then 'Call Use procedure Else If Command.Contains("Quit") Then 'Call Quit procedure Else If Command.Contains("Pause") Then 'Call Pause procedure Else If Command.Contains("Go") Then 'Call Go procedure Else Errors() End If End Sub
另外,StartsWith
或者Contains
你可以这样做,StringComparison
这将允许你忽略大小写.
If Command.StartsWith("Eat", StringComparison.OrdinalIgnoreCase) Then
要么
If Command.Contains("Eat", StringComparison.OrdinalIgnoreCase) Then
忽略该案例将匹配"EAT","Eat","eAt"等.