扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Public Class dogClass dog Private s_name As String Private s_color As Color Private s_age As Integer Public Sub New()Sub New() s_name = "塞北的雪" s_age = 22 s_color = System.Drawing.Color.Red End Sub Public Property Name()Property Name() As String Get Return s_name End Get Set(ByVal value As String) s_name = value End Set End Property Public Property Color()Property Color() As Color Get Return s_color End Get Set(ByVal value As Color) s_color = value End Set End Property Public Property Age()Property Age() As Integer Get Return s_age End Get Set(ByVal value As Integer) s_age = value End Set End Property Public Shared Operator +(ByVal a As dog, ByVal b As dog) Dim c As New dog c.Name = a.Name + b.Name c.Age = a.Age + b.Age c.Color = Color.FromArgb((a.Color.ToArgb + b.Color.ToArgb) / 2) Return c End Operator End Class |
Dim a As New dog Dim b As New dog() If a Is b Then MsgBox("True") Else MsgBox("False") End If b = a b.Name = "NorthSnow" If a Is b Then MsgBox("True") Else MsgBox("false") End If 'false 'true |
Dim sb As New System.Text.StringBuilder Dim m As New dog Dim n As New dog Dim c As dog m.Name = "NorthSnow" c = m + n sb.AppendLine() sb.Append(m.Name) sb.Append("--") sb.Append(m.Age) sb.Append("--") sb.Append(m.Color.ToArgb) sb.AppendLine() sb.Append(n.Name) sb.Append("--") sb.Append(n.Age) sb.Append("--") sb.Append(n.Color.ToArgb) sb.AppendLine() sb.Append(c.Name) sb.Append("--") sb.Append(c.Age) sb.Append("--") sb.Append(c.Color.ToArgb) MsgBox(sb.ToString) 'NorthSnow--22---65536 '塞北的雪--22---65536 'NorthSnow塞北的雪--44---65536 |
3、like操作符
like 操作符用于判断一个字符串与另外一个字符串是否匹配,大小写敏感。语法是:
Result=String like Pattern |
例子如下:
Dim a As String = "Northsnow,塞北的雪" Dim b As String = "Northsnow,塞北的雪" If a Like b Then MsgBox("true") Else MsgBox("false") End If b = "northsnow,塞北的雪" If a Like b Then MsgBox("true") Else MsgBox("false") End If 'true 'false |
另外,Pattern支持通配符。这一点肯定让我们想起了正则表达式(Regular Expression)。不过我们千万不要搞混了,虽然有些地方相近,但是有些通配符与正则表达式不太一样,功能也有区别。like操作符支持的通配符共有5种:
? 表示一个任意字符(1个)
* 任意个任意字符(0-n个)
# 表示一个数字
[charlist] 表示 charlist 中的任意一个字符
[!charlist] 表示 不在charlist 中的任意一个字符
用过正则的朋友一定会很容易的理解的,有几个核心的规则一定要记牢,那就是:
(1) * 可以匹配任何字符串
(2) ?可以匹配任何一个字符
(3) 而# 相当于[0-9] 只能匹配一个数字 如果要匹配两个数字需要写成 ##
(4) 字符列表中的字符可以用-提供一个区间,以简化书写。
(5) 大小写敏感
下面就举几个例子说明一下:
Dim a As String = "Northsnow.123" Dim b As String = "*.###" MsgBox(a Like b) 'True a = "Northsnow.123" b = "[A-Z][a-z]*#" MsgBox(a Like b) 'True a = "Northsnow.123" b = "[A-Z][a-z]*[a-z]" MsgBox(a Like b) 'False a = "Northsnow.123" b = "[A-Z][a-z]*[0-9]" MsgBox(a Like b) 'True |
4、其它的一些特殊操作符
AndAlse ,OrElse 与 and 和 or 差不多,只是他们比 后者更智能,当前面的判断已经确定了返回值的时候,后面的表达式的值就不再计算了。
TypeOf 返回当前对象的类型,每一个类都有一个getType方法与它很相似,据一个小例子如下:
Dim a As New dog Dim b As New dog Dim c As Type c = a.GetType MsgBox(c.ToString) If TypeOf (a) Is vb1.Form7.dog Then MsgBox(True) End If 'vb1.Form7+dog 'True |
就说这么多吧。已有在应用中如果在总结出什么好的经验,再慢慢补充。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。