Compartilhar via


Variáveis no Visual Basic

You often have to store values when you perform calculations with Visual Basic. 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.

Usage

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.

Visual Basic 2008 introduces local type inference, which 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. For more information, see Inferência de tipo de variável local (Visual Basic).

Assigning Values

You use assignment statements to perform calculations and assign the result to a variable, as the following example shows.

' The following statement assigns the value 10 to the variable.
applesSold = 10
' The following statement increments the variable.
applesSold = applesSold + 1
' The variable now holds the value 11.
ObservaçãoObservação

The equal sign (=) in this example is an assignment operator, not an equality operator. The value is being assigned to the variable applesSold.

For more information, see Como: Inserir e Retirar Dados de uma Variável (Visual Basic).

Variables and Properties

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 more information, see Diferenças entre variáveis e propriedades em Visual Basic.

Consulte também

Tarefas

Solução de Problemas de Variáveis em Visual Basic

Como: Inserir e Retirar Dados de uma Variável (Visual Basic)

Conceitos

Declaração de variável no Visual Basic

Variáveis de objeto no Visual Basic

Diferenças entre variáveis e propriedades em Visual Basic

Inferência de tipo de variável local (Visual Basic)