VB2010新特性

1.取消了连接符
(1)","之后
(2)"()"前后
(3)"{}"前后
(4)XML
(5)连接字符"&"后
(6)赋值符号"=","&=",":=","+=","-=","*=","/=","\=","^=","<<=",">>="后
(7)式内の二項演算子"+","-","/","*","Mod","<>","<",">","<=",">=","^",">>","<<","And","AndAlso","Or","OrElse","Like","Xor",后
(8)Is,IsNot后
(9)成员修饰符"."后
(10)XML的关键字".",".@","...",后
(11)属性标记的前后
(12)查询关键字的前后
(13)For Each的In关键字后
(14)集合的From关键字后

2.自动执行属性 - 为getter和setter省略的属性

Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property Name As String


3.初始化数组,使用From{元素}方式给集合对象赋初始值

Dim statuses As New Dictionary(Of Integer, String)

statuses.Add(1, "申请")
statuses.Add(2, "科长确认")
statuses.Add(3, "部长确认")
statuses.Add(50, "已确认")
statuses.Add(80, "返回")
statuses.Add(99, "否决")

Dim statuses As New Dictionary(Of Integer, String) From
{
{1, "申请"},
{2, "科长确认"},
{3, "部长确认"},
{50, "已确认"},
{80, "返回"},
{99, "否决"}
}

' 定义
Public Class OS
Public Property Version As Single
Public Property Name As String
End Class

……省略……

' 变量的定义
Dim oses As New List(Of OS) From
{
New OS With {.Name = "Windows XP", .Version = 5.1},
New OS With {.Name = "Windows Vista", .Version = 6},
New OS With {.Name = "Windows 7", .Version = 6.1}
}

 

4.定义多行Lambda表达式

Dim r = 20.0
Dim pi = Function(birthday As Date)
If birthday.Year > 1990 AndAlso
    birthday.Year < 2000 Then
    Return 3
Else
    Return 3.14
End If
End Function
Console.WriteLine(pi(Date.Today) * r * r)

 

5.协变性和逆变性
协变性(协方差)意味着你可以为一个参数,一个子类型或继承自已定义类型的某些事物使用更多的派生类型,
逆变性意味着只能使用更少的类型,
因为.NET中的一切都派生自对象,因此你可以使用对象替换一个参数。

'
Public Class Person
Public Property Name As String
Public Property Age As Integer
End Class

' 员共 - 继承于人
Public Class Employee
Inherits Person
End Class

' 顾客 - 继承于人
Public Class Customer
Inherits Person
End Class

' 协变性的例子,将员工(Employee)的实例赋值于人(Person)
Dim sato As New Employee With {.Name = "刘英", .Age=32}
Dim man As Person = sato

 

Class Pet
Public Name As String
End Class

Class NameCompare
Implements IComparer(Of Pet)
' ...
End Class

Class Dog
Inherits Pet
End Class

Class Cat
Inherits Pet
End Class

Module Module1
Sub Main()
Dim DogNames As New SortedList(Of Dog, Object)(New NameCompare)
Dim CatNames As New SortedList(Of Cat, Object)(New NameCompare)
End Sub
End Module

' 逆变性的例子,
'VB2008中,需要传递IComparer(Of Dog)或IComparer(Of Cat),利用逆变性传递IComparer(Of Pet)就可以

 

6.动态对象

' 无需定义直接读取MaxRetry属性的值
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MaxRetry" value ="10" />
</appSettings>
</configuration>

Dim appSettings As Object = New SettingManager()
Console.WriteLine(appSettings.MaxRetry)

' 继承DynamicObject,重写TryGetMember方法
Public Class SettingManager
Inherits DynamicObject

' Get访问属性
Public Overrides Function TryGetMember(ByVal binder As System.Dynamic.GetMemberBinder, ByRef result As Object) As Boolean
result = System.Configuration.ConfigurationManager.AppSettings(binder.Name)
Return True
End Function
End Class


' 可以动态执行属性或方法
Dim user As Object = New ExpandoObject

user.Name = "赵鹏"
user.Show = Sub()
Console.WriteLine(user.Name)
End Sub
user.Show.Invoke()
CType(user, IDictionary(Of String, Object)).Remove("Name")

 



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
四知四清四握信息管理系统软件注册 编辑 | 删除 | 权限设置 | 更多▼ 更多▲ 设置置顶 推荐日志 转为私密日志 hamit 发表于2010年06月12日 16:15 阅读(2) 评论(0) 分类: 个人日记 权限: 公开 Imports System.Management Imports System.IO Public Class tizimlitix Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Trim(TextBox2.Text) = "" Then MsgBox("你必须输入<注册码>", MsgBoxStyle.Exclamation) Exit Sub End If If Trim(TextBox2.Text) = Trim(TextBox1.Text) * 2 - 9950019 Then ' 存入(机器码) If File.Exists(Application.StartupPath + "\tizim_id.txt") Then File.Delete(Application.StartupPath + "\tizim_id.txt") End If Dim sr As StreamWriter = File.CreateText(Application.StartupPath + "\tizim_id.txt") sr.WriteLine(TextBox2.Text) sr.Close() '存入(注册码) If File.Exists(Application.StartupPath + "\pc_id.txt") Then File.Delete(Application.StartupPath + "\pc_id.txt") End If Dim sr2 As StreamWriter = File.CreateText(Application.StartupPath + "\Pc_id.txt") sr2.WriteLine(TextBox1.Text) sr2.Close() MsgBox("注册成功!程序将要重新启动!", MsgBoxStyle.Exclamation) Me.Close() End Else MsgBox("注册码不正确!请软件开发者联系 13899326632", MsgBoxStyle.Exclamation) End If End Sub Private Sub tizimlitix_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive") '获得硬盘序列号 2010-6-8 tordin izdap taptim hamdulla mahmut 13909950019 Dim Uint32 As UInt32 For Each cmicWmiObj As ManagementObject In cmicWmi.Get Uint32 = cmicWmiObj("signature") Next TextBox1.Text = Uint32.ToString * 2 - 13909950019 '本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/21aspnet/archive/2004/10/29/159124.aspx TextBox1.Enabled = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值