Title: VB.NET
1VB.NET Technical Overview
2Welcome
- Brian Sokolowski
- MCSD, MCT
- Senior Instructor - DBBasics
- Technical Leader - OnSphere
- sok_at_techpilot.com
3VB.NET
- Todays Agenda
- What happened to VB?
- .NET Framework
- Visual Studio.NET
- VB.NET Changes Enhancements
- Object-Oriented Programming
- Windows Forms
- ADO.NET
- Advanced Features
- Deploying .NET Applications
4What happened to VB?
- Visual Studio 6
- Visual Basic vs. Visual C
- Visual Studio.NET
- VB vs. C jokes are over!
- Common Language Runtime
- Common Type System
- Are all languages equal?
- C vs. VB.NET
- Which language to choose?
- Lessons learned
5.NET Framework
- .NET-compatible languages
- VB.NET, C, C, Jscript.NET, J
- COBOL, Fortran, Pascal, Perl, SmallTalk
- Common Language Runtime (CLR)
- Replaces VB Runtime engine
- Code Execution Management
- Garbage Collection
- Type Safety Checking
- Exception Management
- Thread Support
- IL to Native Code Compiler (JIT)
6.NET Framework cont
Public Sub Foo() VB.NET Dim x As String
abc Dim y As Integer 25 MessageBox.Show(x.To
Upper()) MessageBox.Show(Math.Sqrt(y).ToString())
End Sub public void Foo() // C string x
abc int y 25 MessageBox.Show(x.ToUpper())
MessageBox.Show(Math.Sqrt(y).ToString())
- .NET Base Classes
- Organized by Namespaces
- System.Collections
- System.Data
- System.Diagnostics
- System.IO
- System.Net
- System.Security
- System.Threading
- System.Windows.Forms
- VB language enhancements
- Compatibility with .NET Framework
7Visual Studio.NET
- Shared IDE!
- Project templates
- Windows Applications
- ASP.NET Web Applications
- Web Services
- Console Applications
- Windows Services
- Class Libraries
- Project Files
- .SLN, .VBPROJ, .VB
- Assemblies
- Similar to Standard EXE or COM DLL
8Visual Studio.NET cont
- Referencing Libraries
- Namespaces
- Imports statement
- New IDE Features
- Solution Explorer
- Server Explorer (RAD for the server)
- Task List enhancements
- XML/HTML Editor
- Enhanced Debugging Features
- Breakpoints, Output Window, Call Stack
- Command Window
Dim myConn As System.Data.SQLClient.SqlConnection
Dim myDS As System.Data.DataSet Imports
System.Data Dim myConn As SQLClient.SqlConnection
Dim myDS As DataSet Namespace ABCCompany
Implement classes here End Namespace
9VB.NET Changes
- Common Type System (CTS)
- Everything is an object!
- Data Type conversions use CType
- Data Types Changes
- Short is the VB6 Integer (2 bytes)
- Integer is the VB6 Long (4 bytes)
- Long is a new size (8 bytes)
- Char is for 2-byte Unicode
- Decimal replaces Currency
- Object replaces Variant
- Date no longer stored as Double
- String is now immutable
VB.NET Dim x As Integer Dim y As
System.Int32 // C int x System.Int32 y Dim a
As String Dim b As Integer a 25 b CType(a,
Integer)
Imports System.Text Sub Foo() String
buffer Dim s As New StringBuilder() s.Append(H
ello) s.Append(, ) s.Append(World) Messa
geBox.Show(s.ToString) End Sub
10VB.NET Changes cont
Sub Foo(x As Integer) If x gt 10 Then Dim y As
Integer x 4 y x - 1 End
If MessageBox.Show(y) Error! End
Sub Structure Employee End Structure
- Multiple variable declarations
- Dim x, y, z As Integer
- Initializers
- Dim x As Integer 5
- Block-level scope!
- Arrays are all zero-based
- Option Base no longer supported
- Structure replaces UDT
- TypeEnd Type no longer supported
- Assignment operators
- , -, , /, \, ,
11VB.NET Changes cont
- New Compiler options
- Option Strict On
- Calls to subs functions require ( )
- Display(Hello, World)
- Arguments passed ByVal by default
- Optional parameters require default value
- IsMissing statement is no longer supported
- New Return keyword!
- Old technique is still supported
- Changes to default properties
- Set assignment is no longer supported
Function Foo(x As Double) As Double x
100 Return x Returns immediately! End
Function Dim X As Customer Set X New
Customer Invalid X New Customer() Valid
12Exception Handling
- TryCatchFinallyEnd Try
- Try block provides area of protection
- Catch block is used to handle exception(s)
- Finally block is always executed
- System.Exception class
- Similar in functionality to Err object
- Throwing Exceptions
- Throw New Exception(Invalid ID)
- On Error Goto still supported!
- Can use combination of both
Sub Foo(x As Double, y As Double) Dim z As
Double Try z x / y MessageBox.Show(z.ToStri
ng()) Catch e As Exception MessageBox.Show(e.Me
ssage) Finally Beep Always executed End
Try End Sub
13OOP in VB.NET
- Class statements
- ClassEnd Class
- Access modifiers
- Public accessible everywhere
- Private accessible within type
- Friend accessible within type and assembly
- Protected accessible within type any derived
classes - Protected Friend accessible within assembly
any derived classes - Method declaration is unchanged
14OOP in VB.NET cont
Class Customer Private miID As
Integer Property ReadOnly ID() As
Integer Get Return miID End Get End
Property Public Sub New(ID As Integer) miID
As Integer End Sub End Customer Dim X As New
Customer(20) MessageBox.Show(X.ID)
Public Sub Add(x As Integer, y As
Integer) MessageBox.Show(x y) End Sub Public
Sub Add(a As String, y As String) MessageBox.Show
(a b) End Sub Method is chosen based on
signature Add(25, 10) Add(Hello, World)
- Property declaration changes
- GetEnd Get, SetEnd Set
- Using attributes
- Title, Description, Version, Web Service
- Constructors
- Initialize with Sub New
- Desctructors
- Sub Finalize
- Garbage Collection
- Overloading
- Multiple Methods with the Same Name!
Private sName As String Property Name() As
String Get Return sName End Get Set (ByVal
Value As String) sName Value End Set End
Property
15OOP in VB.NET cont
Public Class BaseClass Public Overriddable Sub
Foo() MessageBox.Show(parent) End Sub End
Class Public Class DerivedClass Inherits
BaseClass Public Overrides Sub
Foo() MessageBox.Show(child) MyBase.Foo() E
nd Sub End Class Dim x As New
DerivedClass() x.Foo() child, then parent
Public Class BaseClass Public Sub Foo() End
Sub End Class Public Class DerivedClass Inherits
BaseClass Inherits behavior of baseclass End
Class Dim x As New DerivedClass() x.Foo()
Public NotInheritable Class SealedClass End
Class Public Class DerivedClass Inherits
SealedClass Compile error! End Class Public
MustInherit Class BaseClass End Class Dim x As
New BaseClass() Invalid
Public Interface IFace Sub Smile() End
Interface Public Class Person Implements
IFace Public Sub Grin() Implements
IFace.Smile MessageBox.Show(Hi) End Sub End
Class Dim x As New Person() Dim poly As IFace
x x.Grin() Calls Person.Grin poly.Smile()
Calls Person.Grin
- Inheritance
- Inherit behaviors from another class
- Inherits
- NotInheritable, MustInherit
- Method overriding
- Override inherited behaviors
- Overridable, Overrides
- Shadows
- MyBase keyword
- Implementing interfaces
- Guarantee or contract
16OOP in VB.NET cont
Delegate Sub MathOp(x As Long, y As Long)
Procedures to be passed Sub Add(a As Long, b As
Long) MessageBox.Show(a b) End Sub Sub Diff(a
As Long, b As Long) MessageBox.Show(a - b) End
Sub Pass in procedures to be invoked Calc(9,
4, AddressOf Add) 13 Calc(9, 4, AddressOf
Diff) 5 Sub Calc(m As Long, n As Long, math As
MathOp) math.Invoke(m, n) End Sub
Private Sub Clicker() Handles Button1.Click,
Button2.Click Implement generic click
handler End Sub or Private Sub Clicker()
Implement generic click handler End Sub Attach
dynamically AddHandler Button1.Click, AddressOf
Clicker AddHandler Button2.Click, AddressOf
Clicker
Public Class MathFunctions Shared Function Add(x
As Long, _ y As Long) As Long Return x
y End Function Shared Function Diff(x As Long,
_ y As Long) As Long Return x - y End
Function End Class Dim x As Long
MathFunction.Add(9, 4) 13 Dim y As Long
MathFunction.Diff(9, 4) 5
- Shared members
- Dont have to create instance
- Create code library
- Implementing Delegates
- Similar to function pointers or callbacks
- Event Handling Changes
- Handles
- AddHandler
- Comparing Classes Structures
- Only classes support inheritance, constructors
- Class is reference-type, Stucture is value-type
17Windows Forms
Imports System.Windows.Forms Public Class
frmMain Inherits System.Windows.Forms.Form
Constructor Public Sub New() Me.TopMost
True End Sub Destructor Public Sub
Dispose() End Sub End Class
- System.Windows.Forms namespace
- Advantages over VB Forms
- Richer controls
- Flat Window style of Windows XP
- Advanced graphics support (GDI)
- Accessibility support
- Visual inheritance
- Extensibility
- Form Changes Enhancements
- Form_Load is now Sub New constructor
- Form_Unload is now Form_Closing
- Many new properties!
18Windows Forms cont
- New Controls
- CheckedListBox
- LinkLabel (allows hyperlinks on forms!)
- Splitter
- NotifyIcon
- Existing Control Changes
- Anchor property
- Location property
- Text property
- SelectAll() method
- New Menu Designer
19ADO.NET
Imports System.Data SQL Server Dim cnSQL As
New SqlClient.SqlConnection() cnSQL.ConnectionStri
ng CS_MY_SERVER cnSQL.Open() OLE DB Dim
access As New OleDbClient.OleDbConnection() access
.ConnectionString CS_MY_SERVER access.Open()
Imports System.Data Dim cmdSQL As New
SqlClient.SqlCommand(cnSQL) cmdSQL.CommandText
_ Select Count() From Authors Execute
aggregate MessageBox.Show(cmdSQL.ExecuteScalar().T
oString)
Imports System.Data Dim cmdSQL As New
SqlClient.SqlCommand(cnSQL) cmdSQL.CommandText
Select From Authors Dim datAuthors As
SqlClient.SqlDataReader datAuthors
cmdSQL.ExecuteReader() Do Until datAuthors.Read
False MessageBox.Show(datAuthors.GetString(1))
Loop
- System.Data namespace
- ADO.NET resides in the .NET Framework
- .NET Managed Providers
- .NET data provider for SQL Server
- .NET data provider for OLE DB
- Connecting to Data Sources
- SqlConnection,OleDbConnection
- Executing Commands
- SqlCommand,OleDbCommand
- New execute methods
20ADO.NET cont
Imports System.Data Dim row As DataRow Add row
dsPubs.Tables(Titles).NewRow row(title)
Blueteeth Primer dsPubs.Tables(Titles).Rows.Ad
d(row) Update row.BeginEdit() row(title)
Bluetooth Primer row.EndEdit()
Delete row.Delete
Imports System.Data Dim daSQL As New
SqlClient.SqlDataAdapter() daSQL.SelectCommand
Select From Authors Open disconnected
dataset Dim dsPubs As New DataSet() daSQL.Fill(dsP
ubs, Authors) Upload data from disconnected
dataset daSQL.Update(dsPubs, Authors)
Imports System.Data New table Dim dsCustom As
New DataSet() Dim tblNew As New
DataTable(Authors) Create new
fields tblNew.Columns.Add(ID,
System.Int32) tblNew.Columns.Add(Name,
System.String) dsCustom.Tables.Add(tblNew)
- Using DataReaders
- Read-only, forward-only recordset
- SqlDataReader,OleDbDataReader
- Using DataSets
- Disconnected recordset
- DataSet,DataTable,DataRow
- SqlDataAdapter,OleDbDataAdapter
- Can populate from database
- Can create programmatically
- Can defining Relationships
- Can make offline changes
21Advanced Features
Imports System.XML Imports System.Data Load
xml Dim doc As New XMLDataDocument() doc.DataSet.R
eadXMLSchema(myschema.xsd) doc.Load(mydata.xml
) Get first row node Dim node As
XMLElement node CType(doc.DocumentElement.FirstC
hild, _ XMLElement) Dump node Dim row As
DataRow row doc.GetRowFromElement(node)
Imports System.Threading Dim t As Thread t
New Thread(AddressOf Foo) t.Start()
Asynchronous procedure Sub Foo() This thread
will sleep for 1 min. Thread.CurrentThread.Sleep(
60) End Sub
Imports System.Messaging Create queue Dim
NewQueue As MessageQueue NewQueue
MessageQueue.Create(".\MyQueue") Send message
to queue NewQueue.Send("Hello World") Read
queue Dim FirstMessage As Message
FirstMessage NewQueue.Peek MessageBox.Show(Firs
tMessage.Body.ToString)
- XML Support
- System.XML namespace
- XML DOM is in the .NET Framework
- Supports parsing, XSLT, XPath Schemas
- Threading
- System.Threading namespace
- Creating free-threaded applications
- Messaging
- System.Messaging namespace
- Working with MSMQ
22Deploying Applications
- .NET deployment advantages
- Applications isolation (fewer DLL conflicts)
- Private components!
- Side-by-side versioning
- On-the-fly updates
- Assemblies
- Global Assembly Cache
- Windows Installer (.msi)
- Zero administration
- Safe uninstalls
- Automatic rollback
23Deploying Applications cont
- Deployment Project types
- Cab project
- Merge Module project
- Setup project
- Web Setup project
- Setup Wizard
- Deployment Editors
- File System
- Registry
- User Interface
- Custom Actions
- Launch Conditions