Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Understanding Language and Syntax Changes

To migrate Visual Basic 6.0 applications to Visual Basic .NET, it is essential to understand the differences between Visual Basic 6.0 and Visual Basic .NET, discussed in the following subsection.

Variant

Visual Basic 6.0 languages support a data type called as variant. The variant is a flexible data type; it can accommodate all other types. If a variable is not declared with any type, it is regarded as a variant type variable. Visual Basic bases the type of the variant variable on its contents. For example, if the variant variable contains a string, Visual Basic handles the variable as a string type variable. If it contains an integer, it is handled as an integer type variable. Consider that there are multiple variables declared on one line as shown in the following code snippet.

Dim Counter1, Counter2, Counter3 As Integer 

Here only the variable Counter3 is declared to be of type Integer. The other two variables Counter1 and Counter2 are of type variant.

In Visual Basic .NET, the variant data type is not supported. All the variables of variant data type get converted to object type in the Visual Basic .NET code. Object is a generic class in the Microsoft .NET Framework class library from which all other classes are derived.

Currency

In Visual Basic 6.0, the currency data type is used for performing financial calculations. currency types are not supported in Visual Basic .NET. During the upgrade process, the upgrade wizard will change all currency data types to decimal type. However, to smooth the migration process all currency variables in Visual Basic 6.0 applications can be converted to double type before upgrading to Visual Basic .NET, as the following code snippet demonstrates:

Dim Sal_Amount As Currency 

In this case, it is recommended to change the above variable declaration to double type:

Dim Sal_Amount As Double 

If the Visual Basic 6.0 application uses the FormatCurrency function, the application is upgraded and works well in Visual Basic .NET with the help of the Microsoft.VisualBasic.Compatibility class.

Date

In Visual Basic 6.0 a date variable is stored internally in double format and all the manipulations on double format are applicable to the date type. Hence double type of data has been frequently used for date manipulations. In Visual Basic .NET, double type variables cannot be used for date manipulation.

Fixed-Length Strings

Fixed-length strings are not supported in Visual Basic .NET. In Visual Basic .NET fixed-length strings cause a problem when used in user-defined types such as structures. So it is recommended to use strings instead of fixed-length strings.

Type

User-defined types can be defined in a Visual Basic application as shown in this code snippet:

Type Customer 
   Customer_ID As Integer 
   Customer_Name As String 
End Type 

In Visual Basic .NET, the Type keyword is not supported. Structure is the keyword for declaring user-defined types in Visual Basic .NET as follows:

Structure Customer 
   Customer_ID As Integer 
   Customer_Name As String 
End Structure 

Visual Basic .NET also supports access modifiers for the members of the structure. Access modifiers like public, private, and friend can be used with the members of a structure. Many of the Windows API function calls require the data types to be passed as parameters. Thus, when these function declarations are upgraded to Visual Basic .NET, they will get converted into equivalent structures.

In Visual Basic .NET, user-defined types are called structures. Structures are in many ways similar to a class. Structures can have constructors, methods, properties, and so on. Structures, however, do not support inheritance. Also, structure variables are value type variables, and they do not use heap memory. A structure inherits from the System.ValueType object. Classes, on the other hand, are reference type variables. Instances of classes are normally allocated on heap memory. Classes support inheritance.

Even though user-defined types such as classes and structures are supported in Visual Basic .NET, fixed-length strings in user-defined types are not supported. In such cases, data marshalling techniques must be used in Visual Basic .NET.

Empty

Variants are initialized to Empty, which automatically gets converted to zero when used in a numeric expression, or to an empty string when used in a string expression. Empty is not supported in Visual Basic .NET. Object type variables are initialized to Nothing in Visual Basic .NET.

Null and Null Propagation

Null values indicate that a variable contains no valid data. If any part of an expression evaluates to null, the entire expression evaluates to a null. Null propagation is not supported in Visual Basic .NET. Normally null values are obtained when database operations are involved. Because null propagation is not supported, Visual Basic .NET offers a database-programming model, which test fields explicitly for null before retrieving their values. Variants containing null are marshalled into the CLR as objects of type DBNull.

Def<Type>

Def<Type> statements are used to set the default data type for variables in Visual Basic 6.0. The following code snippet shows an example of the DefInt type:

DefInt A-D 

Private Sub Form_Click() 
   AVariable = 1 
   BVariable = "Error"'this will give an error 
   MsgBox (AVariable) 
   MsgBox (BVariable) 
End Sub 

The DefType statements are used at the module level. In the preceding code snippet, all variables that begin with A, B, C, and D are defined to be of type integer. The sample code gives an execution error when the string "Error" is assigned to the variable BVariable, which is of integer type variable. This type of data type definition is not supported in Visual Basic .NET.

Scope of Local Variables

In Visual Basic 6.0, if a variable is declared in a block, it is also visible outside the scope of the block, and the Visual Basic 6.0 code can use the value inside the variable. So the following code is valid in Visual Basic 6.0:

Do 
   Dim iLoopCounter As Long 
     iLoopCounter = iLoopCounter + 1 
Loop Until iLoopCounter > 20 
MsgBox(iLoopCounter) 

This type of code is not supported in Visual Basic .NET. If a variable needs to be used outside a block in Visual Basic .NET, it needs to be declared outside of the block. If the preceding code is written in Visual Basic .NET, it will give a compilation error that the variable iLoopCounter has not been declared.

Object Finalization

There is a very huge difference in the way objects are cleared from memory between Visual Basic 6.0 and Visual Basic .NET. In Visual Basic 6.0, the COM reference-counting mechanism is used to free up the object instances. When objects are not in cycles, reference counting will immediately detect when an object is no longer being used and will run its termination code.

In Visual Basic .NET, a garbage collector does the job of cleaning up the unreferenced objects. The garbage collector is a feature of the Microsoft .NET Framework, and it is applicable for all the languages supported by the Microsoft .NET Framework.

The developer does not know when the garbage collector will collect the unreferenced objects. This is known as undeterministic garbage collection. The garbage collector also compacts the memory that is in use. The .NET Framework provides a class called GC that contains a method Collect. Developers can directly call this method to force garbage collection. However, calling the Collect method too often can cause performance issues, because the garbage collector suspends all executing threads before performing garbage collection. Hence it is better to let the garbage collector determine when to perform the garbage collection.

Arrays

In Visual Basic 6.0, the Option Base statement is used to determine the lower bound if a range is not specified in the declaration. We can specify arrays with any lower bounds. This is not supported in Visual Basic .NET. All arrays in Visual Basic .NET have to start off with a base of 0 to make the language interoperable with the other languages supported in the Microsoft .NET Framework. In fact, the Option Base statement is not supported in Visual Basic .NET.

ReDim

In Visual Basic 6.0, ReDim is normally used to redimension an array that has been declared earlier. If the array has not been declared earlier, it is declared with the ReDim statement. In Visual Basic .NET, the ReDim statement can be used only if the array has been declared earlier.

Error Handling

In Visual Basic 6.0, error handling is done with the help of On Error statements. Normally this statement is placed at the beginning of a block of code, and it handles any errors that occur in that block of code. This is not a structured way of handling errors.

Visual Basic .NET has vastly improved the error-handling techniques. In Visual Basic .NET, conditions in which code fails are known as exceptions. A block, which can throw an exception, is enclosed within a try/catch block. If the block throws an exception, it is caught by the code in the catch block. The catch block can handle the exception on its own, or it can rethrow the exception to a higher exception-handling code.

Function Return Values

To return a value from a function, in Visual Basic 6.0, the function name is set to the value that is to be returned. In Visual Basic .NET, the desired value is returned by the Return statement.

Assignment

In Visual Basic 6.0, there are two forms of assignment: Let assignment (the default) and Set assignment. In Visual Basic .NET, there is only one form of assignment. x = y means to assign the value of variable 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, a reference assignment is performed.

Calling Procedures

In Visual Basic 6.0, two forms of procedure calls are supported, one using the Call statement, which requires parentheses around the list of arguments, and one without the Call statement, which does not require parentheses around the argument list. Visual Basic .NET makes it mandatory to have parentheses in procedure calls.

Static Procedures

In Visual Basic 6.0, procedures can be declared with the Static keyword, which indicates that the procedure's local variables are preserved between calls. In Visual Basic .NET, the Static keyword is not supported in the procedure, and all static local variables need to be explicitly declared with the Static statement.

Parameter Passing

In Visual Basic 6.0 the developer can define the parameter as either ByRef (by reference) or ByVal (by value). If the developer does not specify it, the parameter passing defaults to ByRef.

This has changed to ByVal in Visual Basic .NET: All parameters are ByVal by default. So the developer has to declare a subroutine or function explicitly as ByRef to get it to change a parameter.

Optional Parameters

In Visual Basic 6.0, optional Variant parameters with no default values are initialized to a special error code that can be detected by using the IsMissing function. Visual Basic .NET requires that a default value be specified for all optional parameters.

ParamArray Parameters

In Visual Basic 6.0, when variables are passed to a ParamArray argument, they can be modified by the called function. However when variables are passed to a ParamArray argument in Visual Basic .NET, they cannot be modified by the called function. ByRef ParamArray elements are not supported.

As Any

In Visual Basic 6.0, a parameter of a native API can be declared As Any, in which case a call to the native API could pass in any data type. This was supported to enable calling APIs whose parameters supported two or more data types.

In Visual Basic .NET, overloaded Declare statements have to be defined, and it allows a native API to be called with two or more data types.

Implements

In Visual Basic 6.0, the Implements statement is used to specify an interface or class that will be implemented in the class module. The Implements statement in Visual Basic .NET is different than in Visual Basic 6.0 with respect to the following points:

  1. In Visual Basic 6.0, the Implements statement is used to indicate that the current class is implementing some other class. In Visual Basic .NET, the Implements keyword can be used only for interfaces.

  2. Every method that implements an interface method requires an Implements keyword at the end of the method declaration statement. This will specify what interface method it implements.

Property Access

In Visual Basic 6.0, the Get, Let, and Set property functions for a specific property can be declared with different levels of accessibility. For example, the property Get function can be Public, whereas the property Let is Friend. In Visual Basic .NET, the Get and Set functions for a property must both have the same level of accessibility.

Default Properties

In Visual Basic 6.0, any member can be marked as a default for the class. All built-in controls of Visual Basic 6.0 have some default properties. In Visual Basic .NET, only those properties that take parameters can be marked as default. It is common for those properties with parameters to be indexers into a collection.

Enumerations

In Visual Basic 6.0, enumeration constants can be referenced without qualification. In Visual Basic .NET, enumeration constants can be referenced without qualification only if an Import for the enumeration is added at the file or project level.

While Statement

In Visual Basic 6.0, While statements can end with a Wend statement. However, in Visual Basic .NET, to be consistent with other block structures, the terminating statement for While is End While.

On..GoTo and On..GoSub

In Visual Basic 6.0, statements such as On <Expression> GoTo and On <Expression> GoSub are supported. Because these are unstructured ways of coding, they are not supported in Visual Basic .NET.

GoSub Return

In Visual Basic 6.0, the GoSub Return statement is used to branch to a particular subroutine and to return from a subroutine within a procedure. GoSub Return is not a structured way of coding and is not supported in Visual Basic .NET.

LSet Statement

In Visual Basic 6.0, the LSet statement is used to left-align a string within a string variable. LSet pads a string with spaces to make it a specified length or copies a variable of one user-defined type to a variable of a different user-defined type. This is not supported in Visual Basic .NET.

VarPtr, StrPtr, and ObjPtr

In Visual Basic 6.0, VarPtr, StrPtr, and ObjPtr return the addresses of variables as integers, which can then be passed to API functions that take addresses, such as RtlCopyMemory. VarPtr returns the address of a variable, StrPtr returns the address of a string, and ObjPtr returns the address of an object. These are not supported in Visual Basic .NET.

File Handling

File input and output is part of the Visual Basic 6.0 language with statements such Open, Close, Input, and Output. In Visual Basic .NET, all file input and output operations are removed from the language and the functionality has been shifted into the Microsoft .NET Framework class library. Specifically, the classes in the namespace System.IO are used for file-handling purposes in the Microsoft .NET Framework.

Resource Files

Visual Basic 6.0 supports one .res file per project. Visual Basic .NET has rich support for resources. Forms can be bound to retrieve resources automatically from the new .resX-formatted resource files. Any CLR class can be stored in a .resX file.

Visual Basic Forms

Visual Basic 6.0 has its own forms package for creating graphical Windows applications. Windows Forms is a new forms package for Visual Basic .NET. It resides in the System.windows.Forms namespace of the Microsoft .NET class library. Because Windows Forms is built from the ground up to target the CLR, it can take advantage of all of its features.

Caption Property

In Visual Basic 6.0, the caption property is used to display the text in the title bar of the form. It is also used to display the text on the controls such as Label, Menu bar, and so on. The caption property has been changed to the text property in Visual Basic .NET.

Fonts

In Visual Basic 6.0, Windows Forms can use any Windows fonts. However, Visual Basic .NET can use only true type or open type fonts.

Control Arrays

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 grow to as many elements as your system resources and memory permit. Elements of the same control array have their own property settings.

In Visual Basic .NET, the Windows Form architecture natively handles many of the scenarios for which 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.

WebClasses

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 provide content to the browser and expose events.

Web Forms is a Microsoft .NET Framework feature that can be used to create a browser-based user interface for Web applications. Visual Basic .NET has a what you see is what you get (WYSIWYG) designer for graphical Web Form creation using controls from the Toolbox. This gives Web user-interface development the same feel as Windows development. Also, when the project is built, the Internet Information Services (IIS) server does not have to be stopped and restarted to deploy the changes in the application.

ActiveX Documents

ActiveX documents are Visual Basic 6.0 applications that are packaged so that they can appear within Internet browser windows. They can also be used in other containers such as Microsoft Office Binder. They offer built-in viewport scrolling, hyperlinks, and menu negotiation. ActiveX documents are not supported in Visual Basic .NET.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020