Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Converting VB6 to VB.NET, Part II

Moving along

Last month (Vol. 2, issue 9), I gave an executive overview of the conversion process, and started looking at converting general VB6 code to VB.NET. This month I will finish general conversions, including DLLs, then start on database conversions. Next month, in the final segment, I will cover converting ASP.NET Web pages, and look at converting to VB.NET 2005 and C#.

Nothing Is Perfect
After the conversion wizard is done, the upgrade report will probably contain a list of many issues. A lot of these issues are minor things that nothing can be done about, but which in most cases will have little impact on the final program. For instance, if you use the ZOrder property in VB6, the wizard will convert it, but add a message that the behavior has changed. All of the issues flagged by the wizard will have a link to a URL explaining the problem. In the case of ZOrder, the difference between the VB6 behavior and the VB.NET behavior is in that in VB6 the form is brought to the front of the application; in VB.NET the application is also brought to the front of all other applications running on the computer. In most cases this is closer to the desired result than in the VB6 behavior.

The Dir command is another example. In VB6, using the Dir command to list the files in a directory always listed the "." and ".." first in the returned file list, the DIR command would always list them first. In VB.NET, "." and ".." are never listed in the Dir results. These were not always listed in VB6 - for instance, they are not part of the root "\" directory - so correct VB6 code checks for these directories. Such code will perform correctly in VB.NET. However, some VB6 programs just ignore the first two files returned by Dir, assuming they are "." and ".."; this code will always miss the first two files in VB.NET, and in some cases in VB6. This is another example of well-written code converting well.

Variables and Attributes
Most VB6 variable types have equivalent types in VB.NET, but there are a couple of twists. An Integer in VB6 is a 16-bit integer; in VB.NET, it is a 32-bit integer. The 16-bit equivalent in .NET is a Short, which is what the wizard will convert it to. Sometimes this is correct, but in cases where the 16-bit values reflect a VB6 limitation that does not exist in VB.NET, it makes more sense to upgrade the variable to the VB.NET 32 bit Integer. The preferred way to do this is to change the VB6 Integer to a VB6 Long (32-bit integer) before the conversion. A complete list of equivalent data types not only for VB6 and VB.NET, but also IDL (COM), the .NET framework, and MSIL (.NET assembly language) can be found at (Note From the Author: This URL will only work if Visual Studio is installed on your computer) ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dv_vstechart/html/ vbtchTroubleshootingNETInteroperability.htm.

The VB6 Date type is a simple 8-byte variable type; VB.NET uses the .NET DateTime class for the same functionality. In places where the old 8-byte VB6 type is needed, use the DateTime.Date functions FromOADate(x) or ToOADate(x). The wizard will make use of these functions when converting; for example, MyDate = 100 will be upgraded to MyDate = DateTime. Date.FromOADate(100). I have read that the VB6 Date type is one thing that just will not convert to .NET; these functions have done the few things I have required of them, so I suspect that those who say Date types are hard to upgrade are just not aware of these functions. There is so much in .NET that these mistakes are easy to make. When I first converted the MSChart control, I thought the Axis property had not converted (see Part 1 of this series), and spent a lot of time looking for a replacement control. This was not a total waste of time, as I switched to a pure .NET control, which I would have done eventually; but this did delay having working code for the first conversion pass. The lesson to be learned from this is that if you do not think a function exists in .NET, keep looking, you just might find it.

Another type that changes is the VB6 Currency type, which converts to a VB.NET Decimal type. I have heard this can cause issues, but the few tests I have run show them behaving the same.

Attributes are new to .NET. In VB6, a procedure had three things: visibility (public/private), scope (local/global), and type (Integer, etc). .NET adds an arbitrary number of optional attributes; these can be used by the compiler or by the program at runtime (or both). The COMVisible attribute is an example of an attribute used by the compiler at runtime. When added to a class, property, or function, it tells the compiler to create code that will allow that member to be accessed by legacy COM applications. You can create your own custom attributes (it is similar to creating a class), and check at runtime to see if it has been added to a function. Why and how you would do this is outside the scope of this article. The .NET framework provides a number of useful attributes, a few of which will be used later in this article.

DLLs can be a lot of things. Many are just libraries of simple functions; the Windows APIs are a good example of this type of DLL. Others can be much more complex; ActiveX controls, Excel add-ins, and OLE servers are some examples of more complex DLLs. ActiveX controls will be the only complex DLLs covered in this article.

Simple DLLs are easy to use in .NET. Just add one or more declaration statements to your program, and you can call the functions in the library just like they are subroutines in your own program. The upgrade wizard will do a good job of converting these Declare statements from VB6 to VB.NET. In addition to upgrading the variable types discussed above, there are two new .NET attributes, packing and pinning, that may need to be added to some of the parameters being passed.

To understand packing, let's look at an example. Consider a VB6 user-defined type that contains two Integers; after the conversion, we want them to still be 16-bit integers. As mentioned above, the first thing we need to do is change the type from Integer to Short. Now let's look at packing.

Suppose we declare an instance of this type in VB6. The compiler will put it in memory somewhere; to simplify this discussion, we assume the instance is placed at the beginning of memory, at address 0000. The two bytes of the first Integer will be placed at addresses 0000 and 0001. The second Integer will be at addresses 0002 and 0003. This minimizes use of memory, and has no performance penalty on the 16-bit processors (8086/80286) that Windows and VB were originally designed to run on. Modern 32-bit processors can access the first integer at full speed; they load the 32 bits starting at 0000, and just mask out the second 16 bits. Accessing the second Integer is more complex. When the processor loads the first 32 bits, the 16 bits it needs are in the wrong position, so there is a small performance penalty when the processor shuffles the bits to where they need to be. Obviously, loading variables is a very common thing for a processor to do, so this small performance penalty becomes a big factor when you consider all the variables used in a program. .NET prevents this slowdown by putting all variables on 32-bit boundaries (this has been a compile option in C++ for a while). Under .NET, in this instance of the type, the first Integer will be at addresses 0000 and 0001, just like before, but the second integer will be at 0004 and 0005; addresses 0002 and 0003 will not be used. This wastes memory but the program will run faster.

The problem with this is that if you pass the user type to a DLL, that DLL will almost certainly expect the type to be "packed," like in VB6. .NET handles this with the "packed" attribute. Adding this attribute causes the VB.NET compiler to pack the type like in VB6, saving memory and maintaining compatibility at the expense of speed. The following example shows how this is done.

Userdef  MyType
	IntOne as Integer
	IntTwo as Integer
End Type

In cases where user-defined types, arrays, or fixed-length strings are passed as parameters to the DLL, the upgrade wizard will add warnings that marshaling attributes may need to be added. Even though the wizard places the warning at the Declare statement, the attribute (as in the above example) needs to be added where the parameter type itself is defined.

Garbage collection can bring up another issue with DLLs - pinning. In .NET, the memory manager/ garbage collector can and will move variables around in memory while a program is running. For instance, if a program creates five 100-byte arrays, they will probably be in a contiguous 500-byte block of memory. If the array taking up the first 100 bytes is released and gets garbage collected, the other 400 bytes will be moved down 100 bytes so there are no unused "holes" in the memory block. If a program passes a reference to a block of memory to a DLL, and that memory is later moved, the DLL's reference will no longer be valid, causing a major bug. To prevent this, a variable in C# can be "pinned" with the fixed command, which tells the garbage collector not to move the referenced memory; VB.NET does not have an equivalent to the "fixed" command, so I am unsure of how this is handled in VB.NET.

DLLs, DAO, RDO, ADO, and AD.NET; the History of VB DBs
In the early versions of VB, there were no database controls, and databases were accessed by vendor-specific DLLs, but VB's power and ease of creating forms still made it a favorite among database programmers. One thing that made it so easy to create forms in VB was VBXs, the forerunners of ActiveX controls. In VB3, Microsoft added DAO (Data Access Objects), allowing easy access to ODBC databases, and a good thing was started. RDO (Remote Data Objects) came next. Where DAO focused on connecting to small Access-type databases, RDO targeted a different market, large databases such as MS SQL and Oracle, so to a large extent RDO complemented rather than replaced DAO. ADO was to be the technology to combine the two.

Upgrading from the earlier DAO or RDO architectures will be difficult. The Data control that was typically used by DAO and RDO programmers is not supported by .NET, and the upgrade wizard just marks each place the data control is used with a comment that it is no longer supported. Following the comment to the documentation just leads to generic information about upgrading with no further help.

I have had some success creating an ActiveX control in VB6, adding the data control to the new ActiveX control, then adding functions and subroutines to make the data control accessible to its container in the VB client program. The accessors looked like

Public Sub SetDataBaseName(Name As String)
    Data.DatabaseName = Name
End Sub

Public Sub SetRecordSource(Source As String)
    Data.RecordSource = Source
End Sub

Public Sub Refresh()
End Sub

Public Sub RecordSetAddNew()
End Sub

Public Sub RecordSetFields(Field As String, Name As String)
    Data.Recordset.Fields(Field) = Name
End Sub

In the client VB program,


was replaced with

Data1.Recordset.Fields("[Territory Num]") = RTrim(LTrim(txtTerritoryNum.Text))

which was replaced with

Data1.RecordSetFields "[Territory Num], RTrim(LTrim(txtTerritoryNum.Text)),

and so on. This worked well for strings, other RecordSetFields needed for other types of fields, or RecordSetField declared using Variant as the value type, and detecting the proper type by using the VarType function. This works all right if you just want to use the Data control by itself, but the whole point of using the Data control is to bind it to controls such as the DBGrid. To make that work correctly would require making the custom control a true VB data source. This is beyond the scope of this article, and might not work once converted to .NET. For anyone wishing to try this (and it might work), search for "Creating a Data Source" in the MSDN help files for more information.

I used a similar trick to convert a VB6 form to .NET. The form was designed to allow adding new records to an existing database. It could be considered as having three parts, a Data control for accessing the database, a DBGrid (from VB5) for displaying the records in the database, and a set of TextBoxes for inputting the values for the new record. I took the custom data control I created above and added a DBGrid (from VB5) to it. I included some resizing logic so that the grid always covered the entire control area. Next, I set the data source for grid to the data control and built the ActiveX control. I removed both the Data control and the DBGrid from the VB program I was converting, and replaced them with my new custom control containing both. It upgraded to VB.NET and ran fine. Again, being creative in using custom ActiveX controls can make otherwise impossible conversions fairly simple.

ADO.NET is one of the biggest changes yet in VB database connectivity. In ADO, programs were meant to always be connected to the database (note that ADO could be used in a disconnected mode, but most VB6 programs used the normal connected mode). In some ways, this simplified database programming a bit because changes made to records and tables in the program were automatically updated in the database. It also causes a scalability issue because one of the things most database vendors charge for is the number of allowed simultaneous connections. Since ADO connections tend to exist for the entire time the VB program is running, a license is tied up as well. Sometimes this does not matter, but if the program has a lot of users, or even worse, is accessed over the Internet, tying up a license for an extended time like this can get very expensive. Having a large number of open connections to the database can slow overall performance as well.

With ADO.NET, connections are opened to read, write, or update the database, and closed while the program or user works with the data. This causes a little more work for the programmer, but generates a slightly faster, and much more scalable program.

One half-step way to get around a complete rewrite is to switch to .NET, but continue to use ADO instead of ADO.NET. This can be done because ADO is implemented as a COM object in msado15.dll. Like all COM objects .NET needs a wrapper to access the ADO DLL. The .NET framework includes a wrapper for this DLL called adodb.dll, but you can also create your own from any version of ADO by using the tlbimport.exe utility that was briefly mentioned last month. Another option is, as always, that if a form can be separated from the rest of the application, it can be converted into an ActiveX control and added to a .NET form. Another advantage to using one of these techniques is that ADO has a few functions that are not available in ADO.NET, most notably server-side cursors and ADO extensions (ADOX).

I have already mentioned that moving from the connected ADO paradigm to the disconnected ADO.NET paradigm will give a boost in performance, and a big boost in scalability. There are other reasons to switch to ADO.NET, too. ADO uses recordsets that resemble tables; ADO.NET uses datasets that resemble databases. To access more than one table in ADO, the query forming the recordset had to do a join across the tables; in ADO.NET, the dataset can contain all the tables, as well as DataReleation objects, which resemble foreign keys in a real database. This can make complex data manipulations much easier. The old client-side Cursor functionality can still be found in the DataReader object. Another big difference is that while ADO can only load data into record sets, ADO.NET can also load data into many "normal" programming structures such as arrays and lists. Where ADO tables were accessed by forward only cursors, ADO.NET tables have indexers, and can be accessed like any other collection. Also, most of the .NET controls, even text boxes, allow data binding not to just text, but to other properties such as color (Note From the Author: This URL will only work if Visual Studio is installed on your computer) (see ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dnadvnet/html/vbnet02122002.htm).

Finally, ADO.NET is based on XML, which makes moving data between objects and programs easier and more efficient; using XML for this instead of ADO's COM calls also means that data can pass through firewalls without configuration changes.

Next Up
That completes our coverage of converting general and database code. Next month, in the final installment, we will cover ASP to ASP.NET conversion, and converting to VB.NET 2005 and C#; and conclude with some thoughts on when it makes sense to convert.

More Stories By Dennis Hayes

Dennis Hayes is a programmer at Georgia Tech in Atlanta Georgia where he writes software for the Adult Cognition Lab in the Psychology Department. He has been involved with the Mono project for over six years, and has been writing the Monkey Business column for over five years.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
"Digital transformation - what we knew about it in the past has been redefined. Automation is going to play such a huge role in that because the culture, the technology, and the business operations are being shifted now," stated Brian Boeggeman, VP of Alliances & Partnerships at Ayehu, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Synametrics Technologies will exhibit at SYS-CON's 22nd International Cloud Expo®, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Synametrics Technologies is a privately held company based in Plainsboro, New Jersey that has been providing solutions for the developer community since 1997. Based on the success of its initial product offerings such as WinSQL, Xeams, SynaMan and Syncrify, Synametrics continues to create and hone inn...
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
SYS-CON Events announced today that Evatronix will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...