A Brief Look at How the Visual Basic .NET Language Works

A Brief Look at How the Visual Basic .NET Language Works

If you're like me, you want to see code. Now! For a short preview of how the Visual Basic language has evolved, take a look at Figure 1-3. If you are already a Visual Basic programmer, you will immediately notice a difference in the Visual Basic .NET syntax. You use a Dim statement to create an integer variable (iInteger) as you normally would, but you can now initialize the variable in the same statement, as you can in advanced languages. Very clean and efficient.

You can now create and initialize a variable in one statement in Visual Basic .NET.

Because iInteger is an object in Visual Basic .NET, it has its own properties and methods. Adding a period (or scope resolution modifier) after the variable name displays the properties and methods that the object owns with the IntelliSense feature of the code editor.

note

You are probably familiar with the IntelliSense feature in previous versions of the Visual Basic code editor. The Visual Basic .NET source code editor offers automatic completion of various keywords as you type. IntelliSense provides an array of options that make language references easily accessible. When coding, you do not need to leave the text editor to perform searches on language elements. You can keep your context, find the information you need, insert language elements directly into your code, and even have IntelliSense complete your typing for you. Classic Visual Basic used type libraries for IntelliSense. If something was late bound, or a type library wasn't available, you didn't have any IntelliSense help. With .NET, all the data types are available all the time when you use early binding.

In this example, we are going to use the ToString method of the iInteger object shown in Figure 1-3 to make a string copy of the contents and assign it to the string object named sString. When the operation is complete, sString will contain the value 5. The contents of iInteger remain unchanged. The iInteger variable object has a Format method that you can use to change the look of the contents to currency, percent, and so forth for display purposes.

Because we are talking about integers, another change to be aware of is that the underlying data representation of integers has been upgraded in Visual Basic .NET. In Visual Basic 6, the Integer keyword is defined as a 16-bit integer, and the Long keyword is defined as a 32-bit integer. To make Visual Basic .NET more compliant with common C syntax and adhere to the general standard in all .NET languages, Integer is now a 32-bit integer and a Long is a 64-bit integer. If you still need a 16-bit integer, Visual Basic .NET has added the new Short data type. However, because a Short is 16 bits and processors have 32-bit registers, using a Short is actually slower than using a 32-bit integer. In practice, you will rarely use a Short. But you can see that with .NET, it's important to understand data types.

So an Integer is now a Short, and a Long is now an Integer. While most Visual Basic programmers wrote tight code and usually knew exactly what data type was being used, some were a bit cavalier and used buckets of variants. You now have to know all about the data type you are using. And, by the way, Visual Basic .NET no longer supports variants. The .NET analog is an Object.

To give you a flavor of some other general changes, Visual Basic .NET no longer uses fixed-length strings, control arrays, or COM objects, just to name a few differences. Other changes include variable scope, error handling, and the way you call functions. The Set command, used for setting a reference to an object, is gone because now everything is an object, making that keyword redundant. All of the graphical user interface controls, such as the command button, text box, and option button, have been reengineered. As you can see, the changes are fairly wide ranging, making Visual Basic .NET more of a new language than an incremental upgrade from Visual Basic 6. But again, most old-world programming concepts apply, so what you already know will be used as a foundation when stepping up to Visual Basic .NET.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net