Sie sind auf Seite 1von 11

GREATFRIENDS VTALKS #8

 VB Language Versions  Extension Method

 Local Type Inference  Query Expression


 Nullable Types  Lambda Expression
 If()
 Partial Methods
 Object Initialization  Friend Assembly
Expression
 Anonymous Types

GreatFriends VTALKS #8 The New VB 2008 Language Features 2

1
Visual Studio 6  VB 6.0
Visual Studio .NET 2002  VB 7.0 (.NET 1.0)
Visual Studio .NET 2003  VB 7.1 (.NET 1.1)
Visual Studio 2005  VB 8.0 (.NET 2.0)
Visual Studio 2008  VB 9.0 (.NET 3.5)

GreatFriends VTALKS #8 The New VB 2008 Language Features 3

Dim x1 = 0 „ --- what is the x1‟s data type?


Dim x2 = 5.5
Dim x3 = 0.0
Dim x4 = “a”c
Dim x5 = “GreatFriends” ‘ --- New Option statement
Dim x6 = x5.Length()
Dim x7 = x5.ToCharArray() Option Infer On|Off

Dim x8
x8 = 9900100200

What the x8‟s data type?


GreatFriends VTALKS #8 The New VB 2008 Language Features 4

2
„ --- VB 8.0
Dim n As Nullable(Of Integer) = Nothing
Dim m As Integer
If (n.HasValue) Then
m = n.Value // c# 2.0
Else int? n = null;
m=0 int m = n ?? 0;
End If

„ --- VB 9.0
Dim n As Integer? = Nothing
Dim m = If (n, 0)
GreatFriends VTALKS #8 The New VB 2008 Language Features 5

 If (bool, obj1, obj2) 1 -- likes IIF (immediate if) but short-


circuit
If (bool) then
Return(obj1)
Else
Return(obj2)
 If (obj1, obj2) 2 -- likes Nz() and IsNull()
If (obj1 Is Nothing) Then
Return(obj2)
Else
Return(obj1)

GreatFriends VTALKS #8 The New VB 2008 Language Features 6

3
Dim n = 120
Dim d = 4

Dim X1 = n / d
Dim X2 = If(d <> 0, n/d, 0)
Dim X3 = Iif(d <> 0, n/d, 0)

d=0
Dim X4 = n / d
Dim X5 = If(d <> 0, n/d, 0)
Dim X6 = Iif(d <> 0, n/d, 0)

What are the X‟s result value and its data type?

GreatFriends VTALKS #8 The New VB 2008 Language Features 7

Class Ticket
Public UserName As String
Public MessageId As Integer
Named Type
End Class

Dim t1 As New Ticket With { _ Dim t1 As Ticket


.UserName = “Iko_ba”, _ t1 = New Ticket()
.MessageId = 72896} t1.UserName = “Iko_ba”
t1.MessageId = 72896

{ Initializer List … }
GreatFriends VTALKS #8 The New VB 2008 Language Features 8

4
Class Ticket
Public UserName As String
Public MessageId As Integer
Public Sub New(UserName As String)
Me.UserName = UserName
End Sub
End Class

Dim t1 As New Ticket(“Iko_ba”) With { _


.MessageId = 72896}

GreatFriends VTALKS #8 The New VB 2008 Language Features 9

Class Ticket
Public UserName As String
Public MessageId As Integer
Public TicketString As String
End Class

Dim t1 As New Ticket With { _


.UserName = “Iko_ba”, _
.MessageId = 72896, _
.TicketString = .MessageId & “|” & .UserName }

GreatFriends VTALKS #8 The New VB 2008 Language Features 10

5
Public Class Message
Public Id As Integer
Public DateCreated As Date
End Class
Class Ticket
Public Owner As String
Public Msg As Message
Public TicketString As String
End Class

Dim t3 As New Ticket With { _


.Owner = "suthep", _
.Msg = New Message With { .Id = 72896,.DateCreated = Now() }, _
.TicketString = .Msg.Id & "|" & .Owner }
GreatFriends VTALKS #8 The New VB 2008 Language Features 11

Dim t1 = New With { _ Anonymous Type


.UserName = “Iko_ba”, _
.MessageId = 72896, _
.TicketString = .MessageId & “|” & .UserName }
MsgBox(t1.TicketString) „ --- “72896|Iko_ba”

Dim t2 = New With { _


Key .UserName = “Iko_ba”, _
Key .MessageId = 72896, _
.TicketString = .MessageId & “|” & .UserName }
t2.UserName = “Suthep” „ Error. Key Property is read-only
Comparison sample  If (t2.Equals(t3)) Then …
GreatFriends VTALKS #8 The New VB 2008 Language Features 12

6
Imports System.Runtime.CompilerServices

<Extension()> _
Public Function Bold(ByVal text As String) As String
Return ("<b>" & text & "</b>")
End Function

Dim userName As String = “surrealist”


spanUser.InnerHtml = userName.Bold()

GreatFriends VTALKS #8 The New VB 2008 Language Features 13

<Extension()> _
Public Function ToCommaList(ByVal arr() As String) As String
If (arr.Length = 0) Then Return (String.Empty)
Dim out As New StringBuilder()
out.AppendFormat("Found {0} items:", arr.Length)
out.AppendLine()
out.Append(arr(0))
For n As Integer = 1 To arr.Length - 1
out.Append(", " & arr(n))
Next
out.AppendLine(".")
Return (out.ToString())
End Function

GreatFriends VTALKS #8 The New VB 2008 Language Features 14

7
<Extension()> _
Public Function ToLines(ByVal raw As String) As String()
Dim lines() As String
lines = raw.Split(New Char() {Chr(10), Chr(13)}, _
StringSplitOptions.RemoveEmptyEntries)
Return (lines)
End Function

Dim lines() = My.Computer.FileSystem.ReadAllText(“C:\Data.txt”).ToLines()


Dim lines2() = ToLines(My.Computer.FileSystem.ReadAllText(“C:\Data.txt”))

GreatFriends VTALKS #8 The New VB 2008 Language Features 15

A lambda expression is a function without a name that


calculates and returns a single value.

Lambda expression:
Function (num As Integer) num + 1

Think like this:


Function NoName(num As Integer)
Return(num + 1)
End Function

GreatFriends VTALKS #8 The New VB 2008 Language Features 16

8
A lambda expression is a function without a name that
calculates and returns a single value.

Lambda expression:
Function (num As Integer) num + 1

Think like this:


Function NoName(num As Integer) As Integer
Return(num + 1)
End Function

GreatFriends VTALKS #8 The New VB 2008 Language Features 17

Predicate is a function (or lambda expression) that return boolean

Dim numbers() As String = New String() { _


"one", "two", "three", "four", _
"five", "six", "seven", "eight", "nine"}

Dim result = Array.FindAll(numbers, _


Function(s As String) s.Length <= 4)
Debug.Print(result.ToCommaList())

Result:
Found 6 items:
one, two, four, five, six, nine.

GreatFriends VTALKS #8 The New VB 2008 Language Features 18

9
Dim numbers() As String = New String() { _
"one", "two", "three", "four", _
"five", "six", "seven", "eight", "nine"}
Think in your head:
For Each n In Number …
Dim q = From n In numbers _
Where n.Length <= 4
Dim q = numbers _
.Where(Function(n) n.Length <= 4) _
.Select(Function(n) n)

Debug.Print(q.ToCommaList())
GreatFriends VTALKS #8 The New VB 2008 Language Features 19

Dim q = From n In numbers _


Where n.Length <= 4 _
Select New With { .Name = n, .Length = n.Length }

For Each item In q


Debug.Print(“{0} ({1})”, item.Name, item.Length)
Next

GreatFriends VTALKS #8 The New VB 2008 Language Features 20

10
 It’s time for questions and answers.

 Don’t forget evaluation form.

 Download slides and demo programs at GreatFriends.Biz

GreatFriends VTALKS #8 The New VB 2008 Language Features 21

11

Das könnte Ihnen auch gefallen