Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lucian Wischik SESSION CODE: DEV401. Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik, VB spec lead.

Similar presentations


Presentation on theme: "Lucian Wischik SESSION CODE: DEV401. Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik, VB spec lead."— Presentation transcript:

1 Lucian Wischik SESSION CODE: DEV401

2 Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik, VB spec lead

3 “Headliner language features will be done for both langs… We pursue parity for MS samples and content.” [scottwil]

4 VB 9: Dim f = GetType(Fred). _ GetConstructor(New Object() {}). _ Invoke(New Object() {}) VB 10: Dim f = GetType(Fred).GetConstructor({}).Invoke({})

5 VB 9: Dim x As New List(Of String) Dim y As List(Of Object) = x ' List(Of String) cannot be converted to List(Of Object) VB 10: Dim x As New List(Of String) Dim y As List(Of Object) = x ' List(Of String) cannot be converted to List(Of Object) ' Consider using IEnumerable(Of Object) instead Dim y2 As IEnumerable(Of Object) = x ' Works fine!

6

7 Demo DEMO The bulk of the talk is a live demo. The following slides are just placeholders, to indicate the content of the demo.

8 Demo: Nothing (1) [VS2008] Dim x As Boolean? = Nothing If x = Nothing Then Console.WriteLine("is nothing") What does it do? -doesn’t print anything! Why? - Read on!...

9 Demo: Nothing (2) [VS2010] Dim x As Boolean? = Nothing If x = Nothing Then Console.WriteLine("is nothing") ' Warning: This expression will always evaluate to Nothing ' (due to null-propagation). To check if the value ' is null, consider using "Is Nothing" Well that’s why it didn’t print anything! - What does it mean?

10 Demo: Nothing (3) Dim y As Integer? = 5 Dim z = y + Nothing Dim z2 = y * Nothing Console.WriteLine(z.HasValue) Console.WriteLine(z2.HasValue) Nothing means “I don’t know what value it has” (like SQL) - so z and z2 are both “I don’t know” also - So this prints “False” and “False” again

11 Demo: Nothing (4) Dim z? = If(False, 15, Nothing) Console.WriteLine(z.HasValue) Console.WriteLine(z.Value) This is a different “Nothing” issue -The dominant type is “Integer”, not Nullable(Of Integer) -It interprets “Nothing” as 0 -And so prints “True” and “0”

12 Demo: Nothing (5) Private _AccessLevel As Integer? Public Property AccessLevel() As Integer? Get Return _AccessLevel End Get Set(ByVal value As Integer?) If _AccessLevel Is Nothing OrElse _AccessLevel <> value Then _AccessLevel = value End Set End Property AccessLevel = 5 AccessLevel = Nothing Console.WriteLine(AccessLevel) - This prints “5”. Can you see the bug? Can you see how to fix it?

13 Demo: Array trick (1) Module Module1 Sub Main() Dim x As New List(Of String) From { "one", "two", "three"} Dim y As FixedList(Of String) = {"one", "two", "three"} End Sub End Module -“x” is using the Collection Initializer feature, new in VB10 -“y” is a trick with Array Literals (new in VB10) and a user-defined conversion...

14 Demo: Array trick (2) Class FixedList(Of T) Private _list As New List(Of T) Public Sub New(ByVal x As IEnumerable(Of T)) _list.AddRange(x) End Sub Public Shared Widening Operator CType(ByVal x As T()) As FixedList(Of T) Return New FixedList(Of T)(x) End Operator Default Public ReadOnly Property Item(ByVal i As Integer) As T Get Return _list(i) End Get End Property End Class

15 Demo: Synchronous call Dim web = Net.WebRequest.Create("http://blogs.msdn.com/lucian") Using r = web.GetResponse, s = New IO.StreamReader(r.GetResponseStream) Console.WriteLine(s.ReadToEnd) End Using This is the basic synchronous way to write this code. -How would we make it asynchronous? -With multiline statement lambdas (new in VB10)!...

16 Demo: Asynchronous call Dim web = Net.WebRequest.Create("http://blogs.msdn.com/lucian") web.BeginGetResponse(Sub(ir) Using _ r = web.EndGetResponse(ir), s = New IO.StreamReader(r.GetResponseStream) Console.WriteLine(s.ReadToEnd) End Using End Sub, Nothing) Use of multiline statement lambdas -Indispensible for asynchronous programming, Task Parallel Library,...

17 Demo: Multiline Lambdas Dim x = Function() If Rnd() > 0.5 Then Return 1 Else Return 2.5 End If End Function What is the return type of this lambda? -Here it picks “Double” -In general it picks the DOMINANT TYPE of all return arguments -Dominant type is also used during generic type inference, and in If(...) operator -NB. “Nothing” doesn’t influence the choice of dominant type

18 Demo: Dynamic (1) Module Module1 Sub Main() Dim o As Object = New Bag o.x = 15 o.y = 20 o.z = 35 Console.WriteLine(o.x + o.y + o.z) End Sub End Module This is a simple use of DLR interop

19 Demo: Dynamic (2) Class Bag Inherits Dynamic.DynamicObject Private _d As New Dictionary(Of String, Integer) Public Overrides Function TrySetMember(ByVal binder As System.Dynamic.SetMemberBinder, ByVal value As Object) As Boolean _d(binder.Name) = CInt(value) Return True End Function Public Overrides Function TryGetMember(ByVal binder As System.Dynamic.GetMemberBinder, ByRef result As Object) As Boolean Return _d.TryGetValue(binder.Name, result) End Function End Class This is the basic Bag class.

20 Demo: Dynamic (3) Dim s As String = o Console.WriteLine(s) Dim s2 = CTypeDynamic(Of String)(o) Console.WriteLine(s2) Tricky question: how would we make “o” behave a string, so we could print it?... -NB. CTypeDynamic is new in VB10. It includes dynamic and user-defined conversions. -Ctype doesn’t include either

21 Demo: Dynamic (4) C#: string s = d; -- TryConvert C#: var x = "hello" + d; -- ToString VB: Dim s as String = d -- IConvertible VB: Dim s = CTypeDynamic(d) – TryConvert VB: var x = "hello" & d -- IConvertible, else operator & VB: var x = "hello" + d -- IConvertible, else operator + It’s a bit of a mess. Here are the different ways needed to make something string-like: Public Overrides Function TryConvert(ByVal binder As System.Dynamic.ConvertBinder, ByRef result As Object) As Boolean If binder.Type = GetType(String) Then result = "tryconverted" : Return True Return MyBase.TryConvert(binder, result) End Function Public Overrides Function ToString() As String Return "tostringed" End Function Public Function GetTypeCode() As System.TypeCode Implements System.IConvertible.GetTypeCode Return System.TypeCode.String End Function Public Function ToString1(ByVal provider As System.IFormatProvider) As String Implements System.IConvertible.ToString Return "iconvertibled" End Function

22 DEV307: F# in Microsoft Visual Studio 2010 (6/10 from 9:45am – 11:00am) DEV315: Microsoft Visual Studio 2010 Tips and Tricks (6/8 from 5:00pm – 6:15pm) DEV316: Modern Programming with C++Ox in Microsoft Visual C++ 2010 (6/8 from 3:15pm – 4:30pm) DEV319: Scale and Productivity for C++ Developers with Microsoft Visual Studio 2010 (6/9 from 8:00am – 9:15am) DEV401: Advanced Use of the new Microsoft Visual Basic 2010 Language Features (6/9 from 9:45am – 11:00am) DEV404: C# in the Big World (6/8 from 1:30pm – 2:45pm) DEV406: Integrating Dynamic Languages into Your Enterprise Applications (6/8 from 8am – 9:15am) DEV407: Maintaining and Modernizing Existing Applications with Microsoft Visual Studio 2010 (6/10 from 8:00am – 9:15am)

23 DEV03-INT: Meet the C# team (6/9 from 1:30-2:45pm) DEV04-INT: Meet the VB team (6/10 from 3:15 – 4:30pm) DEV09-INT: Microsoft Visual Basic and C# IDE Tips and Tricks (6/7 from 4:30pm -5:45pm) DEV10-INT: Using Dynamic Languages to build Scriptable Applications ((6/9 from 8:00am -9:15am) DEV11 –INT: IronPython Tools (6/10 from 5:00pm – 6:15pm) DEV05-HOL: Introduction to F#

24 Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub.

25 www.microsoft.com/teched www.microsoft.com/learning http://microsoft.com/technet http://microsoft.com/msdn

26

27 Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31 st http://northamerica.msteched.com/registration You can also register at the North America 2011 kiosk located at registration Join us in Atlanta next year

28

29


Download ppt "Lucian Wischik SESSION CODE: DEV401. Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik, VB spec lead."

Similar presentations


Ads by Google