ASP.NET Razor - VB Variables
A variable is a named entity used to store data.
Variables
Variables are used to store data.
A variable name must begin with a letter character and cannot contain spaces or reserved characters.
A variable can be a specified type, representing the type of data it stores. A string variable stores string values ("Welcome to runoob.com"), an integer variable stores numeric values (103), a date variable stores date values, and so on.
Variables are declared using the Dim keyword, or by using a type (if you want to declare the type), but ASP.NET can usually determine the data type automatically.
Example
// Using the Dim keyword:
Dim greeting = "Welcome to runoob.com"
Dim counter = 103
Dim today = DateTime.Today
// Using data types:
Dim greeting As String = "Welcome to runoob.com"
Dim counter As Integer = 103
Dim today As DateTime = DateTime.Today
Data Type
The commonly used data types are listed below:
| Type | Description | Example |
|---|---|---|
| integer | Integer (whole number) | 103, 12, 5168 |
| double | 64-bit floating point number | 3.14, 3.4e38 |
| decimal | Decimal (high precision) | 1037.196543 |
| boolean | Boolean | true, false |
| string | String | "Hello runoob.com", "John" |
operator
Operators tell ASP.NET what command to execute in an expression.
The VB language supports various operators. The commonly used operators are listed below:
| operator | Description | Example |
|---|---|---|
| = | Assigns a value to a variable. | i=6 |
| + |
-
*
/ | 加上一个值或者一个变量。
减去一个值或者一个变量。
乘以一个值或者一个变量。
除以一个值或者一个变量。 | i=5+5
i=5-5
i=5*5
i=5/5 |
| +=
-= | Variable increment.
Variable decrement. | i += 1
i -= 1 | | = | Equal. Returns true if the values are equal. | if i=10 | | <> | Not equal. Returns true if the values are not equal. | if <>10 | | <
>
<=
>= | Less than.
Greater than.
Less than or equal to.
Greater than or equal to. | if i<10
if i>10
if i<=10
if i>=10 | | & | Concatenates strings (a series of related things). | "w3" & "schools" | | . | Dot. Separates an object and its method. | DateTime.Hour | | () | Parentheses. Groups values. | (i+5) | | () | Parentheses. Passes parameters. | x=Add(i,5) | | () | Parentheses. Accesses values in an array or collection. | name(3) | | Not | Not. Inverts true/false. | if Not ready | | And
OR | Logical AND.
Logical OR. | if ready And clear
if ready Or clear | | AndAlso
orElse | Extended logical AND.
Extended logical OR. | if ready AndAlso clear
if ready OrElse clear |
Converting Data Types
Converting from one data type to another is sometimes useful.
The most common example is converting string input into another type, such as an integer or a date.
As a general rule, user input is treated as a string, even if the user enters a number. Therefore, numeric input must be converted to a number before it can be used in calculations.
The commonly used conversion methods are listed below:
| Method | Description | Example |
|---|---|---|
| AsInt() | ||
| IsInt() | Converts a string to an integer. | if myString.IsInt() then |
myInt=myString.AsInt()
end if |
| AsFloat()
IsFloat() | Converts a string to a float. | if myString.IsFloat() then
myFloat=myString.AsFloat()
end if | | AsDecimal()
IsDecimal() | Converts a string to a decimal. | if myString.IsDecimal() then
myDec=myString.AsDecimal()
end if | | AsDateTime()
IsDateTime() | Converts a string to the ASP.NET DateTime type. | myString="10/10/2012"
myDate=myString.AsDateTime() | | AsBool()
IsBool() | Converts a string to a Boolean. | myString="True"
myBool=myString.AsBool() | | ToString() | Converts any data type to a string. | myInt=1234
myString=myInt.ToString() |