Joke Collection Website - Mood Talk - VB’s more commonly used variables

VB’s more commonly used variables

In VB, the following variable types

1, numeric variable (numeric)

2, string variable (string)

3, date variable (date)

4, object variable (object)

5, variant variable (variant)

These vb Among the variable types, the most important ones are the first two, numeric variables and string variables. The meaning is very simple, numeric types can be used to store numbers, and string types can store text.

The following is Introduce these types of variables in detail.

1. Numeric type

There are many types of numeric variables. In our VB, there are 3 numeric data types

< p>1;Integer type

2;Floating point type

3;Currency type

Among them, integer data can also be divided into integer and long integer (long). Floating point types can also be divided into single precision types (single) and double precision types (double)

The purpose of classifying digital variables in such detail is actually to improve the running efficiency of the program. If in the program Extensive use of double precision types will directly affect the running speed of the program, but its accuracy is improved. Earlier we knew how to declare variables, so that we can declare different types of variables!

dim z_ge as integer

dim z_D as long

.....

Wait, etc., they are all declared in this way, but it should be noted that , the dim statement can declare multiple variables in one statement, but you must pay attention to the format of the variable declaration! Otherwise, you will not get the correct result!

For example

dim z_ge, z_zf as integer

The original intention is to declare two variables in the same format, but this is wrong!

We can use the typename() function to verify the above variable type

After declaring the variable, we check like this

debug.print "z_ge is " & typename(z_ge)

After running, in the "Immediate" window, you can see to the result

z_ge is empty

Only the last variable is declared as integer. You can declare a few more and test a few times to find out!

< p>The correct declaration method is

dim z_ge as integer, z_zf as integer

Different numerical types represent different ranges. Integer stores -32,768 to 32,767

p>

Long stores -2,147,483,648 to 2,147,483,647

Single stores negative numbers: -3.402823E38 to -1.401298E-45; positive numbers: 1.401298E-45 to 3.402823E38

Double Store negative numbers: -1.79769313486232E308 to - 4.94065645841247E-324;

Positive numbers: 4.94065645841247E-324 to 1.79769313486232E308

Currency stores numbers from -922,3 37,203,685,477.5808 to 922,337,203,685,477.5807

Isn’t it very high!

In terms of operation speed, integer is the fastest! But it must be decided according to the actual situation! If the variable needs to include a decimal part, it can be declared as single , double, currency. The main difference between single and double is not their numerical range, but the precision of the numerical value. For example, if single is used to represent 1 divided by 3, the result is 0.33

33333 If we use double, its result is 0. 333333333333333 In practical applications, this can be used flexibly!

currency can store fixed-point numbers, and it supports 15 digits before and 4 digits after the decimal.< /p>

What we need to pay attention to here is that different numeric types represent different formats inside the machine. All values ??must end in a certain number of digits. For example, the result of dividing 1 by 3 we just ran is 0.3333333... ...Infinitely many 3. In the machine, even if all the memory is used up, it will still be truncated.

Let’s take a look at the following program

Private Sub Form_Load()

Dim a As Single

a = 1 / 3

Debug.Print a

End Sub

After running, in the "Immediate" box, the result you see is 0. 3333333

If we multiply it by 10000000

the result will be 3333334

Obviously, its result is not what we expected. Maybe we don’t particularly care about these errors, but we must keep in mind that this kind of error is likely to be gradually amplified in future calculations! We have a preliminary understanding There are numeric variables in VB. Let’s talk about the precision of numeric variables. Similarly, each data type has its own precision. We don’t need to pay too much attention to the precision of integer data. The concept of precision is reflected in floating point numbers. More! The smaller the integer part of a floating point number, the more decimal places it can place. This is because the number of memory bytes to store data is certain.

For example, we use double The precision double data type indicates that the result of dividing 2 by 3 is 0.666666666666667 and the result of dividing 200000 by 3 is 66666.6666666667

Because VB can only use 8 bytes to store this number, Some bytes of larger numbers need to be allocated to the integer part, and the number of bytes in the decimal part is smaller.

If we need the highest possible accuracy, some precision control techniques need to be learned. .

1. Adjust the deviation of the number. For example, when we calculate a number between 10000000 and 10000001, we can first subtract 10000000 to reduce the number to between 0 and 1, and then proceed to the operation After completion, add 10000000 to the result

2. Try to reduce unnecessary calculations. Among the various operations performed, if it involves very complex operations, we need to simplify the formula. To avoid unnecessary calculation steps (this is also called laziness). For example, when calculating c=a*b/a+a, we can write the formula as c=b+a. This will reduce unnecessary After understanding the error and precision, let’s learn about the byte data type

As we can see from the above, the minimum memory occupied by digital data storage is 2 bytes, and there is no type of digital data that is stored In a single byte type. However, in some situations, if you want to access a binary file, you need to access each byte of the data. At this time, the byte type comes in handy...

The byte data type can represent integers between 0 and 255. The format is the same as other numeric data declarations.

dim a as byte

byte data Types can also be used for numerical calculations, but the calculated numbers and results must be controlled within the number 255. Otherwise, an overflow error will occur (this is the most taboo for programmers)

For example, the following code< /p>

dim a as byte

dim b as byte

a=230

b=50

b= b+a

debug.print b

The result is 280, but an overflow error will occur at this time, because 280 is not placed in 1 byte. However, it is worth it Note that if we use msgbox a+b, it can display normal results!

>

Haha, vb is interesting...