Beruflich Dokumente
Kultur Dokumente
NET
Compatibility
VB6 and VB.NET can run on the same computer.
Inheritance
You can derive a class from a parent class.
Structured Error Handling
There will be some kind of more structured error handling, though I don't know
the details yet.
Coexistance
You should be able to run VB6 and VB.NET on the same computer.
Upgrading
When you open a VB6 program in VB.NET, a Migration Wizard appears to help
you convert it into VB.NET. There are a lot of changes, however, so you will
probably still need to do a fair amount of work.
Control Anchors
You can anchor a control, to the lower left corner of the form for example, so you
don't need to reposition it in Form_Resize.
Easy Delpoyment
You can install executables and components buy copying them into the right
directory. No more messing with Regsvr32.
New Forms
Visual Basic forms become Windows forms.
No PrintForm
Windows forms do not have PrintForm. I don't know what we're supposed to use
instead.
Unified IDE
All Microsoft languages use the same IDE. You can debug an OCX written in
C++ while running it in a VB.NET program.
Extensibility Model
The new extensibility model applies to the IDE universally. That means you can
write Add-Ins in VB.NET and use them in C#.
GDI+
The GDI (Graphics Device Interface) has new functions including Alpha
blending, anti-aliasing, and support for more file formats.
ADO.NET
Improved performance.
No DAO/RDO Binding
VB.NET does not support DAO or RDO data binding, Data controls, or the RDO
User Connection.
Object type
Survey
Here are the results of a survey I took on 4/21/01. Comments people made about
questions 3 and 4 are at the end.
1. Are you using the VB.NET beta?
2</TD
Yes
< TR>
No 15
2. Do you plan to use Beta 2?
3</TD<
Yes
TR>
Probably 1
Maybe 4
9</TD<
No
TR>
Comments
Here are comments people made for questions 3 and 4.
3. Do you plan to use VB.NET when it is finally released?
No. I'm going to stay with VB6. This, because I am one of those rare, and becoming even
more rare, individuals who use computers for *computing* (i.e., scientific computation).
I can not care less about being able to create Internet-or Intranet-related applications, so
there is no need for me to suffer through the agonies of coming in line with an almost
entirely new development system. Furthermore, I suspect that, knowing Microsoft, the
current Beta version is really an advanced Alpha, and that when the first commercial
version hits the streets, that's when the *real* beta-testing will begin. This is cheaper for
Microsoft. I know that this will doom me to living out the rest of my professional days in
the ever receding backwaters, but that's Ok with me. I'm coming up on 66 years, and VB6
and VC++6 will serve me nicely for as long as I want to remain active in the profession.
Only if held at gunpoint
What choice do I have?
MAYBE. Depends upon its popularity and support, and the Hailstorm/Passport thing (not
sure if I/we accept that yet). Right now the company and I see no need for it in any
project. I guess I’m taking my usual wait-and-see attitude. Everybody here is scratching
their heads and wondering WHAT benefit can be derived from it?????
4. Do you want to see more VB.NET examples?
Yes. At the moment I only have time which permits me to learn either XML or VB.NEt I
need to make a decision which one to choose as of Now.
Yes, slowly. I'm just trying to understand what it is. Also, can you answer what will be
necessary to learn along with it. I gather XML will be one. Will JavaScript work with it.
That's what I mean by number 4
Yes, to find out more about it.
No. I see no reason to get all hyped up about software that isn't even on the open market
yet. As far as I am concerned, VB 6 is the latest version of VB and that is what the
examples, tips, tricks and hints should be based on.
Later on, after VB.NET has been released (and the third service pack released within 6
months after that), then our organization >>> MIGHT <<< consider using it. But
VB.NET is more like a brand new programming language than a new update of VB.
YES, I do not have time to look at it yet and I appreciate your research.
Not until I've made the switch, which will probably be never!
Yes. Even if I don't understand them. I need to get my feet wet sometime.
SURE. I figure I’ll be forced to use it someday, whether I’m ready or not. Already been
passively learning about it through the flood of e-letters stuffed into my inbox.
I've been attending the MSDN Events in Portugal and so far it looks promising (even if it
looks like a MS tailored version of J2EE). At least they are catching up...
For the developers, the .NET is "Code anyway, Run in Windows". Java in the other hand
is "Code in Java, Run anywhere"...
I'm almost ready to install Beta 1, and intend to become used to it's new paradigm. I
would like to see examples of VB.NET (mayhappen open a new layer on the site?)...
Yes, I'd like to see some after VB.NET is finally released. But, even then I'd like to focus
mainly on VB6 for a while since most people (including me) will take a while to upgrade.
Most people don't seem to like VB.NET but see it as unavoidable. You may be able to
avoid VB.NET but you will have to either stick with VB6 or learn a new language. For a
related article, see VB Users Migrating To Newer Languages Latest EDC Study Shows.
I have not commented much on the VB.NET Beta 1 in public. I'm not happy with Beta 1,
but I think it's great that Microsoft has made the Beta so widely available. I also realize
this is only a beta and that performance should improve in Beta 2 and in the final release.
I'll wait until Beta 2 before I start complaining too loudly.
Introduction
With release of Microsoft .NET platform, Visual Basic has emerged into a fully objected
oriented language (dubbed VB.NET). With these radical changes, though, the learning
curve between VB6 and VB.NET might be reasonably high even for experienced
programmers. In this article I will try to list language changes to Visual Basic along with
sample examples wherever appropriate. The list presented here may not be complete but
it does cover majority of features. If you are a VB programmer wanting to shift to
VB.NET this is a must read for you.
Note that currently (as of May 30th, 2001), ASP.NET is in Beta 1, which is freely
downloadable from www.ASP.NET. However, some of the changes discussed in this
article are changes that are proposed for the Beta 2 version, which is scheduled to be
released to the public on June 17th. These areas that are new to Beta 2 (and therefore will
not work with the Beta 1 version of ASP.NET) are marked.
• VB.NET do not have Variant data type. To achieve a result similar to variant type
you can use Object data type. (Since every thing in .NET - including primitive
data types - is an object, a variable of object type can point to any data type).
• In VB6 we used the Type keyword to declare our user defined structures. VB.NET
introduces the structure keyword for the same purpose. The rest of the syntax is
same. That is:
Structure MyStruct1
...
End Structure
Declaring Variables
Consider this simple example in VB6:
Dim x,y as integer
In this example VB6 will consider x as variant and y as integer, which is somewhat odd
behavior. VB.NET corrects this problem, creating both x and y as integers. Furthermore,
VB.NET allows you to assign initial values to the variables in the declaration statement
itself:
Dim str1 as string="hello"
VB.NET also introduces Read-Only variables. Unlike constants Read-Only variables can
be declared without initialization but once you assign a value to it, it can not be changed.
'no initialization here
Dim readonly x as integer
Note that VB6's older error handling techniques (On Error Resume Next and the like) are still
supported for backward compatibility, although when writing new applications with
VB.NET you should valiantly strive not to use these old techniques. The following code
snippet illustrates various error handling techniques with VB.NET:
Try
...
Catch
...
End Try
The above code simply "catches" any exceptions caused by offending code inside the
associated Try block. VB.NET allows you to handle specific exceptions by using multiple
Catch blocks:
Try
Set
M_var = Value
End Set
End Property
Conclusion
There are some changes to VB.NET's semantics and syntax, but these are the most
important changes that you, as an ASP.NET developer, will come across. The most
important things to keep in mind when working with VB.NET to create ASP.NET Web
pages are:
1. Variables can be typed and are no longer all Variants. That is, if you need an
Integer variable, use Dim i as Integer instead of just Dim i. (Typing your variables
leads to tremendous performance increases over untyped variables.)
2. Remember that VB.NET requires that subroutine calls have parenthesis around
the calling parameters! That means Response.Write "Hello, World!" will generate an
error. Rather, you need to place parenthesis around the parameter you are passing
into the function: Response.Write("Hello, World!")
3. VB.NET no longer supports default properties - you must explicitly specify the
property you wish to access from an object.
4. Be careful when declaring arrays. As aforementioned, all arrays in VB.NET have
a lower bound of zero, and an upper bound of the number you specify (resulting
in one more element than you may have thought you had when creating the
array).
Happy Programming!
Visual Fred
Following are the bullets you will not see on the side of the box or the marketeer's glossy.
This list has been characterized as "sensationalistic" and, frankly, it is. The changes
proposed for this language are indeed sensational! I'd personally be ecstatic if there
weren't so many fundamental hazards facing today's functional code. New development
may be very well-served, but migration is a shaky proposition, at best.
8. Arrays are not declared using the upper bound. (Addressed in Beta2)
10. Variants are not supported. Object is now the default data type.
15. True, coerced to an Integer, is not -1, but is 1 instead. (Addressed in Beta2)
21. Local variables are not necessarily visible (in scope) throughout a procedure.
30. Core language constants do not have a "vb" prefix (vbRed becomes Red).
31. Terminate will not fire when an object's last reference is released.
34. Public object variables are not safe from alteration when passed as parameters.
35. Can not expose Property procedures with mixed visibility (Friend Set/Public Get).
41. Use of As New does not force auto-reinstantiation when an object is released.
46. Code is not compiled to native, thus making decompilation much easier.
47. Resource files have changed format and old ones are not supported.
51. UDTs are not by default contiguous blocks of memory, but are objects.
63. And, Or, XOr, and Not are not bitwise operators. (Addressed in Beta2)
64. Comparison operators are not evaluated before logical operators. (Addressed in Beta2)
84. Old forms using vbPixels for Scalemode will not upgrade correctly.
94. The Name property for forms and controls is not exposed at runtime. (Addressed in Beta2)
96. File I/O will not be compatible, at all, and must be rewritten.
97. Printer object methods are not automatically upgraded and must be rewritten.
98. Clipboard object methods are not automatically upgraded and must be rewritten.
99. The Err object is not shared between managed (.NET) and unmanaged (ActiveX) code.
100. The App object is not shared between managed (.NET) and unmanaged (ActiveX) code.
114. …
115. …
116. …
Microsoft Responds!
Yes, it's true. The black helicopters, er, uh, Microsoft does pay
attention to what's going on out here. They've prepared this
response especially for you to download from this site. If you see it
offered elsewhere, there's no vouching for its authenticity. I don't
want to be accused of altering anything they've said, so I'm posting
Microsoft's response in it's original Word 97/2000 format (135K,
last updated 9-Mar-01), and assume you have some way to read
that. For now, I will withhold my own comments, but I am very
interested in hearing what you think about this latest response!
Keep those cards and letters coming, folks! People are paying
attention. :-)
Further Reading
More than likely, you'll want to read more about these issues, and
what you can do to minimize their impact. Randy Birch has
collected a number of interesting articles, many from Visual Studio
developers and product managers, as well as some of the more
poignent newsgroup commentary. He's also conducting a poll
regarding your plans for migration.
Open Sores?
The first open source decompiler and source code generator has
now been released for .NET executables. Looks like the only
protection for your source code is to house it solely on your own
servers. If it's distributed, it's wide-open. Forget the /OWNER
compiler switch, as that's only protection against willing code
breakers like ILDASM.
Parting Thoughts
"Anyway, if you really want to listen and consider other POV's,
here's mine. I personally don't use and don't like GoSub. I do
however realise that it is part of the language and other people do
use it. Forcing an incompatibility with other peoples code because I
don't use a feature is not what I would consider rational. In fact,
because it is harder for them to smoothly move into VB.NET
immediately, it has a net negative impact on the uptake and success
of the language. That will have a negative impact on me, and my
stocks. I honestly think the reality of this situation is if it's a feature
you don't use then don't worry about it, and don't ask for it to be
removed. Look at the bigger picture, and try to make decisions that
benefit all, because as a programmer community, like it or not, our
successes are in part tied to the success of the community as a
whole."
Bill McCarthy, Microsoft Visual Basic MVP, on DevX
http://www.totalenviro.com/vb7
Language
Variant
Visual Variant is a special “universal” data type that can contain any kind of data except
fixed-length strings. An Object variable is used as a pointer to an object. Variant is
Basic 6.0 the default data type.
Visual The common language runtime (CLR) uses Object for the universal data type. Visual
Basic.NET Basic.NET could have continued to use Variant for the universal data type, but chose
to adopt the naming convention of the CLR to avoid confusion for cross-language
development. The type system is simplified by having only a single universal data
type. The default data type is Object.
Upgrade Variant data types are changed to Object, so the following code:
Wizard Dim x As Variant
is upgraded to:
Dim x As Object
Currency
Currency
Visual Visual Basic 6.0 supports a data type. You cannot declare a variable to be
of type Decimal (although variants can have a subtype of Decimal).
Basic 6.0 Currency variables are stored as 64-bit numbers in an integer format, scaled by
10,000 to give a fixed-point number with 15 digits to the left of the decimal point and
4 digits to the right. This representation provides a range of -
922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Decimal variables are stored as 96-bit signed integers scaled by a variable power of
10. The power-of-10 scaling factor specifies the number of digits to the right of the
decimal point, and ranges from 0 to 28. With a scale of 0 (no decimal places), the
largest possible value is +/-79,228,162,514,264,337,593,543,950,335. With 28
decimal places, the largest value is +/-7.9228162514264337593543950335 and the
smallest non-zero value is +/-0.0000000000000000000000000001.
Visual The Currency data type does not provide sufficient accuracy to avoid rounding
Basic.NET errors, so Decimal was created as its own data type.
Upgrade Currency data types are changed to Decimal, so the following code:
Wizard Dim x As Currency
is upgraded to:
Dim x As Decimal
Date
Visual A Date variable is stored internally in a Double format and can be manipulated as
Double.
Basic 6.0 Date variables are stored as IEEE 64-bit floating-point numbers that represent dates
ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to
23:59:59. Any recognizable literal date values can be assigned to Date variables.
When other numeric types are converted to Date, values to the left of the decimal
represent date information while values to the right of the decimal represent time.
Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before 30
December 1899.
Visual Date variables are stored internally as 64-bit integers, so they cannot be manipulated
Basic.NET directly as Double. The .NET Framework provides the ToOADate and
FromOADate functions to convert between Double and Date. Representing dates as
integers simplifies and speeds up the manipulation of dates.
Upgrade Although not all cases can be detected for example, where a variant is used to store a
Wizard Date as a Double), the upgrade tool typically inserts the appropriate ToOADate or
Fixed-length strings
Visual Variables can be declared with a fixed-length string, except for
Public variables in a
class module.
Basic 6.0
Visual Fixed-length strings are not supported in the first version of the CLR. This support
Basic.NET will be added in a later version.
Upgrade In most cases, this is not an issue. A compatibility class provides fixed-length string
Wizard behavior, so the following code:
Dim MyFixedLengthString As String * 100
is upgraded to:
Dim MyFixedLengthString As New VB6.FixedLengthString(100)
See the white paper Preparing Your Visual Basic 6.0 Applications for the Upgrade to
Visual Basic.NET for a full discussion of this topic.
Type
The Type statement is used to define a user-defined data type.
Visual
Basic 6.0
Visual The names Type and User-Defined Type are confusing, because classes, enums, and
Basic.NET interfaces are also types that can be defined by users. Type and User-Defined Type
are vestiges of QuickBasic, in which structures and records were the only types that a
user could define. The CLR uses the name Type in a broad sense to include all data
types.
For this reason, the statement Type is changed to Structure in Visual Basic.NET
Upgrade Type statements are changed to Structure, so the following code:
Wizard Type MyType
MyVariable As Integer
End Type
is upgraded to:
Structure MyType
Dim MyVariable As Short
End Structure
True
True has a value of –1.
Visual
Basic 6.0
Visual True has a value of 1.
Basic.NET For language interoperability, a consistent representation is needed across all
languages.
Upgrade When a Boolean is coerced to a non-Boolean type, code is marked with an upgrade
Wizard warning. For example, the following code:
Dim MyBoolean As Boolean
Dim MyInteger As Integer
MyInteger = MyBoolean
is upgraded to:
Dim MyBoolean As Boolean
Dim MyInteger As Short
' UPGRADE_WARNING: Boolean MyBoolean is being converted into a numeric
MyInteger = MyBoolean
Empty
Variants are initialized to Empty, which automatically converts to zero when used in
Visual
a numeric expression, or to an empty string when used in a string expression.
Basic 6.0
Visual Object variables are initialized to Nothing, which automatically converts to zero
Basic.NET when used in a numeric expression, or to an empty string when used in a string
expression. Using Nothing instead of a special Empty value reduces complexity in
the language and allows for better language interoperability.
Upgrade Empty is converted to Nothing.
Wizard
Def<Type>
Visual DefBool, DefByte, DefInt, DefLng, DefCur, DefSng, DefDbl, DefDec, DefDate,
DefStr, DefObj, and DefVar statements are used at the module level to set the
Basic 6.0 default data type for variables, parameters, and procedure return types whose names
start with the specified characters.
Visual Readability and robustness of code is improved by avoiding the use of implicit type
Basic.NET declarations.
Upgrade Explicit declarations of the variable types are inserted into the code. For example, the
Wizard following code:
DefStr a-z
Sub MySub
s = “Hello”
End Sub
is upgraded to:
Sub MySub
Dim s As String
s = “Hello”
End Sub
New auto-reinstantiation
Visual A class variable declaration of the form "Dim x As New <classname>" causes the
compiler to generate code on every reference to "x". That code checks to see whether
Basic 6.0 "x" is Nothing; if it is Nothing, a new instance of the class is created. For example,
the code:
Dim x As New MyClass
'...
Call x.MyMethod()
is equivalent to:
Dim x As MyClass
'...
If x Is Nothing Then
Set x = New MyClass
End If
Call x.MyMethod()
Even after the variable is set to Nothing, it will be reinstantiated on the next call to it.
Visual A variable declaration of the form "Dim x As New <classname>" is equivalent to
Basic.NET "Dim x As <classname> = New <classname>". No special code is generated for
references to variables that are declared with this syntax.
Visual Basic.NET declarations for "As New" are far more efficient than the same
declaration in Visual Basic 6.0. For most references to such variables, the extra
overhead is unnecessary. Also, the "auto-instantiation" behavior of Visual Basic 6.0
Object finalization
The COM reference-counting mechanism is used to garbage collect object instances.
Visual
When objects are not in cycles, reference counting will immediately detect when an
Basic 6.0 object is no longer being used, and will run its termination code.
Visual A tracing garbage collector walks the objects starting with the reachable references
Basic.NET stored in stack variables, module variables, and shared variables. This tracing process
runs as a background task, and, as a result, an indeterminate period of time can lapse
between when the last reference to an object goes away and when a new reference is
added.
In some cases, clients do need the ability to force an object to release its resources.
The CLR uses the convention that such an object should implement the IDisposable
interface, which provides a Dispose method. When a client has finished using an
object with a Dispose method, it can explicitly call the Dispose method so that its
resources will be released. For example, an object that wraps a database connection
should expose a Dispose method.
The tracing garbage collector can release objects in reference cycles correctly. Also,
the performance of the tracing garbage collector is much faster than the performance
of reference counting.
Upgrade In most cases, this change will not cause a problem. If you have code that holds a
Wizard resource handle open (For example., Microsoft SQL Server™ connections or file
handles), you should explicitly close the handle. The problem is easily detected and
causes a run-time error.
Arrays
Visual Arrays can be defined with lower and upper bounds of any whole number. The
Option Base statement is used to determine the default lower bound if a range is not
Basic 6.0 specified in the declaration.
Visual To enable interoperability with other languages, all arrays must have a lower bound
Basic.NET of zero. This makes the Option Base statement no longer necessary.
Upgrade During upgrade, you have the option to treat your arrays as zero lower bound, or to
Wizard change them to an array compatibility class, as in the following example:
Assignment
Visual There are two forms of assignment: Let assignment (the default) and Set assignment.
Set assignment can be used only to assign object references. The semantics of Let
Basic 6.0 assignment are complex, but can be summarized as follows:
•
If the expression on the right-hand side of the Let statement evaluates to an
object, the default property of the instance is automatically retrieved and the
result of that call is the value that was assigned.
• If the expression on the left-hand side of the Let statement evaluates to an
object, the default Let property of that object is called with the result of
evaluating the right-hand side. An exception to this rule applies if the left-
hand side is a variant containing an object, in which case the contents of the
variant are overwritten.
Visual There is only one form of assignment. "x = y" means to assign the value of variable
Basic.NET or property "y" to the variable or property "x". The value of an object type variable is
the reference to the object instances, so if "x" and "y" are reference type variables,
then a reference assignment is performed. This single form of assignment reduces
complexity in the language and makes for much more readable code.
Upgrade Set and Let statements are removed. The default properties for strongly typed objects
Wizard are resolved and explicitly added to the code.
See the white paper Preparing Your Visual Basic 6.0 Applications for the Upgrade to
Visual Basic.NET for a full discussion of this topic.
Operator precedence
Visual The precedence of the logical and bitwise And, Or, Xor, and Not operators is higher
than the precedence of the comparison operators.
Basic 6.0
Visual The precedence of the And, Or, Xor, and Not operators is lower than the precedence
Basic.NET of the comparison operators, so "a > b And a < c" will be evaluated as "(a > b) And (a
< c)". The precedence of the new BitAnd, BitOr, and BitXor operators is higher than
the precedence of the comparison operators, so "a BitAnd &HFFFF <> 0" will be
evaluated as "((a BitAnd &HFFFF) <> 0)".
Since BitAnd, BitOr, and BitNot are operations that return numeric results, their
precedence is higher than that of the relational operators such that the default
precedence allows the result from one of these operators to be compared with another
value.
This results in a more intuitive precedence system than Visual Basic 6.0.
Upgrade This is handled by the Upgrade Wizard. See the white paper Preparing Your Visual
Wizard Basic 6.0 Applications for the Upgrade to Visual Basic.NET for a full discussion of
this topic.
Calling procedures
Two forms of procedure calls are supported: one using the Call statement, which
Visual
requires parentheses around the list of arguments, and one without the Call statement,
Basic 6.0 which requires that parentheses around the argument list not be used.
It is common in Visual Basic 6.0 for a developer to call a procedure without the call
keyword but to attempt to include parentheses around the argument list. Fortunately,
when there is more than one parameter, the compiler will detect this as a syntax error.
However, when only a single parameter is given, the parentheses around the single
Static procedures
Procedures can be declared with the Static keyword, which indicates that the
Visual
procedure's local variables are preserved between calls.
Basic 6.0
Visual The Static keyword is not supported on the procedure, and all static local variables
Basic.NET need to be explicitly declared with the Static statement.
There is very little need to have all the variables within a procedure be static.
Removing this feature simplifies the language and improves its readability, because
local variables are always stack allocated unless explicitly declared as static.
Upgrade If a procedure is marked as Static, all local variable declarations are changed to
Wizard Static.
Static Sub MySub()
Dim x As Integer
Dim y As Integer
End Sub
Is upgraded to:
Sub MySub()
Static x As Integer
Static y As Integer
End Sub
ParamArray parameters
Visual When variables are passed to a ParamArray argument, they can be modified by the
called function. ByVal ParamArray elements are not supported.
Basic 6.0
Visual When variables are passed to a ParamArray argument, they cannot be modified by
Basic.NET the called function. ByRef ParamArray elements are not supported.
A more common scenario for ParamArray arguments is for them not to modify
variables that are passed in to them. Not supporting ByRef ParamArray arguments
simplifies the ParamArray calling convention by making ParamArray arguments
be normal arrays. This enables ParamArray arguments to be extended to any
element type and allows functions that expect ParamArray arguments to be called
directly with an array rather than an argument list.
Upgrade Procedures that use ParamArray arguments are commented with an upgrade
Wizard warning. For example, the following code:
Function MyFunction(ParamArray p() As Variant)
'...
End Function
is upgraded to:
' UPGRADE_WARNING: ParamArray p was changed from ByRef to ByVal
Function MyFunction(ByVal ParamArray p() As Object)
'...
End Function
Implements
Visual The Implements statement specifies an interface or class that will be implemented in
the class module in which it appears.
Basic 6.0 The Visual Basic 6.0 model stems from the fact that COM does not actually allow
classes to have methods; instead, classes are simply a collection of interface
implementations. Visual Basic 6.0 simulates classes with methods by introducing the
concept of a default interface. When an Implements statement specifies a class, that
class implements the default interface of the class. Unfortunately the default interface
concept is not supported in other languages, and any cross-language programming
must deal directly with the default interface.
Visual Implements in Visual Basic.NET is different than in Visual Basic 6.0 in two
Basic.NET essential ways:
1. Classes can not be specified in Implements statements.
2. Every method that implements an interface method requires an Implements
clause at the end of the method declaration statement. This will specify what
interface method it implements.
Visual Basic.NET maintains a strict distinction between interfaces and classes.
Readability is improved by requiring an Implements clause on each method that
implements a method in an interface; it is obvious when reading code that the method
is being used to implement an interface method.
Upgrade If class "a" implements class "b", the interface is declared for class "b", and class "a"
Wizard is changed to implement the interface of class "b":
Property
Get, Let, Set
Visual In Visual Basic 6.0, the and property functions for a specific property
can be declared with different levels of accessibility. For example, the Property Get
Basic 6.0 function can be Public while the Property Let is Friend.
Visual The Get and Set functions for a property must both have the same level of
Basic.NET accessibility. This allows Visual Basic.NET to interoperate with other .NET
languages.
Upgrade If there is a different level of accessibility, the new property is public
Wizard
Default properties
Visual Any member can be marked as the default for a class.
Basic 6.0
Visual Only properties that take parameters can be marked as default. It is common for those
Basic.NET properties with parameters to be indexers into a collection.
This makes code more readable, since a reference to an object variable without a
member always refers to the object itself, rather than referring to the object in some
contexts and to the default property value in other contexts. For example, a statement
"Call Display(TextBox1)" might be passing the text box instance to the Display
function or it might be passing the contents of the text box.
Also, removing this ambiguity eliminates the need for a separate statement to perform
reference assignment. An assignment "x = y" always means to assign the contents of
variable "y" to variable "x", rather than to assign the default property of the object
that "y" references to the default property of the object that "x" references.
Upgrade Default properties are resolved where possible. Error comments are added where they
Wizard cannot be resolved (on-late bound objects).
Enumerations
Visual Enumeration constants can be referenced without qualification.
Basic 6.0
Visual Enumerations constants can be referenced without qualification if an Import for the
Basic.NET enumeration is added at file or project level.
This keeps consistency with classes, structures, and interfaces in which members can
While
While statements are ended with a WEnd statement.
Visual
Basic 6.0
Visual To be consistent with other block structures, the terminating statement for While is
Basic.NET now End While. This improves language consistency and readability.
Upgrade WEnd statements are changed to End While.
Wizard
GoSub…Return
The GoSub line … Return statement branches to and returns from a subroutine
Visual
within a procedure.
LSet
LSet pads a string with spaces to make it a specified length, or copies a variable of
Visual
one user-defined type to a variable of a different user-defined type.
Basic 6.0
Visual The LSet statement is not supported. LSet is not type safe, so it can result in errors at
Basic.NET run time. Also, because it is not type safe it requires full trust in order to be executed.
Removing the LSet statement discourages the copying of one structure over another;
however, you can achieve the same effect by modifying your Visual Basic.NET code
to use RtlCopyMemory.
Upgrade This statement:
Wizard LSet a1 = a2
It is commented with an upgrade error
' UPGRADE_ISSUE: LSet cannot assign a UDT from one type to another
LSet a1 = a2
Debug.Print
Debug.Print outputs a line of text to the Immediate window.
Visual
Basic 6.0
Visual In Visual Studio.NET, the Immediate window is replaced with the Immediate and Output windows.
Basic.NET The Immediate window is used to enter and display results when an application is in break mode. The
Output window shows build information and program output.
Debug.WriteLine outputs a line of text to the Output window. There is also a
Debug.Write method that outputs text to the Output window without a linefeed.
Upgrade Debug.Print is upgraded to Debug.WriteLine.
Wizard
Resource files
Visual Visual Basic 6.0 supports one .res file per project.
Basic 6.0
Visual Visual Basic.NET has rich support for resources. Forms can be bound to retrieve
Basic.NET resources automatically from the new .resX-formatted resource files. Any CLR class
can be stored in a .resX file.
Upgrade Files are upgraded from .res to .resX, and code is changed to load from the .resX
Wizard files.
Windows Applications
Visual Basic forms
Visual Visual Basic 6.0 has its own forms package for creating graphical Windows
applications.
Basic 6.0
Visual Windows Forms is a new forms package for Visual Basic.NET. Because Windows
PrintForm method
Visual The PrintForm method sends a bit-by-bit image of a Form object to the printer.
However, this printing feature doesn't work correctly on some forms.
Basic 6.0
Visual In Windows Forms, Visual Basic.NET has a printing framework that allows you to
Basic.NET build complex print documents quickly. It also includes a built-in Print Preview
dialog box.
Upgrade PrintForm method calls are commented with an upgrade error. You can use the new
Wizard printing framework to build a print document, or you can even grab a screenshot of
the application window and print it.
Name property
Visual The Name property returns the name used in code to identify a form, control, or data
access object. It is read-only at run time.
Basic 6.0
Visual Windows Forms does not support the Name property for forms and controls at run
Caption property
Visual Some controls, such as Label, have a Caption property that determines the text
displayed in or next to the control. Other controls, such as TextBox, have a Text
Basic 6.0 property that determines the text contained in the control.
Visual In Windows Forms, the property that displays text in a control is consistently called
Basic.NET Text on all controls. This simplifies the use of controls.
Upgrade Caption properties for the controls are changed to Text.
Wizard
Tag property
Visual The Tag property returns or sets an expression that stores any extra data needed for
your program.
Basic 6.0 In Visual Basic 6.0, you need the Tag property because you cannot extend the built-
in controls.
Visual In Windows Forms, you can use inheritance to extend the built-in controls and add
Basic.NET your own properties. Having inheritance available as a tool makes the built-in
controls significantly more flexible. Not only can you add as many properties as you
like, you can also make those properties strongly typed.
Upgrade A Windows Forms extender Tag control in the compatibility library is used to
Wizard provide the same functionality.
ScaleMode property
Visual The ScaleMode property returns or sets a value that indicates the unit of
measurement for coordinates of an object when using graphics methods or when
Basic 6.0 positioning controls.
Visual Windows Forms simplifies form layout by always making measurements in pixels.
Basic.NET In addition, Windows Forms has a better way to handle resizing. The
AutoScaleBaseSize property automatically adjusts the scale according to the
resolution (dpi) of the screen and font size you use.
Upgrade Code that used 'twips' (the default Visual Basic 6.0 ScaleMode setting) upgrades
Wizard perfectly. If ScaleMode is non-twips, you'll have sizing issues.
See the white paper Preparing Your Visual Basic 6.0 Applications for the Upgrade to
Visual Basic.NET for a full discussion of this topic.
Fonts
Visual Forms and controls can use any Windows font.
Basic 6.0
Visual Forms and controls can only use TrueType or OpenType fonts. These types of fonts
Basic.NET solve many inconsistencies across different operating-system versions and their
Screen.MousePointer property
Visual The MousePointer property on the Screen object returns or sets a value indicating
the type of mouse pointer displayed when the mouse is outside your application's
Basic 6.0 forms at run time.
Visual The mouse pointer can be manipulated for forms inside of the application, but it
Basic.NET cannot when it's outside of the application. We will be addressing this feature in a
future release.
Upgrade Use of Sceen.MousePointer is commented with an upgrade error.
Wizard
Timer.Interval property
Visual The Interval property on a Timer control returns or sets the number of milliseconds
between calls to the Timer event. If it's set to 0, it disables the Timer control. The
Basic 6.0 Enabled property also determines whether the timer is running. This is confusing,
because even when the Enabled propertyis true, the timer won't be enabled if the
interval is 0.
Visual The Interval property indicates the time, in milliseconds, between timer ticks. This
Basic.NET property cannot be set to 0. The Enabled property indicates whether the timer is
running. This provides a more intuitive behavior to simplify coding with Timer
objects.
Upgrade Where the Upgrade Wizard can detect that Timer.Interval is set to 0, it will be
Wizard commented with an upgrade error.
You are advised to use Timer.Enabled in your Visual Basic 6.0 applications, as this
upgrades perfectly.
Control arrays
Visual A control array is a group of controls that share the same name and type. They also
share the same event procedures. A control array has at least one element and can
Basic 6.0 grow to as many elements as your system resources and memory permit. Elements of
the same control array have their own property settings.
Visual The Windows Form architecture natively handles many of the scenarios for which
Basic.NET control arrays were used. For instance, in Windows Forms you can handle more than
one event on more than one control with a single event handler.
Upgrade A Control Array Windows Forms extender control in the compatibility library
Wizard provides this feature.
Menu controls
Visual A Menu control represents each item in a menu tree. The same Menu control
instance can be used simultaneously as a main menu or a context menu.
Basic 6.0
Visual A MenuItem control represents each item in a menu tree. The MenuItem control can
Image control
Visual The Image and PictureBox controls both display a graphic from a bitmap, icon,
metafile, enhanced metafile, JPEG, or GIF file.
Basic 6.0
Visual The Visual Basic.NET PictureBox control replaces the Visual Basic 6.0 PictureBox
Basic.NET and Image controls. The Windows Forms PictureBox control also supports animated
GIFs. However, if you require a very lightweight solution for painting an image onto
a form, you can also override the OnPaint event for the form and use the
DrawImage method.
Upgrade Image controls are changed to PictureBox controls.
Wizard
Windowless controls
Lightweight controls, sometimes referred to as windowless controls, differ from
Visual
regular controls in one significant way: They don't have a window handle (hWnd
Clipboard
Visual The Clipboard object provides access to the system clipboard.
Basic 6.0
Visual The Clipboard class provides methods to place data on and retrieve data from the
Basic.NET system clipboard. The new Clipboard class offers more functionality and supports
more clipboard formats than the Visual Basic 6.0 Clipboard object. The object
model has been restructured to support these.
Upgrade The existing clipboard code cannot automatically be upgraded because of the
Wizard differences between object models. Clipboard statements will be commented with an
upgrade error.
Web Applications
WebClasses
Visual A WebClass is a Visual Basic component that resides on a Web server and responds
to input from the browser. A WebClass typically contains WebItems that it uses to
Basic 6.0 provide content to the browser and expose events.
Visual Web Forms is a .NET Framework feature that you can use to create a browser-based
Basic.NET user interface for your Web applications. Visual Basic.NET has a WYSIWYG
designer for graphical Web Form creation using controls from the Toolbox. This
Data
ADO, RDO, and DAO code
Visual ADO, RDO, and DAO objects are used for connected and disconnected data access.
Basic 6.0
Visual ADO.NET provides additional classes for disconnected data access. These classes
Basic.NET provide performance and scalability improvements over previous versions of
ActiveX® Data Objects (ADO) when used in distributed applications. They also
allow simpler integration of XML data with your database data.
Upgrade ADO, RDO, and DAO can still be used in Visual Basic.NET code.
October 2000
Summary: This document provides recommendations for Microsoft Visual Basic developers planning to upgrade their applications to
Visual Basic.NET. It includes information on the Visual Basic.NET Upgrade Tool and discusses architectural guidelines for enabling
a smooth upgrade from Visual Basic 6 to Visual Basic.NET. (26 printed pages)
Contents
Overview
What Is Visual Basic.NET?
Why Is Visual Basic.NET Not 100% Compatible?
Upgrading to Visual Basic.NET
Working with Both Visual Basic 6.0 and Visual Basic.NET
Architecture Recommendations
Browser-based Applications
Client/Server Projects
Single-tier Applications
Data
Upgrading
Variant to Object
Integer to Short
Property Syntax
Visual Basic Forms to Windows Forms
Interfaces
Upgrade Report and Comments
Programming Recommendations
Use Early-Binding
Use Date for Storing Dates
Resolve Parameterless Default Properties
Avoid Null Propagation
Use Zero Bound Arrays
Use Constants Instead of Underlying Values
Arrays and Fixed-Length Strings in User-Defined Types
Avoid Legacy Features
Windows APIs
Considerations for Forms and Controls
Overview
This document provides recommendations for developers using Microsoft® Visual Basic® who are planning to upgrade their
applications to Microsoft Visual Basic.NET.
Visual Basic.NET will open and upgrade Visual Basic 6.0 projects to Visual Basic.NET technologies, but in most cases you will need
to make some modifications to your projects after bringing them into Visual Basic.NET. The purpose of this document is to
recommend how to design and implement your current Visual Basic projects to minimize the number of changes you will need to
make when they are upgraded to Visual Basic.NET. Where appropriate, we use new language constructs; however, this document is
not intended to be a Visual Basic.NET language reference.
Note Visual Basic.NET is still in development; some compatibility details may change before the product is
released. Following the guidelines in this document does not guarantee your code will not require changes;
instead the guidelines aim to reduce the amount of work needed for conversion.
The upgrade wizard and command-line upgrade tools in Visual Basic.NET are still in an early stage of
development and, as such, their functionality is limited. The purpose of including them in the Beta release is to
give you a feel for how the upgrade process will work and to see how VB 6.0 code is modified to work in
VB.NET; in Beta1, most real-world projects probably cannot be migrated successfully.
These new features open new doors for the Visual Basic developer: With Web Forms and ADO.NET, you can now rapidly develop
scalable Web sites; with inheritance, the language now truly supports object-oriented programming; Windows Forms natively supports
accessibility and visual inheritance; and deploying your applications is now as simple as copying your executables and components
from directory to directory.
Visual Basic.NET is now fully integrated with the other Microsoft Visual Studio.NET languages. Not only can you develop
application components in different programming languages, your classes can now inherit from classes written in other languages
using cross-language inheritance. With the unified debugger, you can now debug multiple language applications, irrespective of
whether they are running locally or on remote computers. Finally, whatever language you use, the Microsoft .NET Framework
provides a rich set of APIs for Microsoft Windows® and the Internet.
For example, many of the new features found in Windows Forms could have been added to the existing code base as new controls or
more properties. However, this would have been at the cost of all the other great features inherent to Windows Forms, such as security
and visual inheritance.
One of our major goals was to ensure Visual Basic code could fully interoperate with code written in other languages, such as
Microsoft Visual C#™ or Microsoft Visual C++®, and enable the Visual Basic developer to harness the power of the .NET
Framework simply, without resorting to the programming workarounds traditionally required to make Windows APIs work. Visual
Basic now has the same variable types, arrays, user-defined types, classes, and interfaces as Visual C++ and any other language that
targets the Common Language Runtime; however, we had to remove some features, such as fixed-length strings and non-zero based
arrays from the language.
Visual Basic is now a true object-oriented language; some unintuitive and inconsistent features like GoSub/Return and DefInt have
been removed from the language.
The result is a re-energized Visual Basic, which will continue to be the most productive tool for creating Windows-based applications,
and is now positioned to be the best tool for creating the next generation Web sites.
This happens automatically when you open a Visual Basic 6.0 project in Visual Basic.NET: the Upgrade Wizard steps you through the
upgrade process and creates a new Visual Basic.NET project (your existing project is left unchanged). This is a one-way process; the
new Visual Basic.NET project cannot be opened in Visual Basic 6.0.
When your project is upgraded, the language is modified for any syntax changes and your Visual Basic 6.0 Forms are converted to
Windows Forms. In most cases, you will have to make some changes to your code after it is upgraded. This is required because certain
objects and language features either have no equivalent in Visual Basic.NET, or have an equivalent too dissimilar for an automatic
upgrade. After the upgrade, you may also want to change your application to take advantage of some of the new features in Visual
Basic.NET.
For example, Windows Forms supports control anchoring, so you can remove most of your old Visual Basic 6.0 Form resize code:
To help you make the changes, after your project is upgraded, Visual Basic.NET adds an ‘upgrade report’ to your project itemizing
any problems, and inserts comments into your upgraded code alerting you to statements that will need to be changed. Because these
comments are displayed as ‘TO DO’ tasks in the new Task List window, you can easily see what changes are required, and navigate to
the code statement simply by double-clicking the task. Each task and item in the upgrade report is associated with an online Help topic
giving further guidance as to why the code needs to be changed, and what you need to do.
By following the recommendations in this document, you can minimize and, in some cases, eliminate the changes needed after
upgrading your project to Visual Basic.NET. In most cases, the recommendations simply represent good programming practices;
however, we also identify the objects and methods which have no equivalents, and which should be used sparingly if you intend to
upgrade your project to Visual Basic.NET.
Both Visual Basic.NET and Visual Basic 6.0 can be installed on the same computer and run at the same time. Likewise, applications
written in Visual Basic.NET and Visual Basic 6.0 can be installed and executed on the same computer. Components written in Visual
Basic.NET can interoperate with COM components written in earlier versions of Visual Basic and other languages. For example, you
can drop an ActiveX control written in Visual Basic 6.0 onto a Visual Basic.NET Windows Form, use a Visual Basic 6.0 COM object
from a Visual Basic.NET class library, or add a reference to a Visual Basic.NET library to a Visual Basic 6.0 executable.
Components compiled with Visual Basic.NET have subtle run-time differences from components compiled with Visual Basic 6.0. For
starters, because Visual Basic.NET objects are released through garbage collection, when objects are explicitly destroyed, there may
be a lag before they are actually removed from memory. There are additional differences such as the variant/object changes described
later in this document. The combined result of these differences is that Visual Basic.NET applications will have similar but not
identical run-time behavior to Visual Basic 6.0 applications.
In addition, Visual Basic.NET makes binary compatibility between Visual Basic.NET components and those in Visual Basic 6.0
unnecessary. Components now have a more robust versioning and deployment system than ever before, files can be deployed by
simply copying to a directory (no more RegSvr32), and upgrading to a new version of a component is as simple as replacing the old
file with a new file. All you have to do is ensure classes and methods are compatible with the previous version.
Browser-based Applications
Visual Basic 6.0 and Microsoft Visual Studio® 6.0 offered several technologies for creating browser-based Internet and intranet
applications:
• Webclasses
• DHTML projects
• ActiveX documents
Visual Basic.NET introduces ASP.NET, an enhanced version of ASP, and adds to the architecture with Web Forms, which are HTML
pages with Visual Basic events. The architecture is server-based.
Below is a list of recommendations and architectural suggestions for developing Visual Basic 6.0 browser-based applications that will
most seamlessly migrate to Visual Basic.NET projects:
• We recommend you use the Microsoft multi-tier architecture guidelines (see Building Solutions Today With Microsoft's
Web Solution Platform)to create your applications, create the interface with ASP, and use Visual Basic 6.0 or Visual C++ 6.0
COM objects for your business logic. ASP is fully supported in Visual Basic.NET, and you can continue to extend your
application using ASP, ASP.NET, and Web Forms. The Visual Basic 6.0 and Visual C++ 6.0 business objects can either be used
• DHTML applications contain DHTML pages and client-side DLLs. These applications cannot be automatically upgraded
to Visual Basic.NET. We recommend you leave these applications in Visual Basic 6.0.
• ActiveX documents are not supported in Visual Basic.NET, and like DHTML projects, cannot be automatically upgraded.
We recommend you either leave your ActiveX document applications in Visual Basic 6.0 or, where possible, replace ActiveX
• Visual Basic 6.0 ActiveX documents and DHTML applications can interoperate with Visual Basic.NET technologies. For
example, you can navigate from a Visual Basic.NET Web Form to a Visual Basic 6.0 DHTML page, and vice–versa.
• Webclasses no longer exist in Visual Basic.NET. Webclass applications will be upgraded to ASP.NET; however, you will
have to make some modifications after upgrading. Existing Webclass applications can interoperate with Visual Basic.NET Web
Forms and ASP applications, but for new projects we recommend you use the Windows DNA platform of ASP with Visual
Client/Server Projects
Visual Basic 6.0 offered several technologies for creating client/server applications:
• User controls
In Visual Basic.NET, there is a new form package: Windows Forms. Windows Forms has a different object model than Visual Basic
6.0 Forms, but is largely compatible. When your project is upgraded, Visual Basic Forms are converted to Windows Forms.
Visual Basic.NET improves support for developing middle-tier MTS and COM+ component services components. Using the unified
debugger, you can step from a client application into an MTS/COM+ component and back to the client. You can also use the unified
debugger to step through Visual Basic 6.0 MTS/COM+ components (providing they are compiled to native code, with symbolic debug
information and no optimizations).
Visual Basic.NET also introduces a new middle-tier component, Web Services. Web Services are hosted by ASP.NET, and use the
HTTP transport allowing method requests to pass through firewalls. They pass and return data using industry standard XML, allowing
other languages and other platforms to access their functionality. Although they do not support MTS transactions, you may want to
change your MTS/COM+ components to Web Services in cases where you do not need distributed transactions but still want to
interoperate with other platforms. Although there is no automatic method for this, the task is trivial and can be completed in minutes
using a drag-and-drop operation after your project has been upgraded to Visual Basic.NET.
When your project is upgraded, user controls are upgraded to Windows controls; however, custom property tag settings and
accelerator keys assignments will not be upgraded.
Single-tier Applications
Single-tier database applications are typified by a Visual Basic application storing data in an Microsoft Access database. These
applications will upgrade to Visual Basic.NET with some limitations (see the Data section later in this document).
Now that the Visual Basic.NET IDE is a fully integrated part of the Visual Studio.NET IDE, Visual Basic.NET has a new language-
neutral extensibility model. Visual Basic.NET add-ins are now Visual Studio.NET add-ins, and you can automate and add features to
any language in Visual Studio.NET. For example, you can write a Visual Basic.NET add-in that re-colors a Visual C# Windows Form
or adds comments to a Visual Basic class. In order to provide this functionality, Visual Basic.NET has moved away from the old
extensibility model, and you will need to change the extensibility objects in your application to take advantage of the new features.
Many applications fall under the category of Utility programs. Utility applications that manipulate files, registry settings, and the like
will often upgrade without requiring any additional changes. After upgrading, there are many new features you can take advantage of,
such as exception handling in the language to capture file system errors, and using .NET Framework registry classes to manipulate the
registry. One thing to be aware of is that applications relying on specific performance characteristics of Visual Basic 6.0, such as
arcade games, will probably require some modifications because Visual Basic.NET has different performance characteristics. For
games support in Visual Basic.NET, you can use Microsoft DirectX® 7, or the new version of GDI. GDI+ introduces many new
features, including Alpha blending support for all 2-D graphics primitives, anti-aliasing, and expanded support for image file formats.
Data
Visual Basic.NET introduces an enhanced version of ADO called ADO.NET. ADO.NET targets disconnected data, and provides
performance improvements over ADO when used in distributed applications. ADO.NET offers read/write data binding to controls for
Windows Forms and read-only data binding for Web Forms.
DAO, RDO, and ADO can still be used in code from Visual Basic.NET, with some trivial modifications (covered in the language
section of this document). However, Visual Basic.NET does not support DAO and RDO data binding to controls, data controls, or
RDO User connection. We recommend that if your applications contain DAO or RDO data binding you either leave them in Visual
Basic 6.0 or upgrade the DAO and RDO data binding to ADO before upgrading your project to Visual Basic.NET, as ADO data
binding is supported in Windows Forms. Information on how to do this is available in the Visual Basic 6.0 Help.
Upgrading
When your code is upgraded, Visual Basic.NET creates a new upgraded project and makes most of the required language and object
changes for you. The following sections provide a few examples of how your code is upgraded.
Variant to Object
Previous versions of Visual Basic supported the Variant datatype, which could be assigned to any primitive type (except fixed-length
strings), Empty, Error, Nothing and Null. In Visual Basic.NET, the functionality of the Variant and Object datatypes is combined
into one new datatype: Object. The Object datatype can be assigned to primitive datatypes, Empty, Nothing, Null, and as a pointer to
an object.
When your project is upgraded to Visual Basic.NET, all variables declared as Variant are changed to Object. Also, when code is
inserted into the editor, the Variant keyword is replaced with Object.
Integer to Short
In Visual Basic.NET, the datatype for 16-bit whole numbers is now Short, and the datatype for 32-bit whole numbers is now Integer
(Long is now 64 bits). When your project is upgraded, the variable types are changed:
Dim x As Integer
dim y as Long
is upgraded to:
Dim x As Short
dim y as Integer
Property Syntax
Visual Basic.NET introduces a more intuitive syntax for properties, which groups Get and Set together. Your property statements are
upgraded as shown in the following example:
Property Get MyProperty() As Integer
MyProperty = m_MyProperty
End Property
Property Let MyProperty(NewValue As Integer)
m_MyProperty = NewValue
End Property
is upgraded to:
Property MyProperty() As Short
Get
MyProperty = m_MyProperty
End Get
Visual Basic.NET has a new forms package, Windows Forms, which has native support for accessibility and has an in-place menu
editor. Your existing Visual Basic Forms are upgraded to Windows Forms.
Figure 2. Windows Forms in-place menu editor. (Click figure to see larger image.)
Interfaces
In previous versions of Visual Basic, interfaces for public classes were always hidden from the user. In Visual Basic.NET, they can be
viewed and edited in the Code Editor. When your project is upgraded, you choose whether to have interface declarations automatically
created for your public classes.
After your project is upgraded, an upgrade report is added to your project, itemizing any changes you will need to make to your
upgraded code. Additionally, comments are added to your code to alert you to any potential problems. These comments show up
automatically in the Visual Studio.NET Task List.
Programming Recommendations
This section provides recommendations for how you should write code to minimize the changes you will need to make after upgrading
your project to Visual Basic.NET.
Use Early-Binding
Both Visual Basic 6.0 and Visual Basic.NET support late-bound objects, which is the practice of declaring a variable as the Object
datatype and assigning it to an instance of a class at run time. However, during the upgrade process, late-bound objects can introduce
problems when resolving default properties, or in cases where the underlying object model has changed and properties, methods, and
events need to be converted. For example, suppose you have a Form called Form1 with a label called Label1; the following Visual
Basic 6.0 code would set the caption of the label to “SomeText”:
Dim o As Object
Set o = Me.Label1
o.Caption = "SomeText"
In Visual Basic.NET Windows Forms, the Caption property of a label control is now called Text. When your code is upgraded, all
instances of the Caption property are changed to Text, but because a late-bound object is type-less, Visual Basic cannot detect what
type of object it is, or if any properties should be translated. In such cases, you will need to change the code yourself after upgrading.
If you rewrite the code using early-bound objects, it will be upgraded automatically:
Dim o As Label
Set o = Me.Label1
o.Caption = "SomeText"
Where possible you should declare variables of the appropriate object type rather than simply declaring them as the Object datatype.
In the cases where you do use Object and Variant variables in your Visual Basic 6.0 code, we recommend you use explicit conversions
when you assign the variables, perform operations on the variables, or pass the variables to a function. For example, the intention of
the ‘+’ operation in the following code is unclear:
The above example may result in a run-time error in Visual Basic.NET. Rewriting the final line to use explicit conversions ensures the
code will work:
Var3 = CInt(Var1) + CInt(Var2) 'GOOD: explicit conversion
Visual Basic.NET supports overloading functions based on parameter type. For example, the Environ function now has two forms:
Earlier versions of Visual Basic supported using the Double datatype to store and manipulate dates. You should not do this in Visual
Basic.NET, because dates are not internally stored as doubles. For example, the following is valid in Visual Basic 6.0, but may cause a
compile error in Visual Basic.NET:
Dim dbl As Double
Dim dat As Date
dat = Now
dbl = dat 'VB.NET: Double can't be assigned to a date
dbl = DateAdd("d", 1, dbl) 'VB.NET: Can't use Double in date functions
dat = CDate(dbl) 'VB.NET: CDate can't convert double to date
The .NET framework provides the ToOADate and FromOADate functions to convert between doubles and dates. However, when
your project is upgraded to Visual Basic.NET, it is difficult to determine the intention of code that uses doubles to store dates. To
avoid unnecessary modifications to your code in Visual Basic.NET, always use the Date datatype to store dates.
In Visual Basic 6.0, many objects expose default properties, which can be omitted as a programming shortcut. For example, TextBox
has a default property of Text, so instead of writing:
MsgBox Form1.Text1.Text
you use the shortcut:
MsgBox Form1.Text1
The default property is resolved when the code is compiled. In addition, you could also use default properties with late-bound objects,
as in the following example:
Dim obj As Object
Set obj = Form1.Text1
MsgBox obj
In the late-bound example, the default property is resolved at run time, and the MsgBox displays the value of the default property of
the TextBox as Text1.
Previous versions of Visual Basic supported Null propagation. Null propagation supports the premise that when null is used in an
expression, the result of the expression will itself be Null. In each case in the following example, the result of V is always Null.
Dim V
V = 1 + Null
V = Null + Right$("SomeText", 1)
V = Right("SomeText", 0)
Null propagation is not supported in Visual Basic.NET. The statement 1+Null will generate a type mismatch in Visual Basic.NET.
Additionally, where Visual Basic 6.0 had two versions of the Left function—Left$ returning a string, Left returning a variant which
could be Null—Visual Basic.NET only has one version, Left, which always returns a string.
In order to be compatible with both Visual Basic 6.0 and Visual Basic.NET you should always write code to test for Null instead of
relying on Null propagation. Furthermore, in Visual Basic.NET, the following functions will no longer return Null:
Chr Mid
Command Oct
CurDir Right
Date RTrim
Environ Space
Error Str
Hex Time
LCase Trim
A related issue involves concatenating a string with a Null value. When programming with database objects, it is common practice to
concatenate an "empty string" to a field to ensure that Null values are coerced to an empty string. For example:
MyString = rs!Field1 & ""
This technique is still supported in Visual Basic.NET. When a Null value is concatenated with an empty string (using the & operator),
the result is an empty string.
Visual Basic 6.0 allowed you to define arrays with lower and upper bounds of any whole number. You could also use ReDim to
reassign a variant as an array. To enable interoperability with other languages, arrays in Visual Basic.NET must have a lower bound of
zero, and ReDim cannot be used unless the variable was previously declared with Dim As Array. Although this restricts the way
arrays can be defined, it does allow you to pass arrays between Visual Basic.NET and any other .NET language. The following
example shows the restriction:
Dim a(1 To 10) As Integer 'LBound must be 0 in VB.NET
Dim v
ReDim v(10) 'Can't use ReDim without Dim in VB.NET
Dim b(10) As Integer 'GOOD: Creates an array of 11 integers
ReDim b(5) As Integer 'GOOD: Can ReDim previously Dimed var
A side effect is that Option Base 0|1 is removed from the language.
When your project is upgraded to Visual Basic.NET, any option base statements are removed from your code. If the array is zero
bound, it is left unchanged. However, if an array is non-zero bound, then the lower bound is removed and a warning is inserted into
the code, as in the following example:
Dim a(1 To 10) As Integer
changes to:
'UPGRADE_WARNING: Lower Bound of array a was changed from 1 to 0
Dim a(10) As Integer
In many cases, the upgraded code will work as it did before. However, if your application logic relies on the lower bound being 1, then
you will need to make some modifications. Dim, ReDim, and LBound statements are marked with warnings to help you review the
changes.
For this reason, you should use zero bound arrays in your Visual Basic 6.0 code, avoid using ReDim as an array declaration, and
avoid using Option Base 1.
When writing code, try to use constants rather than relying on their underlying values. For example, if you are maximizing a form at
run time, use:
Me.WindowState = vbMaximized 'Good: Constant name used
rather than:
Me.WindowStyle = 2 'Avoid using underlying value
Me.WindowStyle = X 'Avoid using variables
Likewise, use True and False instead of -1 and 0.
In Visual Basic.NET, the values and in some cases the names of some properties and constants have changed. When your project is
upgraded to Visual Basic.NET, most constants are changed automatically for you; however, if you use underlying values or variables
instead of the constant names, many cases cannot be upgraded automatically. Using constant names minimizes the number of
modifications you have to do.
Due to changes made which allow Visual Basic.NET arrays and structures to be fully compatible with other Visual Studio.NET
languages, fixed-length strings are no longer supported in the language. In most cases this is not a problem, because there is a
compatibility class which provides fixed-length string behavior, so the code:
When your code is upgraded, user-defined types with fixed-length strings or arrays will be marked with a comment telling you to
initialize the fixed-length string or array before using the user-defined type. However, you can shield yourself from this modification
by changing your Visual Basic 6.0 user-defined types to use strings instead of fixed-length strings, and uninitialized arrays instead of
fixed-size arrays. For example:
Private Type MyType
MyArray(5) As Integer
MyFixedString As String * 100
End Type
Sub Bar()
Dim MyVariable As MyType
End Sub
can be changed to:
Because they have been removed from the language, you should avoid using the following keywords:
• Def<type>
• Computed GoTo/GoSub
• GoSub/Return
• LSet
Def<type>
In previous versions of Visual Basic, DefBool, DefByte, DefInt, DefLng, DefCur, DefSng, DefDbl, DefDec, DefDate, DefStr, DefObj
and DefVar were used in the declarations section of a module to define a range of variables as a certain type. For example:
DefInt A-C
defined all variables beginning with the letter A, B, or C as an integer. Instead of using Def<type> statements, you should explicitly
declare variables.
GoSub/Return
GoSub and Return statements are not supported in Visual Basic.NET. In most cases you can replace these with functions and
procedures.
LSet
In Visual Basic 6.0, the LSet statement could be used to assign a variable of one user-defined type to another variable of a different
user-defined type. This functionality is not supported in Visual Basic.NET.
Windows APIs
Many APIs can be used exactly as they were in Visual Basic 6.0, with the caveat that you have to adjust your data types accordingly.
The Visual Basic 6.0 Long datatype is now the Visual Basic.NET Integer datatype, and the Visual Basic 6.0 Integer datatype is now
the Visual Basic.NET Short datatype. During the upgrade, these changes are made for you, and simple APIs work exactly the same as
they did in Visual Basic 6.0. For example:
Private Declare Function GetVersion Lib "kernel32" () As Long
Function GetVer()
Dim Ver As Long
Ver = GetVersion()
MsgBox ("System Version is " & Ver)
End Function
changes to:
Private Declare Function GetVersion Lib "kernel32" () As Integer
Function GetVer()
Dim Ver As Integer
Ver = GetVersion()
MsgBox("System Version is " & Ver)
End Function
In addition to numeric datatype upgrades, Visual Basic 6.0 had a fixed-length string data type which is not supported in Visual
Basic.NET, and which is upgraded to a fixed-length string wrapper class. In many cases in Visual Basic 6.0 you can perform the same
action using a normal string. For example:
Private Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As Long) As Long
Function GetUser()
Dim Ret As Long
Dim UserName As String
Dim Buffer As String * 25
Ret = GetUserName(Buffer, 25)
UserName = Left$(Buffer, InStr(Buffer, Chr(0)) - 1)
MsgBox (UserName)
End Function
can be better written using a normal string explicitly set to length 25 instead of a fixed-length string:
Dim Buffer As String
There are three cases where you may need to make some changes. The first is passing user-defined types that contain fixed-length
strings or byte arrays to APIs. In Visual Basic.NET you may need to change your code, adding the MarshallAs attribute (from
System.Runtime.InteropServices) to each fixed-length string or byte array in the user-defined type. The second case is using the As
Any variable type in a Declare statement. This is not supported in Visual Basic.NET. Variables of type As Any were often used to
pass a variable that was either a string or Null; you can replace this Visual Basic 6.0 usage by declaring two forms of the API, one
with longs, one with strings. For example, the GetPrivateProfileString API has a parameter lpKeyName of type As Any:
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpDefault As String, ByVal
lpReturnedString As String, ByVal nSize As Long, ByVal
lpFileName As String) As Long
You can remove the “As Any” by replacing the Declare with two versions; one that accepts a long, and one that accepts a string:
The final area where you may need to make some changes is if you are using APIs that perform thread creation, Windows subclassing,
message queue hooking, and so on. Some of these functions will cause a run-time error in Visual Basic.NET. Many of these APIs
have equivalents in Visual Basic.NET or the .NET Framework. You will have to fix these on a case-by-case basis.
Visual Basic.NET has a new forms package, Windows Forms. Windows Forms is largely compatible with the forms package found in
Visual Basic 6; however, there are some key differences that are outlined below:
• Windows Forms does not support the OLE container control; you should avoid using this control in your Visual Basic 6.0
applications.
• There is no shape control in Windows Forms. Square and rectangular shapes will be upgraded to labels, while ovals and
circles cannot be upgraded. You should avoid using these in your applications.
• Windows Forms has a new set of graphics commands that replace the Form methods Circle, CLS, PSet, Line, and Point.
Because the new object model is quite different from Visual Basic 6.0, these methods cannot be upgraded.
• For the Timer control, setting the Interval property to 0 does not disable the timer; instead the interval is reset to 1. In your
Visual Basic 6.0 projects, you should set Enabled to False instead of setting the Interval to 0.
• Windows Forms has two menu controls, MainMenu and ContextMenu, whereas Visual Basic 6.0 has one menu control,
Menu, which can be opened as a MainMenu or a ContextMenu. Menu controls are upgraded to MainMenu controls, but you will
not be able to use them as ContextMenus; you will have to recreate your ContextMenus.
• Although Windows Forms has support for drag-and-drop functionality, the object model is quite different from Visual
Basic 6.0. Therefore, the Visual Basic 6.0 drag-and-drop properties and methods cannot be upgraded.
• The .NET framework has an improved Clipboard object (System.Windows.Clipboard) that offers more functionality and
supports more clipboard formats than the Visual Basic 6.0 Clipboard object. However, because of differences between object
• To ensure your forms are upgraded to the right size, you should always use the default ScaleMode of twips in your
applications. During the upgrade, Visual Basic.NET transforms your forms coordinates from twips to pixels.
• Windows Forms only supports true-type and open-type fonts. If your application uses other fonts, these fonts will be
changed to the system’s default font, and all formatting (size, bold, italic, underline) will be lost. This applies to the default VB6
font MS Sans Serif. For this reason, we recommend you use Arial instead of MS Sans Serif, wherever you have formatted text.
Get ready for the biggest transition you'll make since you learned Visual Basic. Microsoft's new .NET
framework will change the way you develop software from top to bottom. Microsoft is bringing all its
languages onto a common platform; they'll share the same runtime and use the same datatypes. VB is
changing the most, with many of the changes bringing VB in line with the other languages.
What you need:
Visual Basic 4.0 or later
Here are 10 tangible things you can do today to make the migration easier. You might already be doing
some of them, because several have long been considered good coding practices. Others are new and
probably unexpected.
1
Stop Using Default Properties and Methods
VB6 and earlier versions allow you to write code like this, where lblTitle is a Label control:
lblTitle = "Top Ten Things"
This works because the label control has a default property, which is Caption. So the preceding line really
means this:
lblTitle.Caption = "Top Ten Things"
VB.NET does away with the concept of default properties and methods in most circumstances. So the first
form won't work in VB.NET. You must use the second form.
Many development shops have long frowned on using default methods and properties, because they make
code more difficult to read. The change in VB.NET gives another, even more compelling reason to do
away with default methods and properties. Even many professional shops use a related construct for data
access coding. It looks like this, where rsTitles is a recordset:
rsTitles("DatePublished").Value = sDate
This line actually means:
rsTitles.Fields _
("DatePublished").Value = _
sDate
In this case, the Fields collection is the default for the Recordset object. Based on the latest information, it
looks like the shorthand form (without the "Fields") will probably work in VB.NET.
10 Ways to Prepare
for VB.NET
2
Control Your Array Bounds
Today, when you declare an array in VB6, you actually get an extra element. That is, if you have this
line, then VB sets aside space for six array elements, starting with index zero and going to index five:
Dim sSubjects(5) As String
Old-time VBers commonly use only elements one through five (I'm guilty of this one). We're going to
have to adapt, I'm afraid. When you use this same line in VB.NET, you get an array of exactly five
Losing the top-most array element is my nominee for biggest breaker of old code. Converting old code
and getting all the index handling right will be a huge headache. Your action item is to stop using the
top array element on arrays. It might be tough to fix old code, but at least you can stop the damage on
code written from this point.
Since version 1, VB has considered controls on a form to be Public. You commonly see code in a
BAS module do something like this:
sTitle = frmArticleEntry.txtTitle.Text
This fails in VB.NET (see Figure 1). You can make it work by explicitly declaring the control
txtTitle on frmArticleEntry as Public Shared. But that's not the best solution.
Since VB4, many professional developers have recommended that all communication of data with a
form take place through custom property procedures. Any code outside the form reads data from or
writes data to the form through these procedures.
For example, suppose you have a form named frmOptions that uses five option buttons, named
optFormat(0) through optFormat(4), to allow the user to specify a report's format type. You need to
find the format type from outside the form (say from a BAS module). With old-fashioned VB syntax,
you would access the format type like this:
For nIndex = 0 To 4
If frmOptions.optFormat(nIndex) Then
nFormatNumber = nIndex
Exit For
End If
Next nIndex
Later, however, you might have to change either the control name or the number of option buttons. Or
you might have to switch to a combo box holding the format types because they increase in number
and start varying. In that case, code from outside the form, such as the preceding lines, would all have
Instead, add a property to the form at the outset to allow outside code to fetch the format type. You
could call the property FormatType. You place this code in the frmOptions code module:
Public Property Get FormatType() As _
Long
For nIndex = 0 To 4
If frmOptions.optFormat(nIndex) _
Then
FormatType = nIndex
Exit For
End If
Next nIndex
End Property
Now calling code from outside the form can get the format type with a single line:
nFormatNumber = frmOptions.FormatType
If you have to change the controls for the format type, adjust the property procedure inside frmOptions
to reflect the changes. For example, if you switch to a combo box, the property procedure looks like
this:
Public Property Get FormatType() As _
Long
FormatType = cboFormat.ListIndex
End Property
Calling code from outside frmOptions doesn't need to change at all. Ideally, the property procedures
would contain some error checking, which I have left out for simplicity. You also want Property Let
procedures for setting the controls from outside the form.
Doing this extra work now has some immediate benefits, such as easier switching of control types, and
will make the transition to .NET easier to boot.
10 Ways to Prepare
for VB.NET
4
Make All Parameters ByRef or ByVal Explicitly
Today, if you fail to make a parameter ByRef or ByVal explicitly, whether it becomes ByRef or
ByVal depends on what type of parameter it is. If it's an intrinsic type (integer, long, Boolean, string,
and so on), then it's ByRef by default. Otherwise, it's ByVal. This allows you to pass in parameters to
subroutines or functions (without making them ByRef) and have the underlying routine change them
and pass them back. This practice is usually considered sloppy, and now there's an even better reason
not to do it.
In VB.NET, all parameters are ByVal by default. You must declare a subroutine or function explicitly
as ByRef to get it to change a parameter. This change can lead to subtle, hard-to-find bugs if you
continue using the older convention. My recommendation: Make it a coding standard to make all
parameters explicitly ByVal or ByRef. This works now and later.