Variables in visual basic pdf




















For example, you might want to calculate several values, compare them, and perform different operations on them, depending on the result of the comparison. You have to retain the values if you want to compare them. Visual Basic, just like most programming languages, uses variables for storing values. A variable has a name the word that you use to refer to the value that the variable contains.

A variable also has a data type which determines the kind of data that the variable can store. A variable can represent an array if it has to store an indexed set of closely related data items. Local type inference enables you to declare variables without explicitly stating a data type. Instead, the compiler infers the type of the variable from the type of the initialization expression.

You use assignment statements to perform calculations and assign the result to a variable, as the following example shows. The value is being assigned to the variable applesSold. Like a variable, a property represents a value that you can access.

However, it is more complex than a variable. A property uses code blocks that control how to set and retrieve its value. For variable naming rules and considerations, see Declared Element Names. A local variable is one that is declared within a procedure. A member variable is a member of a Visual Basic type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal to that class, structure, or module.

In a class or structure, the category of a member variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable , and it exists in a single copy shared among all instances of the class or structure. Otherwise it is an instance variable , and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance of the class or structure in which it was created.

It is independent of a copy of the instance variable in any other instance of the class or structure. The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:. An elementary data type, such as Boolean , Long , or Decimal. An interface type, such as IComparable or IDisposable.

You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables i , j , and k are declared as type Integer , l and m as Long , and x and y as Single :. For more information on data types, see Data Types. For more information on objects, see Objects and Classes and Programming with Components. Type inference is used to determine the data types of local variables declared without an As clause.

The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type.

In the following example, both num1 and num2 are strongly typed as integers. If you want to use local type inference, Option Infer must be set to On. The lifetime of a variable is the period of time during which it is available for use. In general, a variable exists as long as the element that declares it such as a procedure or class continues to exist.

If the variable does not need to continue existing beyond the lifetime of its containing element, you do not need to do anything special in the declaration.



0コメント

  • 1000 / 1000