VBUC Migration Guide - Chapter 11

Upgrading String and File Operations

Chapter 11 describes how to upgrade Visual Basic 6.0 string and file operations to .NET, by either allowing the Upgrade Wizard to automatically upgrade the code or by replacing these operations with new Visual Basic .NET features. The second option offers a variety of possibilities, including VB.NET Streams, the VB.NET File System Object model, and the StringBuilder and Regular Expressions classes.

In general, how can I upgrade VB 6.0 code that performs string or file operations?

There are two approaches to this: to allow the upgrade wizard to automatically convert the code (the resulting code will have the same behavior as the original one with the help of the Visual Basic Compatibility library) or to replace these operations with new Visual Basic .NET features, which requires more work but avoids dependence on the compatibility library.

Which VB 6.0 string operations are automatically handled by the UpgradeWizard?

The most basic string operations involve concatenating strings and obtaining substrings. These are the simplest types of string manipulations, and they are also the most commonly used, and the upgrade wizard can be applied with no resulting issues The Visual Basic 6.0 string functions are upgraded to the equivalent functions in the Microsoft.VisualBasic namespace with the Importstatement.

A complete listing of the string operations that are included in the Microsoft.VisualBasic namespace can be found by viewing Help for this namespace.

Which VB 6.0 file operations are automatically handled by the Upgrade Wizard?

File input and output is supported through the Microsoft.VisualBasic library. Visual Basic 6.0 provides several I/O commands to manage the access to files and directories. These commands include: Open, Close, Reset, Get, Put, Print, Write, Input, LineInput, Lock, Unlock, and Width. In this case, the code resulting form the upgrade wizard compiles and executes with the same behavior as the original code, thanks to the support provided by the compatibility library. As previously stated, whenever the resources allow it, it is possible to rewrite the output using only core Microsoft .NET Framework functionality. The preferred method for working with files in Visual Basic .NET is to use streams. A stream is a more generic view of a sequence of bytes that allows reading, writing, and seeking. Streams can be associated with a file, a network connection, and memory, to name just a few examples. Visual Basic .NET even provides for reading and writing cryptographic streams. The Visual Basic .NET code generated by the upgrade wizard can be rewritten to use streams instead of text file functions.

Also, it is not unusual to find Visual Basic 6.0 applications that communicate with legacy systems through flat files. Visual Basic 6.0 provided useful tools for that type of interaction, including random, binary, and sequential file access mode. These can also be migrated by the upgrade wizard.

When is it recommended to replace instances of String in Visual Basic 6.0 code to use StringBuilders in Visual Basic .NET?

If you are not modifying your strings or if you are concatenating small numbers, you might leave your strings as they are. If, on the other hand, you are performing many manipulations (concatenating, changing case, replacing/inserting characters, and so on), you might be better off changing your String instances to StringBuilders. By following this approach, you would be assured that your application will not suffer performance degradation due to String manipulation.

In Visual Basic 6.0 and Microsoft Visual Studio .NET, strings are immutable. This means that whenever a string is declared and a value is assigned to it, the value that the string holds in memory never changes. The drawback is that you are using more memory than what you actually require to perform a simple task.

String variables can be used when the stored values will not be changed frequently during the program execution. In cases where a fixed quantity of strings is concatenated, the usage of a String data type is appropriate; the StringBuilder class, found in the System.Text Namespace, is the best choice because no new objects are created when the string value is altered. In operations such as appending text to a string, the StringBuilder will most likely outperform String concatenation because there is less overhead in terms of creating new objects in memory for each operation.

When is it recommended to replace String manipulation in Visual Basic 6.0 code with Regular Expressions in Visual Basic .NET?

The .NET implementation of regular expressions allows code to be written in a more efficient and maintainable manner than using string functions to perform intricate string manipulation.

Regular expressions allow a programmer to define patterns to be found in a general manner, which is great for several reasons. For starters, the amount of coding required to perform a specific task is greatly reduced because regular expressions allow a programmer to write in a few lines of code in what usually took many lines before.

Visual Studio .NET programming languages, including Visual Basic .NET, are able to fully exploit the power of regular expressions. To take advantage of the classes that provide access to the .NET Framework regular expression engine, you need to import the System.Text.RegularExpressions namespace into your code.

You should seriously consider using regular expressions if your upgraded code handles any type of complex input validation or complex string modification. For example, visualize a scenario in which you are upgrading a Visual Basic 6.0 password verification routine that has intricate requirements: the password must be at least eight characters long, must contain at least one digit, and should contain at least one special character. The non-regular expression code for that function may span many lines of code to parse the password chosen by a user. The available Visual Basic 6.0 procedures used to code this type of algorithm might leave no other choice but to write very hard to read code that might be unnecessarily complicated. By using the classes under the RegularExpression namespace, you could carry out the verification with one line of code if your regular expression has been well planned.

How can File I/O be improved in .NET?

The upgrade wizard is able to upgrade most of the file access methods using the Visual Basic 6.0 Compatibility library available in .NET. An alternative to upgrading file access is to use new Visual Basic .NET methods for file access. The preferred method in .NET is to use streams, manually rewriting the upgraded operations.

Streams are the recommended choice for reading and writing data from files in .NET for most new development projects. A stream is a generic view of a sequence of bytes that allows reading, writing, and seeking. Streams can be associated with a file or other sources (such as network connections) and have highly optimized methods that make them faster than the compatibility library methods for file access.

So how do streams work in .NET?

Reading from a stream is fairly straightforward. Instead of opening a file associated with a file handle, you create a StreamReaderobject to open the file. After the file is opened, the StreamReader method Read can be used to obtain the next character. The Peek property allow you to look ahead one character to determine whether the end of the file has been reached. After the file contents are read, the Close method closes the stream.

The StreamReader class’s Read, ReadBlock, ReadLine, and ReadToEnd methods are used to read the contents of a file. The StreamWriter class provides methods for writing to streams. The process is similar to reading. First, create a StreamWriter object, use the appropriate methods for writing to the stream, and close the stream when finished. You can use the StreamWriter class’s Write and WriteLine methods to write to the stream.

Please note that for Visual Basic 2005, the System.IO.File.ReadAll method provides functionality to open a file, read the contents into a string variable, and close the file using a single method. The analogous method for writing files is System.IO.File.WriteAll.

Traditional Visual Basic 6.0 sequential I/O has some characteristics in common with streaming. Streaming also has similarities to binary I/O. Sequential access with streaming functionality can be achieved using the FileStream and StreamWriter classes, however this has the limitation that there is no built-in capability to read delimited data. When processing delimited data with streams, it is the programmer’s responsibility to parse the data.

Sequential writing of a file can be achieved with the StreamWriter.WriteLine method, which can write a string followed by a line terminator. Binary writing of a file, which allows any amount of data to be placed in the file during a writing operation, can be achieved with StreamWriter.Write; this method can write different data types at the current position.

Stream classes are available in the System.IO namespace. Additionally, this namespace includes classes for other file operations, such as directory handling.

How does the file access through the File System Object model works in Visual Basic .NET?

Another option available in Visual Basic .NET for accessing and manipulating files is to use the File System Object (FSO) model. This model provides objects and methods for working with files and folders (directories) in an object-oriented way. This model is provided through the Visual Basic Scripting type library (Scrrun.dll).

Using the FSO model, you can create or delete files and folders, obtain information about files and folders (such as path information), and perform other manipulations such as copying and moving files and folders. The model also provides a class, TextStream, which allows you to create objects for reading and writing text files. Note that binary file reading and writing is not supported by the FSO model.

Using methods and properties of the FileSystemObject, you can also perform activities such as creating temporary file and folder names, check for the existence of a particular file or folder, delete a file or folder, and much more.

Talk To An Engineer