|
Variable Declaration
The new syntax for declaring variables in VB.NET is quite interesting,
the style is not new but most VB6 developers will be surprised to see
such a change in VB.NET
You can actually declare and initialize a variable in a single line
1) Dim s as string = "Venkat"
2) Dim objClass as class = new Class
3) Dim objClass as new Class VB6 way also supported
Line 3 is similar as in VB6 with the difference that the object is
initialized as soon as it encounters the new keyword which was not possible
in VB6 (popularly known as "Lazy initialization") where the
class will be initialized when you access the class members.
No Variant Type
Dim s
In VB6 would result in variable "s" being declared as VARIANT
"s" is declared as Variant by the compiler, in VB.NET in
case of absence of any specific variable type "s" will be
declared a object type.
Also in VB.NET when you declare several variables in the same line
all variables will have the same type
Dim I,y,x as integer
I,y,x will be declared as integers in VB.NET where as in VB6 I,y would
be declared as variants.
No Set Keyword
VB.NET eliminated the need of using the "Set" keyword, in
fact VB.NET compiler will throw an error when you try to compile a code
with the SET keyword which will say "Let and Set are no longer
supported"
Instead VB.NET provides a simple way to creating object instances
Dim objClass as Class1
ObjClass = new Class1
Return Statement
VB.NET uses the "Return" statement to pass a return value
from a function, this feature takes VB closer to other object oriented
languages.
Function MyFunction() as string
Return "Hi From Manish"
End function
Traditional VB6 compiler would throw an error when this code is compiled
because the used to accept the functions name for passing values back
Function MyFunction() as string
MyFunction = "Hi From Manish"
End function
VB.NET supports both styles with the difference that when it encounters
Return it stops execution and returns control back to the caller.
Parameter Passing
Use of Parenthesis
While working with VB6 we have followed a rule that we needed to add
parenthesis when the called function return value had to be caught in
a variable.
VB.NET removed this inconsistency by enforcing that whenever you call
a function or a procedure, which accepts one or more parameters the
parameters must be enclosed in parenthesis, if they take no parameters
then the use or parenthesis is optional.
Convention
In VB.Net all parameters are passed ByVal as opposed to ByRef in VB6
Optional Parameters
VB.NET supports optional parameters but the IsMissing function is missing
making it difficult to differentiate omitted parameters. But, the feature
of Method Overloading can be utilized to achieve the required functionality.
Arrays
Arrays in VB.Net are 0 based, Option Base statement has been removed
Declaring an Array in VB.NET as
Dim myArr(3) as string
Will result in elements
Myarr(0)
Myarr(1)
Myarr(2)
And not from range 0 to 3 as in VB6
New Syntax
VB.NET provides a new syntax for initializing arrays
Dim o() as string = {"S1","S2"}
Properties
The motivation of using the properties has remained the same, the syntax
for defining them has changed
Private pvtFname as string
Public Property Fname as String
Get
Return pvtFname
End Get
Set
PvtFname = Value
End Set
End Property
A property can be Read-only or Write-only by adding the words "ReadOnly"
or "WriteOnly" in front of property declaration.
Winforms
VB.NET gets a new set of Rich UI components called Winforms.
Winforms have the ability to provide users with rich UI with the same
flexibility as traditional forms in VB6.
Winforms are packed with great functionality any developer would have
dreamt of , VB.Net WinForms have built-in support for resizing controls
as the user resize the form at runtime. It also supports anchoring controls
and docking forms to give developers a lot more control over the form
layout with minimal coding effort. In earlier versions of VB these features
required extensive coding or third party controls.
Winform Events
VB.NET has given away and added a few of the form events
Form_Load
Form_Activate
The above mentioned events are a thing of the past.
OO Boost
VB.NET gets a whole dose of OO
VB.NET is now a fully matured Object Oriented Language
Everything in VB.Net is an object
Concepts like Inheritance, Interfaces, Classes, NameSpaces, Overloading,
OverRiding, Polymorphism, Delegates have been introduced.
VB.NET also gets Free Threading, Exception Handling, Constructors and
all that a hard-core Object Oriented Developer asks for, yet preserving
the simplicity of VB.
Creating console-based application is also possible.
Imports System
NameSpace N1
Class C1
Shared Sub Main
Console.Writeline("From From Main")
End sub
Public sub SayHi
Console.Writeline("From From Sub SayHi")
End sub
End class
End NameSpace
This is a VB.NET class.
Does this code look similar to VB6, hmmm I would say a BIG NOOO
When compiled this code can produce a .EXE or a .DLL (library) file
Add some more OO ingredient to the same file
Imports System
NameSpace N1
Class C1
Shared Sub Main
Console.Writeline("From From Main")
End sub
Public sub SayHi
Console.Writeline("From From Sub SayHi")
End sub
End class
Class C2
Inherits C1
Shared Sub main
Dim o as new C2
o.SayHi
End sub
End Class
End NameSpace
Other Changes
Other Changes include
Default Option Explicit
Includes Option Strict for better Type Safety
Indexed Properties
Better ways to handling events
Delegates
Conclusion
As you might have realized VB.NET in many ways is different from its
predecessor, the good news is that VB has been transformed into a better,
more powerful language. The bad news is project written in VB6 will
require a bit of work to migrate it to .NET platform though Microsoft
plans to ship migration tools to assist in migrating from VB6 to VB.NET,
Also .NET framework is in a BETA stage and VB.NET might undergo further
revisions.
|