Razor VB variables
ASP.NET Razor - VB Variables
Variables are named entities used to store data.
Variables
Variables are used to store data.
The name of a variable must start with an alphabetic character and cannot contain spaces or reserved characters.
A variable can be a specified type, indicating the type of data it stores. The string variable stores a string value ("Welcome to W3CSool.cc"), the integer variable stores a numeric value (103), the date variable stores a date value, and so on.
Variables are declared using the Dim keyword, or by using a type if you want to declare a type, but ASP.NET can usually determine the data type automatically.
Example
Dim greeting = "Welcome to W3CSchool.cc"
Dim counter = 103
Dim today = DateTime.Today
// Using data types:
Dim greeting As String = "Welcome to W3CSchool.cc"
Dim counter As Integer = 103
Dim today As DateTime = DateTime.Today
Data type
The following is a list of commonly used data types:
Type | Description | Instance |
---|---|---|
integer | Integer (all numbers) | 103, 12, 5168 |
double | 64-bit floating point number | 3.14, 3.4e38 |
decimal | Decimal number (high precision) | 1037.196543 |
boolean | Boolean value | true, false |
string | string | "Hello W3CSschool.cc", "John" |
Operators
Operators tell ASP.NET what commands to perform in an expression.
VB language supports multiple operators. Commonly used operators are listed below:
Operator | Description | Example | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
= | Assign a value to a variable. | i=6 | |||||||||||||||||||||
+ - * / | plus a value or a variable . Subtract a value or a variable. Multiply a value or a variable. Divide by a value or variable. | i=5+5 i=5-5 i=5*5 i=5/5 | |||||||||||||||||||||
-= | Variable increment. Variable is decremented. | i += 1i -= 1 | |||||||||||||||||||||
are equal. Returns true if the values are equal. | if i=10 | ||||||||||||||||||||||
does not vary. Returns true if the values are not equal. | if <>10 | ||||||||||||||||||||||
<= >= is less than . | more than the. Less than or equal to. greater or equal to. if i<10 | if i>10if i<=10 if i>=10 | ##&|||||||||||||||||||||
"w3" & "schools" | . | ||||||||||||||||||||||
DateTime.Hour | () | ||||||||||||||||||||||
(i+5) | () | ||||||||||||||||||||||
x=Add(i,5) | () | ||||||||||||||||||||||
name(3) | Not | ||||||||||||||||||||||
if Not ready | And | ||||||||||||||||||||||
Logical AND. Logical OR. | if ready And clear if ready Or clear | ||||||||||||||||||||||
Extended logical AND. Extended logical OR. | if ready AndAlso clear if ready OrElse clear |
Method | Description | Example |
---|---|---|
AsInt() IsInt() | Convert string to integer. | if myString.IsInt() then myInt=myString.AsInt() end if |
IsFloat() | Convert string to floating point number. if myString.IsFloat() then | myFloat=myString.AsFloat() end if |
IsDecimal() | Convert string to decimal number. if myString.IsDecimal() then | myDec=myString.AsDecimal() end if |
Convert string to ASP.NET DateTime type. | myString="10/10/2012" | myDate=myString.AsDateTime()|
Convert string to Boolean value. | myString="True" | myBool=myString.AsBool()|
Convert any data type to string. | myInt=1234 | myString=myInt.ToString()