1 / 29

Advanced Use of the New Microsoft Visual Basic 2010 Language Features

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

kaipo
Télécharger la présentation

Advanced Use of the New Microsoft Visual Basic 2010 Language Features

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SESSION CODE: DEV401 Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik

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

  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 AsNewList(OfString) Dim y AsList(OfObject) = x ' List(Of String) cannot be converted to List(Of Object) VB 10: Dimx AsNewList(OfString) Dimy AsList(OfObject) = x ' List(Of String) cannot be converted to List(Of Object) ' Consider using IEnumerable(Of Object) instead Dim y2 AsIEnumerable(OfObject) = x ' Works fine!

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

  7. 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! ...

  8. 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?

  9. 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

  10. 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”

  11. 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?

  12. 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...

  13. 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

  14. 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)! ...

  15. 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, ...

  16. 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

  17. 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

  18. 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.

  19. 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. CTypeDynamicis new in VB10. It includes dynamic and user-defined conversions. • Ctype doesn’t include either

  20. 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

  21. Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Related Content – Breakout Sessions 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)

  22. Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Related Content – Interactive Sessions and HOL's 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#

  23. Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Track Resources • Visual Studio – http://www.microsoft.com/visualstudio/en-us/ • Soma’s Blog – http://blogs.msdn.com/b/somasegar/ • MSDN Data Developer Center – http://msdn.com/data • ADO.NET Team Blog – http://blogs.msdn.com/adonet • WCF Data Services Team Blog – http://blogs.msdn.com/astoriateam • EF Design Blog – http://blogs.msdn.com/efdesign

  24. Required Slide Resources Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers http://microsoft.com/msdn http://microsoft.com/technet

  25. Required Slide Complete an evaluation on CommNet and enter to win!

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

  27. © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

More Related