510 likes | 623 Views
What’s Happened to Visual Basic?. Everything is an object now. Overview. Describe the strategies behind VB.NET and Visual Studio.NET Discuss changes to the developer interface (IDE) Identify significant VB.NET language changes Introduce C# Introduce ADO.NET
E N D
What’s Happened to Visual Basic? Everything is an object now.
Overview • Describe the strategies behind VB.NET and Visual Studio.NET • Discuss changes to the developer interface (IDE) • Identify significant VB.NET language changes • Introduce C# • Introduce ADO.NET • Using VB.NET as a teaching language
VB.NET Strategies • Reinventing Internet development • and desktop development along the way • Technology ‘marries’ Windows and the Internet • Visual Studio.NET • Windows.NET (code named Whistler) • The conflict • Windows is a cash cow so preserving the PC is important • .NET generates no revenue today but if successful will turn Microsoft into an Internet powerhouse • Remember the transition from DOS to Windows
VB.NET - Introduction • The new name for Visual Basic • In summary, it’s a new language and a new development environment • Form and Code windows have been overhauled • Module and program structure is new • Supports object-oriented features including overloading, inheritance, etc. • Supports an integrated Web development environment
What is in Visual Studio.NET? • Common IDE shared by all Visual Studio.NET applications • C++, C#, Visual FoxPro, VB.NET • J++ is not part of Visual Studio • Visual InterDev appears to be obsolete • Common Language Runtime (CLR) exposes objects instead of COM • COM supported through wrapper procedures for backward compatibility • Common Type System (CTS) standardizes data types between all Visual Studio.NET languages • ADO.NET replaces ADO • ASP.NET replaces ASP
Converting VB6 Programs • Conversion Wizard activated by opening a .vbp file • Uses compatibility libraries to migrate COM objects • So it is not writing the best managed code • Uses Microsoft.VisualBasic.Compatibility.VB6 namespace • Late Binding causes problems • Control instances convert well • Those that don’t convert are turned into labels • First iteration of tool • Did not ship with the PDC Preview edition • Will likely undergo further development
IDE Change Summary • New default development environment • Modules displayed in tabbed dialogs similar to previous Visual Studio editions • MDI mode is supported but SDI mode seems to be obsolete • SDI mode appeared in PDC preview but not in Beta 1 • Enhanced window management features • Docking and auto-hide • Enhanced debugging windows
Window Types • Document windows • Appear on individual tabs • Multiple tabbed lists supported • Each tab corresponds to a module file • Tool windows • Dockable along any edge • Auto-hide allows windows to be hidden along any window edge to maximize screen real estate • Some tool windows can appear as document windows • Solution Explorer, Help Windows • Windows can float on desktop
VB.NET - IDE Document windows Auto-hide Docked tool windows
Projects are now Solutions • Solution Explorer replaces the Project Explorer • Conceptually the same window though • Project groups (.vbg files) no longer exist • Solution file replaces project group • Projects contained by solution • Project contains references to namespaces • Solution and projects are organized into multiple directories
Solution Explorer References to namespaces Solutions contain multiple directories
The Toolbox and Controls • VB.NET controls are no longer COM controls • Controls based on .NET assemblies • Control names and interfaces have changed • Button control replaces Command Button • Shape and Line not supported • Functionality replaced by System.Drawing namespace • Six separate controls replace the CommonDialog control • GroupBox replaces frame • Property names have changed too • Text property replaces Caption in most cases • Default properties no longer supported • Control arrays are not supported by .NET
The Toolbox Auto-hide enabled Tabs group controls based on function Multiple clipboards supported
The WinForm Designer • Forms divided into WinForms and WebForms • WinForm designer works like the Form window • Process of creating, moving, resizing a control instance is the same as previous editions • Form window contains a tray to display invisible control instances • ImageList, OpenFileDialog, etc. • WinForm designer creates exposed code to: • Import namespaces • Instantiate the form • Create control instances
Debugging Windows • Locals and Watch windows operate similar to VB6 • Autos window shows variables in current and previous statement • This window examines current class instance (Me keyword) • Modules window lists modules (DLLs) used by a program • CallStack window shows procedure call sequence • Output window shows compilation results • VB.NET performs real compilation – All syntax errors discovered at compile time • Breakpoints window lists current breakpoints • Breakpoints now persistent between invocations
Command Window • Command window replaces Immediate window • Command mode used to execute Visual Studio commands bypassing menu system • Useful to create command macros • Display the Open File dialog File.OpenFile • Immediate window used for debugging • Similar to VB6 • Note Debug class has new properties and methods
Menus • Menu implementation has changed • Use MainMenu control in Toolbox to create menu • Menu creation is now ‘in place’ • Create menus directly on the WinForm designer • MainMenu class defines menu • MenuItem class defines subordinate menu items • MenuItems can contain subordinate menu items to define sub menus • Menu integration implemented differently in MDI applications
Anatomy ofa WinForm In-place menu editor New checked list box and NumericUpDown controls Controls support images and text alignment Invisible controls appear in tray
The .NET Framework • Classes are exposed through assemblies • Compare an assembly to an .ocx file • Assemblies contain one or more namespaces • Namespaces are similar to the classes exposed by an .ocx file • Access to COM provided through wrapper procedures supplied by compatibility namespaces • Microsoft.VisualBasic.Compatibility.VB6
Common Language Runtime • All Visual Studio.NET languages share the same run-time • CLR performs object creation and destruction • Garbage collector destroys objects • Runs periodically as a background thread • Object destruction no longer occurs when reference counter becomes zero • Conceptually implemented similar to the Java virtual machine • We could be running .NET under Sun Solaris • All code compiled to standard Intermediate Language (IL) in .NET
Common Type System • All .NET languages use the Common Type System • Required for language interoperability • Changes to VB data types • New Char type contains single character • Date data type is no longer a double • Long Integer is 64 bits • Integer is 32 bits • New Short data type is 16 bits • Variant not supported • Generic late bound Object type still exists • All data types are represented as classes
Code Modules • Code Editor is the new term for the Code window • New features • Statements automatically indented • Procedures can be expanded or collapsed • #Region / #End Region “preprocessor” directives allow statement and procedure grouping • Expand or collapse large code blocks • Techniques to select event procedures has changed • Syntax errors appear in Task window • VB.NET performs full syntax check when compiled • No need to correct 1 error at a time
CodeEditor Region / procedures can be expanded or collapsed Task window displays errors
Language Changes – Overview • New type conversion rules • Arrays implemented differently • They are now classes supporting methods • Structured error handling • New OOP features • Static classes replace many intrinsic functions • Inheritance – Implementation and Interface • Parameterized constructors • Overloading • Methods, properties, constructors
Strict Type Checking • New Option Strict statement prohibits: • Implicit numeric to string type conversion • Implicit type conversion to more restrictive types • Long to Integer • Double to Single • Late binding of object variables • Great for structured programming purists • Even better for teaching data types and type conversion
Namespaces – Getting to Objects • Access to all classes is through a namespace containing one or more classes • Namespaces are hierarchical. Namespaces may contain other namespaces • Imports statement provides a module with a namespace reference Imports SystemImports System.ComponentModelImports System.DrawingImports System.WinForms
Type Conversion – The Old and the New • Functions CInt, CLng, CSng, CDbl etc. still supported but: • Variables are really classes too • Remember everything is an object • We say that the Integer and Long data types are represented as class structures provided by the System namespace • Thus, variables support type conversion methods such as Format and ToString
Type Conversion - Example Dim pintX As Integer = 33344 Dim plngX As Long plngX = CLng(pintX) plngX = pintX.ToInt64 Note automatic initialization Convert Integer to Long with intrinsic Function Convert Integer to Long using static method
Array Upgrades • All arrays are 0 based • Option Base statement no longer supported • Fixed size arrays are obsolete – All arrays are dynamic • Arrays are classes too so we call methods to: • Perform a binary search • Copy arrays or parts of an array • Sort arrays
Sorting Made Easy Dim A1() As Integer = {134,224,18,955,3,994} System.Array.Sort(A1) Declare a dynamic array and initialize it using c-like syntax Sort the array by calling the Sort method pertaining to the Array class
Static Classes Replace Intrinsic Functions • Many intrinsic functions are replaced by static classes • Math class contains mathematical functions • String class contains string functions • And many more • Get absolute value using Math class pintFoo = System.Math.Abs(-442)
Structures • Structures replace user-defined types • Structures may contain methods • Similar to classes • Declaration syntax has changed Public Structure Customer Public Name As String Public Address As String End Structure
Assigning Object References – The Set Statement is Gone • VB6 required the Set statement to assign object references because of default properties • Default properties no longer supported • Set statement obsolete Dim t As New Thing Dim tRef As Thing tRef = t
Structured Error Handling • Try… Catch… Finally… statements provide a structured error handling mechanism • Try block executes code that may cause a run-time error • Catch block provides error trap • Based on exceptions • Multiple Catch blocks trap different exceptions • Finally block always executes • Use for housekeeping • Replaces On Error Resume syntax • On Error still supported for backward compatibility
Error Handler – Example Dim srdCurrent As System.IO.StreamReader Dim pstrLine As String, pstrFile As String ofdCommon.showdialog() ‘ Display OpenFileDialog Try srdCurrent = New System.IO.StreamReader(ofdCommon.Filename) mstrLine = srdcurrent.ReadLine() Do Until mstrline = Nothing mstrLine = srdCurrent.Readline() Loop srdCurrent.Close() pstrMessage = “File Opened.” Catch excCurrent As Exception pstrMessage = “Cannot open file.” Finally MsgBox(pstrMessage,,”Module Status”) End Try
Drawing – Graphical Methods • Form methods obsolete – Replaced by System.Drawing namespace • Methods closely correspond to the GDI • Provides functionality previously available only through API calls • Use pens to draw lines / arcs, and outline rectangles • Use brushes to fill rectangles • Draw graphical shapes with Graphics class
Drawing a Rectangle • First define a color • Apply the color to a brush • Fill rectangle using brush and color Dim colCurrent As Color = Color.FromARGB(255,255,255,255)Dim sbCurrent As New SolidBrush(colCurrent) Dim recCurrent As New Rectangle(1,1,72,72) e.Graphics.FillRectangle(sbCurrent,recCurrent)
Drawing a Line • Define a color and apply it to a pen • Draw a line using a pen Dim penLine As New Pen = Pen(Color.FromARGB(1,1,1)) e.Graphics.DrawLine(penLine,1,1,100,100)
VB.NET is an OOP Language • VB.NET is formally an OOP language • Parameterized constructors • Supply arguments when creating an object from underlying class • Inheritance (interface and implementation) • Overloading • Multiple methods can have the same name and varying arguments • Procedures, properties, methods, and constructors can be overloaded
Parameterized Constructors • Arguments allowed when constructing objects from a class • Parameters may be overloaded • Create a StopLight using 3 different parameterized constructors Dim LightNorth, LightSouth, LightEast As StopLight LightNorth = New StopLight() LightSouth = New StopLight(StopLight.eLight.Green) LightEast = New StopLight(StopLight.eLight.Green,2500,500,3000)
Overloading • Constructors, methods, and properties may be overloaded • Methods of the same name support a variable number of arguments • Arguments may have different types • CLR detects which method to call based number and data type of arguments
Overloaded Constructors • New procedure is called by CLR when a class instance is initialized • Replaces Class_Initialize event • May have arguments • Procedure may be overloaded • Overloads keyword marks an overloaded procedure
Overloaded Constructors – Example Public Overloads Sub New() ‘ statements End Sub Public Overloads Sub New(byVal pLight as eLight) ‘ statements End Sub
Overloaded Help Sort method is overloaded
Inheritance • VB6 supported interface inheritance through abstract classes • The implementation was poor • Implementation inheritance was not supported • We could not derive a class from another class • VB.NET supports both implementation and interface inheritance • Inherits keyword used to create a derived class from a base class • Overrides keyword is used to override a method defined in the inherited class
Inheritance – Uses • Every form inherits from the System.WinForms namespaceInherits System.WinForms.Form • Forms are classes • Create a form designed as a template • Have developers inherit the template into their programs • Develop realistic object models from VB.NET classes
ADO+ • Next generation of ADO • XML used as transmission protocol • Disconnected recordset superceded by new DataSet class • In memory copy of data • May contain multiple tables and queries • All of the controls and development tools have changed considerably • Much more complex
C# (pronounced c-sharp) • New development language • Shares syntactical elements of Visual C++ and Visual Basic • The best of both worlds? • All languages share the same run-time so that all share the same namespaces • Microsoft hopes to replace Java?
Release Schedule • 2 to 3 betas • Release candidate 4th quarter 2001