vb.net中的操作符应用实例
2006-12-16 16:18
519 查看
任何一门语言都提供了丰富的操作符,vb也不例外。操作符一般分为:算术运算符,赋值运算符,比较运算符,逻辑运算符,其它。本文只以几个小例子来介绍其中一些比较重要的应用。
在本文中将会用到一个自定义类 dog ,代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
Public Class dogClass dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_name As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_color As Color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_age As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Sub New()Sub New()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_name = "塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_age = 22
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_color = System.Drawing.Color.Red
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Name()Property Name() As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_name = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Color()Property Color() As Color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As Color)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_color = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Age()Property Age() As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As Integer)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_age = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Public Shared Operator +(ByVal a As dog, ByVal b As dog)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim c As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Name = a.Name + b.Name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Age = a.Age + b.Age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Color = Color.FromArgb((a.Color.ToArgb + b.Color.ToArgb) / 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Operator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
End Class
1,Is 运算符
Is运算符比较两个引用对象是否引用了同一实例。 例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As New dog()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Is b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("True")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("False")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b.Name = "NorthSnow"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Is b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("True")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2,操作符重载
我们可以根据需要对操作符进行重载,本文中的 dog 类 就 对 + 操作符 进行了重载 操作。此时对两个dog 类进行相加时,会返回一个dog 类实例 ,他的 Name 是 那两个 dog 实例的 名字相加,年龄也是 dog 实例的年龄之和,颜色是两个颜色的平均值。例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim sb As New System.Text.StringBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim m As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim n As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim c As dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
m.Name = "NorthSnow"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c = m + n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(sb.ToString)
'NorthSnow--22---65536
'塞北的雪--22---65536
'NorthSnow塞北的雪--44---65536
3,like操作符
like 操作符用于判断一个字符串与另外一个字符串是否匹配,大小写敏感。语法是:
Result=String like Pattern
例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As String = "Northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As String = "Northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Like b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("true")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Like b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("true")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'false
另外,Pattern支持通配符。这一点肯定让我们想起了正则表达式(Regular Expression)。不过我们千万不要搞混了,虽然有些地方相近,但是有些通配符与正则表达式不太一样,功能也有区别。like操作符支持的通配符共有5种:
? 表示一个任意字符(1个)
* 任意个任意字符(0-n个)
# 表示一个数字
[charlist] 表示 charlist 中的任意一个字符
[!charlist] 表示 不在charlist 中的任意一个字符
用过正则的朋友一定会很容易的理解的,有几个核心的规则一定要记牢,那就是:
(1) * 可以匹配任何字符串
(2) ?可以匹配任何一个字符
(3) 而# 相当于[0-9] 只能匹配一个数字 如果要匹配两个数字需要写成 ##
(4) 字符列表中的字符可以用-提供一个区间,以简化书写。
(5) 大小写敏感
下面就举几个例子说明一下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As String = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As String = "*.###"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*#"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*[a-z]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*[0-9]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
4,其它的一些特殊操作符
AndAlse ,OrElse 与 and 和 or 差不多,只是他们比 后者更智能,当前面的判断已经确定了返回值的时候,后面的表达式的值就不再计算了。
TypeOf 返回当前对象的类型,每一个类都有一个getType方法与它很相似,据一个小例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim c As Type
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c = a.GetType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(c.ToString)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If TypeOf (a) Is vb1.Form7.dog Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(True)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'vb1.Form7+dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
就说这么多吧。已有在应用中如果在总结出什么好的经验,再慢慢补充。
在本文中将会用到一个自定义类 dog ,代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
Public Class dogClass dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_name As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_color As Color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private s_age As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Sub New()Sub New()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_name = "塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_age = 22
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_color = System.Drawing.Color.Red
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Name()Property Name() As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_name = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Color()Property Color() As Color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As Color)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_color = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Property Age()Property Age() As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return s_age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Set(ByVal value As Integer)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_age = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Public Shared Operator +(ByVal a As dog, ByVal b As dog)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim c As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Name = a.Name + b.Name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Age = a.Age + b.Age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c.Color = Color.FromArgb((a.Color.ToArgb + b.Color.ToArgb) / 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Operator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
End Class
1,Is 运算符
Is运算符比较两个引用对象是否引用了同一实例。 例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As New dog()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Is b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("True")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("False")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b.Name = "NorthSnow"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Is b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("True")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2,操作符重载
我们可以根据需要对操作符进行重载,本文中的 dog 类 就 对 + 操作符 进行了重载 操作。此时对两个dog 类进行相加时,会返回一个dog 类实例 ,他的 Name 是 那两个 dog 实例的 名字相加,年龄也是 dog 实例的年龄之和,颜色是两个颜色的平均值。例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim sb As New System.Text.StringBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim m As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim n As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim c As dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
m.Name = "NorthSnow"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c = m + n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(m.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(n.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.AppendLine()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append("--")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sb.Append(c.Color.ToArgb)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(sb.ToString)
'NorthSnow--22---65536
'塞北的雪--22---65536
'NorthSnow塞北的雪--44---65536
3,like操作符
like 操作符用于判断一个字符串与另外一个字符串是否匹配,大小写敏感。语法是:
Result=String like Pattern
例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As String = "Northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As String = "Northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Like b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("true")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "northsnow,塞北的雪"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If a Like b Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("true")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox("false")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'false
另外,Pattern支持通配符。这一点肯定让我们想起了正则表达式(Regular Expression)。不过我们千万不要搞混了,虽然有些地方相近,但是有些通配符与正则表达式不太一样,功能也有区别。like操作符支持的通配符共有5种:
? 表示一个任意字符(1个)
* 任意个任意字符(0-n个)
# 表示一个数字
[charlist] 表示 charlist 中的任意一个字符
[!charlist] 表示 不在charlist 中的任意一个字符
用过正则的朋友一定会很容易的理解的,有几个核心的规则一定要记牢,那就是:
(1) * 可以匹配任何字符串
(2) ?可以匹配任何一个字符
(3) 而# 相当于[0-9] 只能匹配一个数字 如果要匹配两个数字需要写成 ##
(4) 字符列表中的字符可以用-提供一个区间,以简化书写。
(5) 大小写敏感
下面就举几个例子说明一下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As String = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As String = "*.###"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*#"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*[a-z]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = "Northsnow.123"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b = "[A-Z][a-z]*[0-9]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(a Like b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
4,其它的一些特殊操作符
AndAlse ,OrElse 与 and 和 or 差不多,只是他们比 后者更智能,当前面的判断已经确定了返回值的时候,后面的表达式的值就不再计算了。
TypeOf 返回当前对象的类型,每一个类都有一个getType方法与它很相似,据一个小例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim a As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim b As New dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Dim c As Type
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c = a.GetType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(c.ToString)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
If TypeOf (a) Is vb1.Form7.dog Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MsgBox(True)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'vb1.Form7+dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
'True
就说这么多吧。已有在应用中如果在总结出什么好的经验,再慢慢补充。
相关文章推荐
- 技巧 vb.net中的操作符应用实例
- [VB.NET源码]Instant VB.NET实例解析应用编程(PDF)
- VBdotnet2005 TCP IP System.Net.Sockets应用实例
- [VB.NET源码]WebBrowser实例应用
- VB.Net中应用ArrayList实例
- VB.NET应用程序中多线程的应用实例
- VB.NET中TextBox的智能感知应用实例
- VB.Net--应用ArrayList实例
- 章鱼哥出品_VB.NET 线程委托应用实例 一个简单的例子让你轻松掌握委托的用法
- 在VB.NET中应用SQLDMO
- VB.NET拷贝整个目录下所有子目录及文件的实例代码
- 正则表达式在VB.NET中的应用
- asp.net反射简单应用实例
- DATALIST分页的实例(VB.NET)
- 只允许创建一个实例的VB.NET窗口
- C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
- vb.net 教程 5-12 绘图实例之统计图 2 折线图
- ASP.NET弹出页面的应用(VB)
- VB.NET多线程开发实例
- JavaScript delete操作符应用实例