SESSION CODE DEV 401 Lucian Wischik Advanced Use
- Slides: 29
SESSION CODE: DEV 401 Lucian Wischik
Advanced Use of the New Microsoft Visual Basic 2010 Language Features Lucian Wischik, VB spec lead
“Headliner language features will be done for both langs… We pursue parity for MS samples and content. ” [scottwil]
VB 9: Dim f = Get. Type(Fred). _ Get. Constructor(New Object() {}). _ Invoke(New Object() {}) VB 10: Dim f = Get. Type(Fred). Get. Constructor({}). Invoke({})
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 y 2 As IEnumerable(Of Object) = x ' Works fine!
Demo DEMO The bulk of the talk is a live demo. The following slides are just placeholders, to indicate the content of the demo.
Demo: Nothing (1) [VS 2008] Dim x As Boolean? = Nothing If x = Nothing Then Console. Write. Line("is nothing") What does it do? - doesn’t print anything! Why? - Read on!. . .
Demo: Nothing (2) [VS 2010] Dim x As Boolean? = Nothing If x = Nothing Then Console. Write. Line("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?
Demo: Nothing (3) Dim y As Integer? = 5 Dim z = y + Nothing Dim z 2 = y * Nothing Console. Write. Line(z. Has. Value) Console. Write. Line(z 2. Has. Value) Nothing means “I don’t know what value it has” (like SQL) - so z and z 2 are both “I don’t know” also - So this prints “False” and “False” again
Demo: Nothing (4) Dim z? = If(False, 15, Nothing) Console. Write. Line(z. Has. Value) Console. Write. Line(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”
Demo: Nothing (5) Private _Access. Level As Integer? Public Property Access. Level() As Integer? Get Return _Access. Level End Get Set(By. Val value As Integer? ) If _Access. Level Is Nothing Or. Else _Access. Level <> value Then _Access. Level = value End Set End Property Access. Level = 5 Access. Level = Nothing Console. Write. Line(Access. Level) - This prints “ 5”. Can you see the bug? Can you see how to fix it?
Demo: Array trick (1) Module 1 Sub Main() Dim x As New List(Of String) From { "one", "two", "three"} Dim y As Fixed. List(Of String) = {"one", "two", "three"} End Sub End Module - “x” is using the Collection Initializer feature, new in VB 10 “y” is a trick with Array Literals (new in VB 10) and a user-defined conversion. . .
Demo: Array trick (2) Class Fixed. List(Of T) Private _list As New List(Of T) Public Sub New(By. Val x As IEnumerable(Of T)) _list. Add. Range(x) End Sub Public Shared Widening Operator CType(By. Val x As T()) As Fixed. List(Of T) Return New Fixed. List(Of T)(x) End Operator Default Public Read. Only Property Item(By. Val i As Integer) As T Get Return _list(i) End Get End Property End Class
Demo: Synchronous call Dim web = Net. Web. Request. Create("http: //blogs. msdn. com/lucian") Using r = web. Get. Response, s = New IO. Stream. Reader(r. Get. Response. Stream) Console. Write. Line(s. Read. To. End) End Using This is the basic synchronous way to write this code. How would we make it asynchronous? With multiline statement lambdas (new in VB 10)!. . .
Demo: Asynchronous call Dim web = Net. Web. Request. Create("http: //blogs. msdn. com/lucian") web. Begin. Get. Response(Sub(ir) Using _ r = web. End. Get. Response(ir), s = New IO. Stream. Reader(r. Get. Response. Stream) Console. Write. Line(s. Read. To. End) End Using End Sub, Nothing) Use of multiline statement lambdas Indispensible for asynchronous programming, Task Parallel Library, . . .
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
Demo: Dynamic (1) Module 1 Sub Main() Dim o As Object = New Bag o. x = 15 o. y = 20 o. z = 35 Console. Write. Line(o. x + o. y + o. z) End Sub End Module This is a simple use of DLR interop
Demo: Dynamic (2) Class Bag Inherits Dynamic. Object Private _d As New Dictionary(Of String, Integer) Public Overrides Function Try. Set. Member(By. Val binder As System. Dynamic. Set. Member. Binder, By. Va _d(binder. Name) = CInt(value) Return True End Function Public Overrides Function Try. Get. Member(By. Val binder As System. Dynamic. Get. Member. Binder, By. Re Return _d. Try. Get. Value(binder. Name, result) End Function End Class This is the basic Bag class.
Demo: Dynamic (3) Dim s As String = o Console. Write. Line(s) Dim s 2 = CType. Dynamic(Of String)(o) Console. Write. Line(s 2) Tricky question: how would we make “o” behave a string, so we could print it? . . . NB. CType. Dynamic is new in VB 10. It includes dynamic and user-defined conversions. Ctype doesn’t include either
Demo: Dynamic (4) C#: VB: VB: string s = d; var x = "hello" + d; Dim s as String = d Dim s = CType. Dynamic(d) var x = "hello" & d var x = "hello" + d -- Try. Convert -- To. String -- IConvertible – Try. Convert -- IConvertible, else operator & -- IConvertible, else operator + It’s a bit of a mess. Here are the different ways needed to make something string-like: Public Overrides Function Try. Convert(By. Val binder As System. Dynamic. Convert. Binder, By. Ref result As Object) As Boolean If binder. Type = Get. Type(String) Then result = "tryconverted" : Return True Return My. Base. Try. Convert(binder, result) End Function Public Overrides Function To. String() As String Return "tostringed" End Function Public Function Get. Type. Code() As System. Type. Code Implements System. IConvertible. Get. Type. Code Return System. Type. Code. String End Function Public Function To. String 1(By. Val provider As System. IFormat. Provider) As String Implements System. IConvertible. To. String Return "iconvertibled" End Function
DEV 307: F# in Microsoft Visual Studio 2010 (6/10 from 9: 45 am – 11: 00 am) DEV 315: Microsoft Visual Studio 2010 Tips and Tricks (6/8 from 5: 00 pm – 6: 15 pm) DEV 316: Modern Programming with C++Ox in Microsoft Visual C++ 2010 (6/8 from 3: 15 pm – 4: 30 pm) DEV 319: Scale and Productivity for C++ Developers with Microsoft Visual Studio 2010 (6/9 from 8: 00 am – 9: 15 am) DEV 401: Advanced Use of the new Microsoft Visual Basic 2010 Language Features (6/9 from 9: 45 am – 11: 00 am) DEV 404: C# in the Big World (6/8 from 1: 30 pm – 2: 45 pm) DEV 406: Integrating Dynamic Languages into Your Enterprise Applications (6/8 from 8 am – 9: 15 am) DEV 407: Maintaining and Modernizing Existing Applications with Microsoft Visual Studio 2010 (6/10 from 8: 00 am – 9: 15 am)
DEV 03 -INT: Meet the C# team (6/9 from 1: 30 -2: 45 pm) DEV 04 -INT: Meet the VB team (6/10 from 3: 15 – 4: 30 pm) DEV 09 -INT: Microsoft Visual Basic and C# IDE Tips and Tricks (6/7 from 4: 30 pm -5: 45 pm) DEV 10 -INT: Using Dynamic Languages to build Scriptable Applications ((6/9 from 8: 00 am -9: 15 am) DEV 11 –INT: Iron. Python Tools (6/10 from 5: 00 pm – 6: 15 pm) DEV 05 -HOL: Introduction to F#
http: //www. microsoft. com/visualstudio/en-us/ http: //blogs. msdn. com/b/somasegar/ http: //msdn. com/data http: //blogs. msdn. com/adonet http: //blogs. msdn. com/astoriateam http: //blogs. msdn. com/efdesign
www. microsoft. com/teched www. microsoft. com/learning http: //microsoft. com/technet http: //microsoft. com/msdn
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
- Lucian wischik
- Food code 3-401 be displayed texas
- Damon wischik
- Damon wischik
- Wischik
- Pkt_aggregate
- Code commit code build code deploy
- Lsp401
- Svartpilen 401 dyno
- Cse401
- Ge 401
- Eng m 401
- Clase 401
- 401 branard street houston tx
- Bmb 401
- Comm 401
- 401 relevance
- Hinário adventista 401
- Hd-wp-4k-401-c
- Quantas prendas faltam para a turma 401 empatar com a 402
- Stitch type 401
- Program 401 ploiesti
- Chem 401
- Multi thread chain stitch
- Jul 401 english
- Ir 401
- Heat stabilised film
- Faa p-401
- Csc 401
- Concurrent engineering advantages and disadvantages