Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic 2005: Advanced Language and IDE Features Amanda Silver Program Manager Visual Basic Session Code: DEV343.

Similar presentations


Presentation on theme: "Visual Basic 2005: Advanced Language and IDE Features Amanda Silver Program Manager Visual Basic Session Code: DEV343."— Presentation transcript:

1 Visual Basic 2005: Advanced Language and IDE Features Amanda Silver amandas@microsoft.com Program Manager Visual Basic Session Code: DEV343

2 Agenda Advanced IDE Features Lots of new language features Full platform support

3 Advanced IDE Features Application Designer Settings, Resources Application Events Extensible code snippets Rename Symbol New Async calling convention

4 Configuration and Settings Basic support in VS 2002 and 2003 App.config contains settings in XML Framework class for reading settings Can extend with additional capabilities Straightforward, but not really easy

5 Configuration and Settings Huge improvements in Whidbey Framework classes support read/write Strongly-typed access to settings IntelliSense for settings Choose app-scoped or user-scoped settings Works in partial trust Validation model Extensible provider model Shared infrastructure across client and web

6 Settings Architecture SettingsBase ApplicationSettingsBase WindowsApp1Settings Provider Interface Local Settings RemoteSqlCustomAccessCustom My.Settings

7 Settings Architecture Customizable At the provider level By writing custom settings classes

8

9 Settings Designer allows Creating / modifying / deleting entries Type of entry (for strong typing) Choosing user-scoped or app-scoped Default value Different “profiles” Special support for Connection Strings Web service proxies VS will automatically create settings

10 Settings Architecture App settings User Settings Myapp.exe.config … fred.config … ethel.config … gladys.config …

11 Code Samples LoadSave Handling events for validation My.Settings.UseHighQuality = True ‘ Settings automatically loaded on first access Private Sub Settings_SettingChanging(ByVal sender As Object, ByVal e As SettingsArg) Handles MyBase.SettingChanging If e.SettingName = “SignatureFile” Then If e.SettingName = “SignatureFile” Then If Not My.Computer.FileSystem.FileExists(e.Setting.Value) Then If Not My.Computer.FileSystem.FileExists(e.Setting.Value) Then ‘ Cancel event ‘ Cancel event End If End If End Sub My.Settings.UseHighQuality = True My.Settings.Save()

12 Advanced IDE Features Application Designer Settings Application Events Extensible code snippets Rename Symbol New Async calling convention

13 Application Designer One stop shopping for app settings Resources App and user settings References Version Info Deployment Easier to find Non-modal window for better usability

14 Application Designer New Application Events StartupShutdown Unhandled exception Network connected Network disconnected

15 Advanced IDE Features Application Designer Settings Application Events Extensible code snippets Rename Symbol New Async calling convention

16 Extensible Code Snippets Easy way to package code for reuse Snippet editor For creating, editing snippets Standard, XML based schema Add items to the right-click menu Multiple snippet stores Local or network share Sharing is encouraged!

17 Rename Symbol Quick way to change identifiers Rename all cases of “TextBox1” Type declaration Variable declarations Events More precise than text replacement Doesn’t affect comments Other “refactoring” features queued Create method, generate method, etc.

18 Advanced IDE Features Application Designer Settings Application Events Extensible code snippets Rename Symbol New Async calling convention

19 Asynchronous calling Easier way to call slow background tasks Model asynchronous calls as events PictureBox1.LoadAsync(url) PictureBox1_LoadCompleted(…, …) Completion routine is on main thread Web service proxies Supports progress, cancellation Background worker component

20 Background Worker Structure BackgroundArgs Dim arg1 As Integer Dim arg1 As Integer Dim arg2 As String Dim arg2 As String End Structure Private Sub Button1_Click(…) Handles Button1.Click Dim args As BackgroundArgs Dim args As BackgroundArgs BackgroundWorker1.RunWorkerAsync(args) BackgroundWorker1.RunWorkerAsync(args) End Sub Private Sub BackgroundWorker1_DoWork(…) Handles BackgroundWorker1.DoWork ‘ This is run on a background thread ‘ This is run on a background thread Dim args As BackgroundArgs Dim args As BackgroundArgs args = CType(e.Argument, BackgroundArgs) args = CType(e.Argument, BackgroundArgs) ‘ Now do something on the background thread ‘ Now do something on the background thread e.Result = e.Result = End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(…) Handles Background… result = e.Result result = e.Result End Sub

21 Background Worker Structure BackgroundArgs Dim arg1 As Integer Dim arg1 As Integer Dim arg2 As String Dim arg2 As String End Structure Private Sub Button1_Click(…) Handles Button1.Click Dim args As BackgroundArgs Dim args As BackgroundArgs Worker1.RunWorkerAsync(args) Worker1.RunWorkerAsync(args) End Sub Private Sub Worker1_DoWork(…) Handles Worker1.DoWork ‘ This is run on a background thread ‘ This is run on a background thread Dim args As BackgroundArgs Dim args As BackgroundArgs args = CType(e.Argument, BackgroundArgs) args = CType(e.Argument, BackgroundArgs) ‘ Now do something on the background thread ‘ Now do something on the background thread e.Result = [some object] e.Result = [some object] End Sub Private Sub Worker1_RunWorkerCompleted(…) Handles Worker1… result = e.Result result = e.Result End Sub

22 Advanced IDE Features

23 New Language Features Using statement Continue statement Global keyword Property accessor accessibility Partial types Unsigned types Custom Event Accessors Operator Overloading GenericsWarnings

24 Explicit Array Bounds Arrays are still zero based You can now specify the lower and upper bounds in the declaration ‘In VS 2002 and 2003, you did it this way Dim x(10) As Integer ‘Now you can also declare it like this Dim y(0 To 10) As Integer

25 Using Statement Acquire, Execute, Release Fast way to correctly release resources Easier to read than Try, Catch, Finally Couple with Dispose pattern stub gen ‘Using block disposes of resource Using fStr As New FileStream(path, FileMode.Append) For i As Integer = 0 To fStr.Length For i As Integer = 0 To fStr.Length fStr.ReadByte() fStr.ReadByte() Next Next ‘End of block will close stream ‘End of block will close stream End Using

26 Continue Statement Skips to next iteration of loop Loop logic is concise, easier to read For j As Integer = 0 to 5000 While matrix(j) IsNot thisValue While matrix(j) IsNot thisValue If matrix(j) Is thatValue If matrix(j) Is thatValue ‘ Continue to next j ‘ Continue to next j Continue For Continue For End If Graph(j) Graph(j) End While End While Next j

27 Global Keyword Access to root (empty) namespace Complete name disambiguation Better choice for code generation Namespace HeadTrax Class Form1 Class Form1 Inherits Windows.Forms.Forms Sub LastName(nm As String) Sub LastName(nm As String) Global.Microsoft.VisualBasic.Left(nm) Global.Microsoft.VisualBasic.Left(nm) End Sub End Sub End Class End Class End Namespace

28 Accessor Accessibility Granular accessibility on Get and Set Forces calls to always use get, set Easy way to enforce validation Property Salary() As Integer Get Get Return mSalary End Get End Get Private Set( value As Integer) Private Set( value As Integer) If value < 0 Then Throw New Exception(“Mistake”) Throw New Exception(“Mistake”) End If End Set End Set End Property

29 Partial Types Single Type in Multiple Files Used to separate designer gen into another file Factor implementation Public Class Form1 Inherits Windows.Forms.Form Inherits Windows.Forms.Form ‘ Your Code End Class Partial Class Form1 ‘ Designer code Sub InitializeComponent() ‘ Your controls ‘ Your controls End Sub End Class

30 Unsigned Types Full support in the language Full platform parity Easier Win32 Api calls and translation Memory and performance win Dim sb As SByte = -4 ‘Error:negative Dim us As UShort Dim ui As UInteger Dim ul As ULong ‘Full support in VisualBasic modules If IsNumeric(uInt) Then ‘ Will now return true End If

31 Custom Event Accessors Define custom backing store Custom Event Enter As EnterHandler AddHandler(ByVal value As EnterHandler) AddHandler(ByVal value As EnterHandler) ' Hook up handler to backing store ' Hook up handler to backing store End AddHandler End AddHandler RemoveHandler(ByVal value As EnterHandler) RemoveHandler(ByVal value As EnterHandler) ' Remove handler from backing store ' Remove handler from backing store End RemoveHandler End RemoveHandler RaiseEvent() RaiseEvent() ' Invoke the listeners ' Invoke the listeners End RaiseEvent End RaiseEvent End Event Use just like normal events Private Sub Control1_Enter() Handles Control1.Enter ‘ Handler code here End Sub RaiseEvent Enter()‘ Invoke via RaiseEvent

32 New Language Features

33 Operator Overloading Create your own base types Class Addr Private mString As String Private mString As String Property Value() As String Property Value() As String Get Get Return mString Return mString End Get End Get Set (value As String) Set (value As String) If Valid(value) Then If Valid(value) Then mString = value mString = value End If End If End Set End Set Shared Operator &(ad1 As Addr, ad2 As Addr) _ Shared Operator &(ad1 As Addr, ad2 As Addr) _ As Addr As Addr Return New Addr(ad1.Value & ad2.Value) Return New Addr(ad1.Value & ad2.Value) End Operator End Operator

34 Operator Overloading

35 List intList = new List() intList.Add(1) ‘ Argument is boxed intList.Add(2) ‘ Argument is boxed intList.Add("Three") ‘ Should be an error int i = CInt(intList(0)) ‘ Cast required Generics Public Class List Public Class List Private elements() As ItemType Private elements() As ItemType Private count As Integer Private count As Integer Public Sub Add(element As ItemType) Public Sub Add(element As ItemType) If (count = elements.Length) Then _ If (count = elements.Length) Then _ Resize(count * 2) Resize(count * 2) count += 1 count += 1 elements(count) = element elements(count) = element End Sub End Sub Public Property default(index As Integer) As ItemType Public Property default(index As Integer) As ItemType Get : Return elements(index) : End Get Get : Return elements(index) : End Get Set : elements(index) = value : End Set Set : elements(index) = value : End Set End Property End Property Public Property Count As Integer Public Property Count As Integer Get : Return count : End Get Get : Return count : End Get End Property End Property End Class Public Class List Private elements() As Object Private elements() As Object Private mCount As Integer Private mCount As Integer Public Sub Add(element As Object) Public Sub Add(element As Object) If (mCount = elements.Length) Then _ If (mCount = elements.Length) Then _ Resize(mCount * 2) Resize(mCount * 2) mCount += 1 mCount += 1 elements(mCount) = element elements(mCount) = element End Sub End Sub Default Public Property i(index As Integer) As Object Default Public Property i(index As Integer) As Object Get Get Return elements(index) Return elements(index) End Get End Get Set Set elements(index) = value elements(index) = value End Set End Set End Property End Property Public Property Count() As Integer Public Property Count() As Integer Get : Return mCount : End Get Get : Return mCount : End Get End Property End Property End Class Dim intList As New List intList.Add(1)intList.Add(2)intList.Add("Three") Dim i As Integer = intList(0) Dim intList As New List(Of Integer) intList.Add(1) ‘ No boxing intList.Add(2) ‘ No boxing intList.Add("Three") ‘ Compile-time error int i = intList(0) ‘ No cast required

36 Generics (Specifics) Compile Time Checking Eliminates runtime errors Performance No casting or boxing overhead Code reuse Easy to create strongly typed collections Generic classes in Fx Dictionary, HashTable, List, Stack, etc.

37 Generics

38 Unit Testing Unit tests are tests written by a developer or other programmer during the code development process Typically, Unit tests map very closely to product code in name and scope (one product method, one unit test).

39

40

41 Visual Basic Warnings Early warning of runtime behavior Overlapping catch blocks or cases Recursive property access Unused Imports statement Unused local variable Function, operator without return Reference on possible null reference Option Strict broken down “Late binding” VB Style Conversions Etc.

42 Warnings

43 Summary Ease of use is built on solid infrastructure Many points of extensibility Very powerful new language features And some cool fit and finish ones too Full access to the platform

44 Q & A: We want your feedback!

45 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "Visual Basic 2005: Advanced Language and IDE Features Amanda Silver Program Manager Visual Basic Session Code: DEV343."

Similar presentations


Ads by Google