DEV 410 Visual Basic NET Advanced Sam Spencer

DEV 410: Visual Basic. NET: Advanced Sam Spencer Program Manager Visual Basic. NET Microsoft Corporation

Agenda l l l Introduction to the Language Inheritance Type Conversions Delegates and Events Threading COM Interop

A Great Basic Language The language you already know for building. NET applications l New language features Ø Ø Ø l Modern – Inheritance, overloading, shadowing, shared members, structured exception handling Robust – Strict type checking, initialize variables at declaration, constructors, variable declaration types fixed Powerful – Delegates, free threading, VBC Simplified and Consistent Ø One form of assignment, “New” is consistent, some old constructs removed

Tour Of Language

VB. NET And The Runtime l VB. NET targets the CLR Ø Compiler generates assemblies § Ø Ø VB Types all map to CLR types Class in VB. NET = Class in C# § Ø Ø l IL is JIT compiled at run or install time Most features are similar in both languages Full access to runtime features Microsoft. Visual. Basic. dll IDE and Command-line compiler Ø Now easy to perform automated build

Classes And Inheritance l Classes support inheritance Ø Ø l l l Derive from a single base class Implicitly inherits from System. Object Inheritance enables extending classes Overriding is explicit using “Overridable” and “Overrides” keywords Classes can be declared as: Ø Ø Not. Inheritable Must. Inherit

Interfaces l l Declared using Interface Keyword Class can implement multiple interfaces Ø l l l Implementing member name doesn’t have to match Describes a contract for functionality Does not provide any implementation Support multiple Inheritance

Abstract Classes l l l Variables can be declared using type Instances cannot be created Declared using Must. Inherit Can store/represent data Members can provide implementation Members can be abstract Ø Use Must. Override keyword

Inheritance Demo

When To Use Inheritance l l Don’t go overboard Inheritance Ø Use when § § § l The derived class needs to act like base class Need to extend functionality of base class Could be considered specialization of base class Encapsulation Ø Ø Model used in Visual Basic® 6 Use when § § You need functionality provided by class Class “is kind of” base is not true

Overriding l l l Provide a new implementation for method declared in base class Use “Overrides” keyword Signature must match base class Use “My. Base” to refer to base class Use “Me” to refer to current class

Shadowing l l l Re-use of member name from base class Calls through base class ignore shadowed implementation Shadow by Name Ø Ø l Use “Shadows” keyword Hides all base class members with name Shadow by Signature Ø Ø Use “Overloads” keyword Signature must be different

Overriding Class Base Sub Foo() Msg. Box(“Base Foo”) End Sub End Class Dim derived 1 As New Derived Dim base 1 as Base base 1=derived 1. Foo() base 1. Foo() Class Derived Inherits Base Overrides Sub Foo() Msg. Box(“Derived Foo”) End Sub End Class

Shadowing Class Base Sub Foo() Msg. Box(“Base Foo”) End Sub End Class Dim derived 1 As New Derived Dim base 1 as Base base 1=derived 1. Foo() base 1. Foo() Class Derived Inherits Base Shadows Sub Foo() Msg. Box(“Derived Foo”) End Sub End Class

Type Conversions l Visual Basic supports Strong typing Ø Option Strict § § l Prevents narrowing conversions Prevents late-bound calls Project or per file setting Turn it on! How to convert types Ø Type specific functions § Ø CByte, CShort, CInt, CLng, CSng, CDbl, CDec, CStr, CDate CType( var, typename) Operator

l l l Double Single Decimal Long Integer Short Byte Widening Conversions Numerics widen as shown above Enumeration to underlying type From a type to one of its base classes From a type to an interface it implements Char to String From array of x to array of y if x and y are reference types and x widens to y

Narrowing Conversions l l l l Numeric conversions that may overflow Boolean to numeric and numeric to Boolean From a numeric type to an enumerated type A conversion to a class type from an interface or a non-deriving class A conversion to an interface from a nonderiving class From string to numeric and numeric to string From String to Char From Char() to String

Overloading l l Enables multiple versions of a member with different signature Overloading based on parameter types Return type not considered Overloading rules Ø Ø Rule out narrowing conversions Match based on widening rules

Class Instance Constructors l l l Enables initialization of class at creation Declared using “New” keyword Can be overloaded Compiler generates an implicit new Compiler spits field initializers into New Use Access modifiers to control creation Ø Use shared functions to provide a class factory

Delegates l l Delegate is an object oriented function pointer Class with a signature Tracks method and class instance Useful for callbacks Delegate Sub Output(s As String) Sub Win. Output(msg As String) Msg. Box(msg) End Sub Dim d As Output = Address. Of Win. Output d. Invoke(“Hello World”)

Events l l Based on top of delegates Hookup of events is explicit Sub Button 1_Click(o As Object, _ e As Event. Args) Handles Button 1. Click l Dynamic Hookup of events Add. Handler Text. Box 1. Enter, Address. Of Enter. Text. Box ‘Event now hooked up Remove. Handler Text. Box 1. Enter, Address. Of Enter. Text. Box ‘Event unhooked

Events Demo

Threading l l Why Multi-threading? System. Threading Namespace Ø l l Classes for threads and synchronization System. Threading. Thread Class Thread executes a Sub Ø Sub specified using delegate Dim t As New Threading. Thread(Address. Of Thread. Func) t. Start()

Threading Demo

Threading Gotcha’s l l Problems with deadlocks and race conditions Solution: Synchronization mechanisms Ø Ø l VB Synclock statement Mutex, Interlocked, Monitor, Reader. Writer. Lock, Reset Events Windows® forms not thread safe Ø Ø Only thread that owns control’s window handle should call methods Background threads must marshal calls to control

COM Interop – Importing l l l Use Tlb. Imp / Project system to create wrapper Not a 1: 1 mapping from COM to. NET Primary Interop Assemblies Array mapping Co. Class/Interface → Class mapping Ø Ø Declare variable as Interface type Create instances of Class Type

Com Interop – Exporting l Runtime needs explicit declarations Ø Ø l Default Interface for the class Events Interface Visual Basic. Net makes it easy: Ø Ø COM Class template item Com. Class Attribute § Ø Interface declarations done by compiler Project system § § Strong Name Register COM object

Summary l l Modern Basic Language More power More flexibility Great Language for building. NET Applications

- Slides: 29