ASP.NET Razor - VB Variables

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
  • +
  • -
  • *
  • /
  • Add value or variable
  • Subtract value or variable
  • Multiply value or variable
  • Divide value or variable
  • i=5+5
  • i=5-5
  • i=5*5
  • i=5/5
  • +=
  • -=
  • Increment variable
  • Decrement variable
  • i += 1
  • i -= 1
= Equal. Return true if the values are equal. if i=10
<> Not equal. Return 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)
& 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
  • 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

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
  • AsInt()
  • IsInt()
Convert a string to an 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 a string to an ASP.NET DateTime type myString = "10/10/2012"
myDate = myString.AsDateTime()
  • AsBool()
  • IsBool()
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()