ASP.NET Razor - VB Variables
Variables are named entities used to store data.
Variables
Variables are used to store data.
A variable name must start with a letter character and cannot contain spaces or reserved characters.
Example
Data Types
The following lists common data types:
| Type | Description | Example |
|---|---|---|
| integer | Whole numbers | 103, 12, 5168 |
| double | 64-bit floating-point number | 3.14, 3.4e38 |
| decimal | Decimal numbers (high precision) | 1037.196543 |
| boolean | Boolean values | true, false |
| string | String of characters | "Hello tutorialpro.org", "John" |
Operators
Operators tell ASP.NET what command to perform in an expression.
VB supports various operators. The following lists common operators:
| Operator | Description | Example |
|---|---|---|
| = | Assigns a value to a variable. | i=6 |
| + <br>- <br>* <br>/ <br> | Adds a value or variable. <br>Subtracts a value or variable. <br>Multiplies a value or variable. <br>Divides a value or variable. | i=5+5 <br>i=5-5 <br>i=5*5 <br>i=5/5 |
| += <br>-= | Increments the variable. <br>Decrements the variable. | i += 1 <br>i -= 1 |
| = | Equality. Returns true if values are equal. | if i=10 |
| <> | Inequality. Returns true if values are not equal. | if i<>10 |
| < <br>> <br><= <br>>= | Less than. <br>Greater than. <br>Less than or equal to. <br>Greater than or equal to. | if i<10 <br>if i>10 <br>if i<=10 <br>if i>=10 |
| & | Concatenates strings. | "w3" & "schools" |
| . | Dot. Separates objects and methods. | DateTime.Hour |
| () | Parentheses. Groups values. | (i+5) |
| () | Parentheses. Passes parameters. | x=Add(i,5) |
| () | Parentheses. Accesses array or collection values. | name(3) |
| Not | Negation. Inverts true/false. | if Not ready |
| And <br>OR | Logical AND. <br>Logical OR. | if ready And clear <br>if ready Or clear |
| AndAlso <br>orElse | Extended logical AND. <br>Extended logical OR. | if ready AndAlso clear <br>if ready OrElse clear |
Converting Data Types
Converting from one data type to another can be useful.
Generally, user input is treated as a string, even if the user enters numbers. Therefore, numeric input must be converted to a number before it can be used in calculations.
The following lists common conversion methods:
| Method | Description | Example |
|---|---|---|
| AsInt() <br>IsInt() | Converts a string to an integer. | if myString.IsInt() then <br>myInt=myString.AsInt() <br>end if |
| AsFloat() <br>IsFloat() | Converts a string to a floating-point number. | if myString.IsFloat() then <br>myFloat=myString.AsFloat() <br>end if |
| AsDecimal() <br>IsDecimal() | Converts a string to a decimal number. | if myString.IsDecimal() then <br>myDec=myString.AsDecimal() <br>end if |
| AsDateTime() <br>IsDateTime() | Converts a string to an ASP.NET DateTime type. | myString="10/10/2012" <br>myDate=myString.AsDateTime() |
| AsBool() <br>IsBool() | Converts a string to a boolean value. | myString="True" <br>myBool=myString.AsBool() |
| ToString() | Converts any data type to a string. | myInt=1234 <br>myString=myInt.ToString() |