ASP.NET Razor - VB Variables


Variables are named entities used to store data.


Variables

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. A string variable stores string values ("Welcome to example.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 specifying a type (if you want to declare a type), but ASP.NET can usually automatically determine the data type.

Examples

// Using the Dim keyword:
Dim greeting = "Welcome to example.com"
Dim counter = 103
Dim today = DateTime.Today

// Using data types:
Dim greeting As String = "Welcome to example.com"
Dim counter As Integer = 103
Dim today As DateTime = DateTime.Today


Data Types

The following lists commonly used data types:

Type Description Example
integer Integer (whole numbers) 103, 12, 5168
double 64-bit floating point number 3.14, 3.4e38
decimal Decimal number (high precision) 1037.196543
boolean Boolean true, false
string String "Hello example.com", "John"


Operators

Operators tell ASP.NET what kind of commands to perform in expressions.

The VB language supports multiple operators. The following lists commonly used operators:

Operator Description Example
= Assigns a value to a variable. i=6
+
-
*
/
Adds a value or a variable.
Subtracts a value or a variable.
Multiplies by a value or a variable.
Divides by a value or a variable.
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
Increments the variable.
Decrements the variable.
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 interrelated things). "w3" & "schools"
. Dot. Separates objects and methods. DateTime.Hour
() Parentheses. Groups values. (i+5)
() Parentheses. Passes parameters. x=Add(i,5)
() Parentheses. Accesses values in arrays or collections. name(3)
Not Not. Reverses 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 to another type, such as an integer or a date.

As a general rule, user input is treated as strings, even if the user enters numbers. Therefore, numeric input must be converted to numbers before it can be used in calculations.

The following lists commonly used conversion methods:

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 floating-point number. if myString.IsFloat() then
myFloat=myString.AsFloat()
end if
AsDecimal()
IsDecimal()
Converts a string to a decimal number. 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()

Other Extensions