Razor Tutoriallogin
Razor Tutorial
author:php.cn  update time:2022-04-11 14:21:21

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

// Using the Dim keyword:
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:

TypeDescriptionInstance
integerInteger (all numbers)103, 12, 5168
double64-bit floating point number3.14, 3.4e38
decimalDecimal number (high precision)1037.196543
booleanBoolean valuetrue, false
stringstring"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:

##+=Variable increment. i += 1##=<><>more than the. if i>10##&Connection string (a series of related things). "w3" & "schools".Dot mark. Separate objects and methods. DateTime.Hour()Parents. Group values. (i+5)()Parents. Pass parameters. x=Add(i,5)()Parents. Access the values ​​of an array or collection. name(3)NotNot. True/false negation. if Not readyAndORAndAlsoorElse

Convert data types

It is sometimes useful to convert from one data type to another.

The most common example is converting a string input to another type, such as an integer or date.

Under general rules, 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.

Commonly used conversion methods are listed below:

OperatorDescriptionExample
=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 is decremented.
i -= 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 .
Less than or equal to.
greater or equal to.

if i<10
if i<=10
if i>=10

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

##AsFloat()Convert string to floating point number. if myString.IsFloat() thenAsDecimal()Convert string to decimal number. if myString.IsDecimal() then##AsDateTime()IsDateTime()myDate=myString.AsDateTime()AsBool()IsBool()myBool=myString.AsBool()ToString()myString=myInt.ToString()
MethodDescriptionExample
AsInt()
IsInt()
Convert string to integer. if myString.IsInt() then
myInt=myString.AsInt()
end if
IsFloat()
myFloat=myString.AsFloat()
end if
IsDecimal()
myDec=myString.AsDecimal()
end if

Convert string to ASP.NET DateTime type.
myString="10/10/2012"

Convert string to Boolean value.
myString="True"
Convert any data type to string. myInt=1234