Razor supports VB (Visual Basic) in ASP.NET, and this section explains VB variables.
Variables are named entities used to store data.
Variables are used to store data.
A variable name must begin with an alphabetic character and cannot contain spaces or reserved characters. A variable can be of a specified type, indicating the type of data it stores. The string variable stores a string value ("Welcome to w3cschool.cn"), 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.
// Using the Dim keyword: Dim greeting = "Welcome to w3cschool.cn" Dim counter = 103 Dim today = DateTime.Today // Using data types: Dim greeting As String = "Welcome to w3cschool.cn" Dim counter As Integer = 103 Dim today As DateTime = DateTime.Today
Commonly used data types are listed below:
type | describe | Example |
---|---|---|
integer | Integer (all numbers) | 103, 12, 5168 |
double | 64-bit floating point number | 3.14, 3.4e38 |
decimal | Decimal numbers (high precision) | 1037.196543 |
boolean | Boolean value | true, false |
string | string | "Hello w3cschool.cn", "John" |
Operators tell ASP.NET what commands to perform in an expression.
The VB language supports a variety of operators. Commonly used operators are listed below:
operator | describe | Example |
---|---|---|
= | Assign a value to a variable. | i=6 |
+ - */ | Add a value or a variable. Subtract a value or variable. Multiply a value or variable. Divide by a value or variable. | i=5+5 i=5-5 i=5*5 i=5/5 |
+= -= | The variable is incremented. Decrement the variable. | i += 1 i -= 1 |
= | equal. Returns true if the values are equal. | if i=10 |
<> | No wait. 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 |
& | A connection string (a series of related things). | "w3" & "schools" |
. | Dot number. Separate objects and methods. | DateTime.Hour |
() | parentheses. Group values. | (i+5) |
() | parentheses. Pass parameters. | x=Add(i,5) |
() | parentheses. Access the values of an array or collection. | name(3) |
Not | No. True/false negation. | 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 from one data type to another is sometimes useful. The most common example is converting a string input to another type, such as an integer or date.
As a general rule, user input is treated as a string, even if the user enters a number. Numeric inputs must therefore be converted into numbers before they can be used in calculations.
Common conversion methods are listed below:
method | describe | Example |
---|---|---|
AsInt() IsInt() | Convert string to integer. | if myString.IsInt() then myInt=myString.AsInt() end if |
AsFloat() IsFloat() | Convert a string to a floating point number. | if myString.IsFloat() then myFloat=myString.AsFloat() end if |
AsDecimal() IsDecimal() | Convert a string to a decimal number. | if myString.IsDecimal() then myDec=myString.AsDecimal() end if |
AsDateTime() IsDateTime() | Convert string to ASP.NET DateTime type. | myString="10/10/2012" myDate=myString.AsDateTime() |
AsBool() IsBool() | Convert a string to a Boolean value. | myString="True" myBool=myString.AsBool() |
ToString() | Convert any data type to string. | myInt=1234 myString=myInt.ToString() |
The above is the introduction to the basic knowledge of variables, operators, data types and data type conversion related to VB variables in this section.