ASP.NET Razor - VB Variables
- Previous Page Razor C# Logic
- Next Page Razor VB Loop
Variables are named entities used to store data.
Variables
Variables are used to store data.
Variable names must start with a letter character and cannot contain spaces or reserved characters.
Variables can be of a specific type, indicating the type of data they store. String variables store string values ("Welcome to CodeW3C.com"), integer variables store numeric values (103), date variables store date values, and so on.
Declare variables using the Dim keyword or type, but ASP.NET can usually automatically determine the data type.
Example
// Use Dim keyword: Dim greeting = "Welcome to CodeW3C.com" Dim counter = 103 Dim today = DateTime.Today // Use data types: Dim greeting As String = "Welcome to CodeW3C.com" Dim counter As Integer = 103 Dim today As DateTime = DateTime.Today
Data type
Here is a list of common data types:
Type | Description | Example |
---|---|---|
integer | Integer | 103, 12, 5168 |
double | 64-bit floating-point number | 3.14, 3.4e38 |
decimal | Decimal | 1037.196543 |
boolean | Logical values | true, false |
string | String values | "Hello CodeW3C.com", "Bill" |
Operator
Operators tell ASP.NET which type of command to execute in an expression.
VB supports multiple operators. Here are some common operators:
Operator | Description | Example |
---|---|---|
= | Assign value to variable. | i=6 |
|
|
|
|
|
|
= | Equal. Return true if the values are equal. | if i=10 |
<> | Not equal. Return true if the values are not equal. | if <>10 |
|
|
|
& | String concatenation (chaining or juxtaposition) | "w3" & "schools" |
. | Dot. Separate object and method. | DateTime.Hour |
() | Parentheses. Group values. | (i+5) |
() | Parentheses. Pass parameters. | x=Add(i,5) |
() | Parentheses. Access the value in an array or collection. | name(3) |
Not | Not. Invert true or false. | if Not ready |
|
|
|
|
|
|
Convert Data Type
Converting one data type to another can be very useful sometimes.
Most common examples are converting string input to another type, such as an integer or a date.
As a rule, user input becomes a string, even if the user input is a number. Therefore, the numeric input values must be converted to numbers before they are used for calculations.
Below is a list of commonly used conversion methods:
Method | Description | Example |
---|---|---|
|
Convert a string to an integer. |
if myString.IsInt() then myInt = myString.AsInt() end if |
|
Convert a string to a floating-point number. |
if myString.IsFloat() then myFloat = myString.AsFloat() end if |
|
Convert a string to a decimal number. |
if myString.IsDecimal() then myDec = myString.AsDecimal() end if |
|
Convert a string to an ASP.NET DateTime type |
myString = "10/10/2012" myDate = myString.AsDateTime() |
|
Convert a string to a logical value. |
myString = "True" myBool = myString.AsBool() |
ToString() | Convert any data type to a string. |
myInt = 1234 myString = myInt.ToString() |
- Previous Page Razor C# Logic
- Next Page Razor VB Loop