Variables are essential to computer programming, just as they are in many other aspects of life. Any container with a label is the real-world equivalent of a variable. And you're surrounded by named containers — they're fundamental to data processing.
Text variables (strings) are pretty simple. The string is the only fundamental text data type. In contrast, numeric variables come in several fundamental types. The reason for these different numeric data types is to enable you to speed up your applications with some of them and achieve greater precision with others.
Previous versions of Visual Basic (VB) had a default variable type. By default, unless you specifically defined them as something else with Dim, VB made all variables the variant type. This was convenient because you didn't have to worry about saying: ThisVariable holds strings, but ThisOtherVariable holds only whole numbers (no fractions). Instead, you let VB decide which type to use based on the value you assigned to it or the context in which it was used. For example, if you assigned what was a numeric variable type to a TextBox, the variable type was automatically changed from numeric to string (because a TextBox can display only a string type).
Fatal flaws
The variant type, efficient though it often was, had two fatal flaws from the perspective of those who designed VB.NET. First, in some cases, VB had a hard time figuring out which type the variant should change to — resulting in an error. Second, the other languages in the .NET universe do not use variants — and the .NET philosophy requires conformity between its various languages (at least on the fundamental issues, such as variable typing). Therefore, the variant variable is no longer part of the VB language. It has been banished in VB.NET.
Following is an example that shows how variants achieved their chameleon changes:
A = 12
B = 12.4
When it assigns the 12 to A, VB figures that 12 can be an integer type, but when it assigns 12.4 to B, VB knows that this number has to be changed to the floating-point (has a decimal point) type because it is a fraction. So, VB types the variables for you. It can even convert some kinds of data:
A = "12"
B = 14
B = B + A
MsgBox (B)
In this example, you get the correct mathematical answer of 26 because when you assigned 14 to B, it automatically became an integer variable type, and then you assigned a string to it, which converted the string into an integer. However, don't take this too far. It's best not to mix types if you can avoid it.
The interpreting that VB must do when it works with variants was said to slow program execution down some. In any case, variants are no longer available. But VB.NET can change an integer type into a string type, for example, so the integer can be properly displayed in a TextBox.
Important numeric types
The simplest numeric variable type is Boolean. It can hold only two states: True and False (it defaults to False). Use this when you want a toggle variable (something that switches off and on like a light switch). To create a Boolean variable, use the following code:
Dim MyToggle As Boolean
Another simple data type is the Integer and its larger sister, the Long type. Before VB.NET, the Integer data type was 16 bits large and the Long data type was 32 bits large. Now these types are twice as big as they used to be: Integer is 32 bits large and Long is 64 bits large (and Long is an Integer too — no fraction, no decimal point). If your program needs to use a 16-bit integer, use the new type Short.
So if you're translating pre-.NET VB code, you need to change any As Integer or Cint commands to As Short and Cshort, respectively. Similarly, As Long and CLng now must be changed to As Integer and Cint.
You'd be surprised at how often the only thing you need is an integer in programming that involves math. In most programming, the Integer is the most common numeric data type. (No fractions are allowed with an Integer.) If your non-fractional number is larger or smaller than an integer can hold, make it a Long data type.
Dim MyLittleNumber As Integer
Dim MyBigNumber As Long
The other major numeric type is called floating point. It has similar small and large versions called Single and Double, respectively. Use it when your program requires the precision of using fractions:
Dim MyFraction As Single, MyBiggerNumber As Double
VB.NET also has a new Char type, which is an unsigned 16-bit type that is used to store Unicode characters. The new Decimal type is a 96-bit signed integer scaled by a variable power of 10.
dummies
Source:http://www.dummies.com/how-to/content/understanding-data-types-in-visual-basic-net.html
No comments:
Post a Comment