VB中的is是什么意思?

qq25539088 2008-12-21 07:26:05
Static a As Integer
If Text1.Text = "008" Then
MsgBox "密码正确"
Else
a = a + 1
End If '结束语句

Select Case a
Case 1
MsgBox "密码错误你还有2次机会"
Text1.Text = ""
Text1.SetFocus
Case 2
MsgBox "密码错误你还有1次机会"
Text1.Text = ""
Text1.SetFocus
Case Is > 2
MsgBox "您三次输入密码都是错误的,程序将关闭"
End
End Select


在以上代码中就用到了is语句 请问is是什么意思?
...全文
6201 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
wax100628 2011-10-04
  • 打赏
  • 举报
回复
case is > 2

顾名思义: 一种是大于2的情况
僵哥 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 SYSSZ 的回复:]
在回此贴之前,我的也认为Is>2,相当于a>2,多直观,多好理解!就是因为为了回贴,查了MSDN,我才改变了我多年的看法.
(1)也许是我孤陋寡闻,我还没看到MS说Is就相当于a这样的表达式;
(2)前面已有Select Case a,为什么不可以理解为后面a省略了呢?
(3)我们把Is>2理解为"是" 大于2或者"属于"大于2的情况不更符合"Is 关键字是配合比较运算符来指定一个数值范围"这样的思想吗?
(4)Case a 不是Is也可省略的吗?
个人的看法,也许这种看…
[/Quote]
VB的本意就是简化编程,所以很多规则性的东西几乎相当于是一种“定式”,与之相对的就是“灵活”,这或许就是VB的一特征。毕竟它是属于解释性执行的,与编译执行还是有相当多不同的地方,首先就是效率,对于编译执行的,可以先由语义分析器进行分析,然后再进行预处理,然后再编译,但是对于解释执行来讲,这些开销就过大了。
僵哥 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 Tiger_Zhao 的回复:]
引用 18 楼 unsigned 的回复:
VB的本意就是简化编程,所以很多规则性的东西几乎相当于是一种“定式”,与之相对的就是“灵活”,这或许就是VB的一特征。毕竟它是属于解释性执行的,与编译执行还是有相当多不同的地方,首先就是效率,对于编译执行的,可以先由语义分析器进行分析,然后再进行预处理,然后再编译,但是对于解释执行来讲,这些开销就过大了。

作为一个星级用户,还在说 VB 是编译执行的就欠考虑了。
如果 VB 是解释执行的,《高级 Visual Basic 编程》(Advanced Visual Basic) 中的“绑定函数到对象上”、“轻量COM对象”、“重载函数”等等就做不到了。

建议看看:
真是想不到系列
关于VB的Native Code的传奇[/Quote]
没必要去抠字眼,我没有说VB只能解释执行,但是它的设计初衷就是基于VM的。
Tiger_Zhao 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 unsigned 的回复:]
VB的本意就是简化编程,所以很多规则性的东西几乎相当于是一种“定式”,与之相对的就是“灵活”,这或许就是VB的一特征。毕竟它是属于解释性执行的,与编译执行还是有相当多不同的地方,首先就是效率,对于编译执行的,可以先由语义分析器进行分析,然后再进行预处理,然后再编译,但是对于解释执行来讲,这些开销就过大了。[/Quote]
作为一个星级用户,还在说 VB 是编译执行的就欠考虑了。
如果 VB 是解释执行的,《高级 Visual Basic 编程》(Advanced Visual Basic) 中的“绑定函数到对象上”、“轻量COM对象”、“重载函数”等等就做不到了。

建议看看:
真是想不到系列
关于VB的Native Code的传奇
靳永富 2008-12-23
  • 打赏
  • 举报
回复
学习了
fangmanhua8888 2008-12-23
  • 打赏
  • 举报
回复
學習...........................................
  • 打赏
  • 举报
回复
这里的is就是你用的case里面的a 的意思,相当于a>2的意思。
Tiger_Zhao 2008-12-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 SYSSZ 的回复:]
尽管上面多数人都认这是条件表达式的值,正如我在七楼的贴子表达的那样,我仍坚持Is是运算符,这是Is关键字的本质.MSDN---"Is 关键字是配合比较运算符来指定一个数值范围",显然也没有"值"的意思,倒是有运算符的意思[/Quote]

[Quote=MSDN:]Select Case 语句

注意 Is 比较运算符和使用在 Select Case 语句中的 Is 关键字并不相同。[/Quote]
SYSSZ 2008-12-22
  • 打赏
  • 举报
回复
尽管上面多数人都认这是条件表达式的值,正如我在七楼的贴子表达的那样,我仍坚持Is是运算符,这是Is关键字的本质.MSDN---"Is 关键字是配合比较运算符来指定一个数值范围",显然也没有"值"的意思,倒是有运算符的意思
Tiger_Zhao 2008-12-22
  • 打赏
  • 举报
回复
这里的 is 就是 Select Case testexpression 中 testexpression 值的别名。
SYSSZ 2008-12-22
  • 打赏
  • 举报
回复
在回此贴之前,我的也认为Is>2,相当于a>2,多直观,多好理解!就是因为为了回贴,查了MSDN,我才改变了我多年的看法.
(1)也许是我孤陋寡闻,我还没看到MS说Is就相当于a这样的表达式;
(2)前面已有Select Case a,为什么不可以理解为后面a省略了呢?
(3)我们把Is>2理解为"是" 大于2或者"属于"大于2的情况不更符合"Is 关键字是配合比较运算符来指定一个数值范围"这样的思想吗?
(4)Case a 不是Is也可省略的吗?
个人的看法,也许这种看法根本就是错的,说出来大家一起探讨,也是向大家讨教.
也希望都能深刻思考一下此问题.
SYSSZ 2008-12-22
  • 打赏
  • 举报
回复
说Is就是表达式了吗?
SYSSZ 2008-12-22
  • 打赏
  • 举报
回复
Visual Basic Language Reference  
Select...Case Statement (Visual Basic)

Runs one of several groups of statements, depending on the value of an expression.


Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select Parts
testexpression
Required. Expression. Must evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort).

expressionlist
Required in a Case statement. List of expression clauses representing match values for testexpression. Multiple expression clauses are separated by commas. Each clause can take one of the following forms:

expression1 To expression2

[ Is ] comparisonoperator expression

expression

Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.

Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.

The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.

statements
Optional. One or more statements following Case that run if testexpression matches any clause in expressionlist.

elsestatements
Optional. One or more statements following Case Else that run if testexpression does not match any clause in the expressionlist of any of the Case statements.

End Select
Terminates the definition of the Select...Case construction.

Remarks
If testexpression matches any Case expressionlist clause, the statements following that Case statement run up to the next Case, Case Else, or End Select statement. Control then passes to the statement following End Select. If testexpression matches an expressionlist clause in more than one Case clause, only the statements following the first match run.

The Case Else statement is used to introduce the elsestatements to run if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. Although not required, it is a good idea to have a Case Else statement in your Select Case construction to handle unforeseen testexpression values. If no Case expressionlist clause matches testexpression and there is no Case Else statement, control passes to the statement following End Select.

You can use multiple expressions or ranges in each Case clause. For example, the following line is valid.

Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber

Note
The Is keyword used in the Case and Case Else statements is not the same as the Is Operator (Visual Basic), which is used for object reference comparison.


You can specify ranges and multiple expressions for character strings. In the following example, Case matches any string that is exactly equal to "apples", has a value between "nuts" and "soup" in alphabetical order, or contains the exact same value as the current value of testItem.

Case "apples", "nuts" To "soup", testItem

The setting of Option Compare can affect string comparisons. Under Option Compare Text, the strings "Apples" and "apples" compare as equal, but under Option Compare Binary, they do not.

Note
A Case statement with multiple clauses can exhibit behavior known as short-circuiting. Visual Basic evaluates the clauses from left to right, and if one produces a match with testexpression, the remaining clauses are not evaluated. Short-circuiting can improve performance, but it can produce unexpected results if you are expecting every expression in expressionlist to be evaluated. For more information on short-circuiting, see Boolean Expressions.


If the code within a Case or Case Else statement block does not need to run any more of the statements in the block, it can exit the block by using the Exit Select statement. This transfers control immediately to the statement following End Select.

Select Case constructions can be nested. Each nested Select Case construction must have a matching End Select statement and must be completely contained within a single Case or Case Else statement block of the outer Select Case construction within which it is nested.

Example
The following example uses a Select Case construction to write a line corresponding to the value of the variable number. The second Case statement contains the value that matches the current value of number, so the statement that writes "Between 6 and 8, inclusive" runs.

Visual Basic Copy Code
Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Not between 1 and 10, inclusive")
End Select
zhufenghappy 2008-12-22
  • 打赏
  • 举报
回复
学习了
xrongzhen 2008-12-22
  • 打赏
  • 举报
回复
VB中Is的两种情况:
1. 比较运算符
[Quote=MSDN]
Is 运算符示例:
使用 Is 运算符来比较两个对象引用。示例中的对象变量名只是作说明用途的一般性名称而已。

Dim MyObject, YourObject, ThisObject, OtherObject, ThatObject, MyCheck
Set YourObject = MyObject ' 指定对象引用。
Set ThisObject = MyObject
Set ThatObject = OtherObject
MyCheck = YourObject Is ThisObject ' 返回 True。
MyCheck = ThatObject Is ThisObject ' 返回 False。
' 假设 MyObject <> OtherObject
MyCheck = MyObject Is ThatObject ' 返回 False。

[/Quote]

2. Select Case 语句中的 Is 关键字
Select Case testexpression 目标表达式
[Case expressionlist-n case子句中用的是表达式的值
[statements-n]] ...
[Case Else
[elsestatements]]

如果case子句中要表示一个范围,但子句中又不能出现“表达式”本身,这个时侯就用is关键字代替。。。

楼上僵哥的示例第2个“不使用is”中:Case getvalue = 2 '注意这里会再次执行GetValue
慎用!!!

僵哥 2008-12-22
  • 打赏
  • 举报
回复
Is 使用的示例

1.不使用Is
Dim a As Integer, b As Integer
Dim m As Boolean
m = True
b = 10
a = 10
Select Case m
Case (a = b)
MsgBox "true"
Case Else
MsgBox "false"
End Select


2.使用Is改变结果
Dim a As Integer, b As Integer
Dim m As Boolean
m = True
b = 10
a = 10
Select Case m
Case Is <> (a = b)
MsgBox "true"
Case Else
MsgBox "false"
End Select


注意:Is后面只能接运算符"=", "<>", "<", "<=", ">", ">="之一,在CASE当中属于固定的句式,同时它也代指select CASE所指的“目标表达式”,但是不可以使用来代替,比如上例当中对于M这个本身就是boolean型的值,否则结果是非预料中的,同时,正因为是“目标表达式”,所以使用IS不需要做二进次表达式运算,否则结果也是不可预知的,比如说“目标表达式”当中是一个可变的结果。

示例:
1.使用IS
Dim StoredValue As Integer
Function getvalue() As Integer
StoredValue = StoredValue + 1
getvalue = StoredValue
End Function

Call getvalue
Select Case getvalue
Case 1
MsgBox "false"
Case Is = 2
MsgBox "true"
Case Is > 2
MsgBox "false"
Case Else
MsgBox "else"
End Select

2.不使用IS
Dim StoredValue As Integer
Function getvalue() As Integer
StoredValue = StoredValue + 1
getvalue = StoredValue
End Function

Call getvalue
Select Case getvalue
Case 1
MsgBox "false"
Case getvalue = 2 '注意这里会再次执行GetValue
MsgBox "true"
Case Is > 2
MsgBox "false"
Case Else
MsgBox "else"
End Select
僵哥 2008-12-22
  • 打赏
  • 举报
回复
关于VB当中的IS,要分两种情况:
第一种,操作符,通常用于两个对象(OLE对象)变量,用于检查是否同时指向同一个对象实例;或者通过TypeOf判断某一个对象变量(的实例)是否兼容某一类型。
第二种,Case当中的一种特定表达式,范围判断表达式,
case总共有三类表达式:
1). [值表达式]
2). [值表达式1] To [值表达式2]
3). Is [范围表达式(允许操作符:=, <>, <, <=, >, or >=)]
SYSSZ 2008-12-21
  • 打赏
  • 举报
回复
Option Explicit
Private Sub Command1_Click()
Static a As Integer
If Text1.Text = "008" Then
MsgBox "密码正确"
Else
a = a + 1
End If '结束语句
Select Case a
Case Is = 1
MsgBox "密码错误你还有" & 6 - a & "次机会"
Text1.Text = ""
Case Is = 2
MsgBox "密码错误你还有" & 6 - a & "次机会"
Text1.Text = ""
Case Is = 3, 4, 5
MsgBox "密码错误你还有" & 6 - a & "次机会"
Case Is > 5
MsgBox "您6次输入密码都是错误的,程序将关闭"
End
End Select
End Sub

'我故意将程序改成这样,以些说明此类问题的格式是统一的Case 1是Case Is = 1简略写法,Case 3 To 5是Case Is = 3, 4, 5简略写法,我认为在这样的表达式中Is应看作运算符,尽管和用来比较两个对象的引用变量的Is 运算符有所不同
mokton 2008-12-21
  • 打赏
  • 举报
回复
is 就是你 case 参数的值
加载更多回复(3)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧