48
Visual Basic.NET Preview David Stevenson Consulting Software Engineer, ABB [email protected]

Visual Basic.NET Preview

  • Upload
    orrin

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

Visual Basic.NET Preview. David Stevenson Consulting Software Engineer, ABB [email protected]. How to Get Visual Basic.NET. Requires Windows 2000, IE 5.5 (helpful to install IIS 5 on Win2K Pro) NET Overview: msdn.microsoft.com/net - PowerPoint PPT Presentation

Citation preview

Page 1: Visual Basic.NET Preview

Visual Basic.NET Preview

David StevensonConsulting Software Engineer, ABB

[email protected]

Page 2: Visual Basic.NET Preview

How to Get Visual Basic.NET

Requires Windows 2000, IE 5.5 (helpful to install IIS 5 on Win2K Pro)

NET Overview: msdn.microsoft.com/net Download NET:

http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/000/976/msdncompositedoc.xml

Page 3: Visual Basic.NET Preview

Visual Basic.NET Goals

Rapid Application Development of Enterprise Web Applications

Language Interoperability Improved Object Oriented Features

Page 4: Visual Basic.NET Preview

Modern Language Features

Free Threading Structured Exception Handling Type Safety Shared Members Initializers

Page 5: Visual Basic.NET Preview

Object Oriented Features

Inheritance Encapsulation Overloading Polymorphism Parameterized Constructors

Page 6: Visual Basic.NET Preview

Disclaimer

Syntax subject to change in released product.

Most of the following is a summary of Microsoft’s article: Visual Studio Enables the Programmable Web. Examples may be changed slightly.

Page 7: Visual Basic.NET Preview

OOP: Inheritance

Ability to reuse code via Inherits keyword. Derived class inherits all methods and

properties of the base class. Derived class can override methods defined

in the base class using the Overrides keyword.

The derived class can extend the base class by adding new methods and properties.

Page 8: Visual Basic.NET Preview

Inheritance Example

Public Class MyBaseClass

Function GetCustomer ()

Console.WriteLine ( "MyBaseClass.GetCustomer" )

End Function

End Class

Public Class MyDerivedClass : Inherits MyBaseClass

Function GetOrders ()

...

End Function

End Class

Public Module modmain

Sub Main()

Dim d As MyDerivedClass

d.GetOrders ()

End Sub

End Module

Page 9: Visual Basic.NET Preview

Overrides Example

Class MyVeryDerivedClassInherits MyDerivedClass

Overrides Function GetOrders ( )

Page 10: Visual Basic.NET Preview

OOP: Encapsulation

New Protected keyword. Hides properties/methods except for derived

classes.

Protected cName as string

Protected Function ChangeName ( NewName )Me.cName = NewName

End Function

Page 11: Visual Basic.NET Preview

OOP: Overloading

VB6: Overloading via Implicit Auto-Conversion was potentially dangerous.

VB.NET: Creating two or more functions with the same name, but with different function signatures (parameters).

Overloaded functions can process data differently.

Page 12: Visual Basic.NET Preview

Overloading Example

Overloads Sub Display ( theChar As Char )

Overloads Sub Display ( theInteger As Integer )

Overloads Sub Display ( theString As String )

Page 13: Visual Basic.NET Preview

OOP: Polymorphism

Ability to process an object differently depending on its data type or class.

Ability to redefine methods for derived classes.

Page 14: Visual Basic.NET Preview

Polymorphism Example

Class Employee

Overridable Function PayEmployee () As Decimal

PayEmployee = Hours * HourlyRate

End Function

End Class

Class CommissionedEmployee

Inherits Employee

Overrides Function PayEmployee ( ) As Decimal

PayEmployee = BasePay + Commissions

End Function

End Class

Page 15: Visual Basic.NET Preview

OOP: Parameterized Constructors VB6: Unparameterized Class_Initialize VB.NET: Allows the creation of a new

instance of a class while passing arguments for initializing the data of the new class.

VB.NET: Simultaneous creation and initialization of an object instance.

Page 16: Visual Basic.NET Preview

OOP: Parameterized ConstructorsPublic Class Test

Private i as Integer

Overloads Public Sub New()

MyBase.New

i = 321

End Sub

Overloads Public Sub New ( ByVal par as Integer )

MyClass.New()

i = Par

End Sub

End Class

Page 17: Visual Basic.NET Preview

Interfaces

Abstract classes without implementation. Can inherit from other interfaces. Concrete classes can singly inherit, but have

multiple interface implementations.

Page 18: Visual Basic.NET Preview

Interface Example Part 1

Public Interface IDog

Function Barks ( ByVal strBark As String )

End Interface

Public Interface IDog2 : Inherits IDog

Function Bites ( ByVal intNumBites As Integer )

End Interface

Page 19: Visual Basic.NET Preview

Interface Example Part 2

Public Class Dogs

Implements IDog

Implements IDog2

Public Function Bites ( ByVal intNumBites As Integer ) _

Implements IDog2.Bites

Console.WriteLine ( "is worse than {0} bites.", intNumBites )

End Function

Public Function Barks ( ByVal strBark As String ) _

Implements IDog.Barks

Console.Write ( "A Dog's bark {0} ", strBark )

End Function

End Class

Page 20: Visual Basic.NET Preview

Free Threading

Concurrent processing improves scalability. Can start a thread and run asynchronously.

Page 21: Visual Basic.NET Preview

Free Threading Example

Sub CreateMyThread ( )

Dim b As BackGroundWork

Dim t As Thread

Dim b = New BackGroundWork ()

Set t = New Thread ( New ThreadStart ( AddressOf b.DoIt )

End Sub

Class BackGroundWork

Sub DoIt ( )

End Sub

End Class

Page 22: Visual Basic.NET Preview

Structured Exception Handling

VB6: On Error Goto• Problem: Goto Error Handling routine, Goto

Central Error Processing Routine, Exit out of procedure.

VB.NET: Try… Catch… Finally

Page 23: Visual Basic.NET Preview

Structured Exception Handling ExampleSub SEH ( )

TryOpen “TESTFILE” For Output As #1

Write #1, CustomerInformation

CatchKill “TESTFILE”

FinallyClose #1

End try

Page 24: Visual Basic.NET Preview

Try, Catch, Finally Syntax

Try

tryStatements

Catch exception1 [ As type ] [ When expression ]

catchStatements

[ Exit Try ]

Catch exception2 [ As type ] [ When expression ]

catchStatements

[ Exit Try ]

Finally

finallyStatements

End Try

Page 25: Visual Basic.NET Preview

Type Safety

VB6: Implicit Auto-Conversion on subroutine/function calls. Fails during run-time if data loss occurs.

VB.NET: Option Strict generates compile-time errors if a conversion is required.Option Strict Off.

Page 26: Visual Basic.NET Preview

Data Type Changes

Decimal data type replaces Currency data type.

Long is 64 bits. Integer is 32 bits. Short (new) is 16 bits.

Page 27: Visual Basic.NET Preview

Shared Members

Data and method members of classes that are shared by all instances of a class.

A shared data member is one that all instances of a class share.

A shared method is a method that is not implicitly passed an instance of the class. (consequently, access to class data members is not allowed).

Page 28: Visual Basic.NET Preview

Initializers

Initialization of variables on the lines they are declared.

Dim X As Integer = 1 Equivalent to:

Dim X As Integer

X = 1 VB6 Recommendation:

Dim X As Integer : X = 1

Page 29: Visual Basic.NET Preview

Namespaces

For organizing code hierarchically. Example:Namespace UserGroups.NewYork.Rochester.VDUNY

... Insert your code here

End Namespace

Page 30: Visual Basic.NET Preview

Namespaces

Always public. Components within the namespace may have

Public, Friend or Private access. Default access type is Friend. Private members of a namespace are accessible

only within the namespace declaration they were declared in.

Page 31: Visual Basic.NET Preview

Imports Statement

Imports namespace names from referenced projects and assemblies.

Syntax: Imports [aliasname = ] namespace

Imports System

Imports Microsoft.VisualBasic

Page 32: Visual Basic.NET Preview

Common Language Run-Time

System.* “This changes everything.” Dodge Intrepid

Commerical

Page 33: Visual Basic.NET Preview

Hello World!

' Allow easy reference System namespace classes

Imports System

' Module houses the application’s entry point

Public Module modmain

' "Main" is application's entry point

Sub Main()

' Write text to the console

Console.WriteLine ("Hello World using Visual Basic!")

End Sub

End Module

Page 34: Visual Basic.NET Preview

Migrating from VB6 or VBScript to VB.NET Following information from A Preview of

Active Server Pages+, Appendix B, Moving from VBScript or VB6 to VB7

Set and Let keywords no longer supported in VB.NET.

Set objVar = objRef ‘ VB6 objVar = objRef ‘ VB.NET Class property syntax changes in VB.NET.

Page 35: Visual Basic.NET Preview

VB 6 Class Property Syntax

Private mstrString As String

Public Property Let MyString ( ByVal NewVal As String )

mstrString = NewVal

End Property

Public Property Get MyString () As String

MyString = mstrString

End Property

Page 36: Visual Basic.NET Preview

VB.NET Class Property Syntax

Private mstrString As String

Public Property MyString As String

Get

MyString = mstrString

End Get

Set

mstrString = Value

End Set

End Property

Page 37: Visual Basic.NET Preview

ReadOnly and WriteOnly Properties in VB.NETPrivate mstrReadOnlyString As String

ReadOnly Public Property MyReadOnlyString As String

Get

MyReadOnlyString = mstrReadOnlyString

End Get

End Property

Page 38: Visual Basic.NET Preview

Method, Function and Subroutine Calls Require Parenthesis VB.NET requires parenthesis around

method, function and subroutine call parameters.

MyFunction ( “Param1”, 1234 )

objRef.MyMethod ( “Param1”, “Param2” )

Response.Write ( “<B>Some Text</B>” ) VB6: Use: Call MyFunction ( “par1” )

Page 39: Visual Basic.NET Preview

Parameters Default to ByVal

VB.NET defaults to ByVal for all parameters that are intrinsic data types.

Previously(VB6), the default was ByRef. VB6 Recommendation: Explicitly declare

all parameters to ByVal or ByRef. References to objects, interfaces, array and

string variables still default to ByRef.

Page 40: Visual Basic.NET Preview

Declarations in VB.NET

Variables declared in the same statements must be the same type. No longer allowed:Dim a As Integer, b As String

Variables can be initialized in the same statement they are declared:Dim a As Integer = 123Dim intArray ( 3 ) = ( 12, 34, 56 )

Page 41: Visual Basic.NET Preview

Default Values andOptional Parameters Default values can be supplied in function

parameters.Sub MySubr ( ByVal intParam1 As Integer = 123 )

Optional parameters must always supply a default value.Function MyFunction ( Optional ByVal strParam As String = “MyString” )

IsMissing keyword is no longer supported.

Page 42: Visual Basic.NET Preview

Explicit Casting Now Required

VB6 Sometimes did implicit auto-conversion of data types.

VB.NET now requires explicit casting.Response.Write ( CStr ( intLoop ) ) Trace.Write ( CStr ( intLoop ) )

Page 43: Visual Basic.NET Preview

Shorthand assignments

VB6: intVar = intVar + 1 VB.NET:

intVar += 1 intVar -= 1 intVar *= 2intVar /= 2

Page 44: Visual Basic.NET Preview

Short Circuited Conditional Statements Conditional expressions with And or Or: if

first test fails, following expressions won’t be executed:if ( a = b ) And ( c = d ) Then …If a is not equal to b, then the test for c = d will not be executed.

Page 45: Visual Basic.NET Preview

References

Visual Studio Enables the Programmable Web, http://msdn.microsoft.com/vstudio/nextgen/technology/language.asp

Joshua Trupin, The Future of Visual Basic: Web Forms, Web Services, and Language Enhancements Slated for Next Generation,http://msdn.microsoft.com/msdnmag/issues/0400/vbnexgen/vbnexgen.asp

Page 46: Visual Basic.NET Preview

References

Introducing Win Forms, http://msdn.microsoft.com/vstudio/nextgen/technology/winforms.asp

Visual Studio Enables the Programmable WebWeb Forms,http://msdn.microsoft.com/vstudio/nextgen/technology/webforms.asp

Page 47: Visual Basic.NET Preview

References

Richard Anderson, Alex Homer, Rob Howard, Dave Sussman, A Preview of Active Server Pages +, Appendix B, Moving from VBScript or VB6 to VB7.

Page 48: Visual Basic.NET Preview

Resources

Microsoft .NET & ASP+ Resources and Information, http://www.devx.com/dotnet/resources/

News Server: news.devx.comNews Group: vb.vb7

News Server: msnews.microsoft.comNews Group: microsoft.public.net.*