Latest web development tutorials

ASP.NET Razor VB variable

Variable is a named entity used to store data.


variable

Variable is used to store data.

A variable name must begin with an alphabetic character, and can not contain spaces or reserved characters.

A variable can be a specified type, it indicates the type of data it stores. string variable to store the string value ( "Welcome to W3CSchool.cc"), integer variable stores the digital value (103), date variable to store date values, and so on.

Variables declared using the Dim keyword, or by using the type (if you want to declare type) statement, but generally ASP.NET can automatically determine the data type.

Examples

// 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


type of data

Listed below are commonly used data types:

类型 描述 实例
integer 整数(全数字) 103, 12, 5168
double 64 位浮点数 3.14, 3.4e38
decimal 十进制数字(高精度) 1037.196543
boolean 布尔值 true, false
string 字符串 "Hello W3CSchool.cc", "John"


Operators

Operators tell ASP.NET what kind of command in the expression.

VB language supports a variety of operators. Listed below are commonly used operators:

运算符 描述 实例
= 给一个变量赋值。 i=6
+
-
*
/
加上一个值或者一个变量。
减去一个值或者一个变量。
乘以一个值或者一个变量。
除以一个值或者一个变量。
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
变量递增。
变量递减。
i += 1
i -= 1
= 相等。如果值相等则返回 true。 if i=10
<> 不等。如果值不等则返回 true。 if <>10
<
>
<=
>=
小于。
大于。
小于等于。
大于等于。
if i<10
if i>10
if i<=10
if i>=10
& 连接字符串(一系列互相关联的事物)。 "w3" & "schools"
. 点号。分隔对象和方法。 DateTime.Hour
() 圆括号。将值进行分组。 (i+5)
() 圆括号。传递参数。 x=Add(i,5)
() 圆括号。访问数组或者集合的值。 name(3)
Not 非。真/假取反。 if Not ready
And
OR
逻辑与。
逻辑或。
if ready And clear
if ready Or clear
AndAlso
orElse
扩展的逻辑与。
扩展的逻辑或。
if ready AndAlso clear
if ready OrElse clear


Converting Data Types

Conversion from one data type to another data type, sometimes very useful.

The most common example is to convert the input string to another type, such as integer or date.

Under the general rule, it is seen as the user input string processing, even if the user enters the number. Therefore, the value must be converted to a digital input, before it can be used for calculations.

The following lists common conversion methods:

方法 描述 实例
AsInt()
IsInt()
转换字符串为整数。 if myString.IsInt() then
myInt=myString.AsInt()
end if
AsFloat()
IsFloat()
转换字符串为浮点数。 if myString.IsFloat() then
myFloat=myString.AsFloat()
end if
AsDecimal()
IsDecimal()
转换字符串为十进制数。 if myString.IsDecimal() then
myDec=myString.AsDecimal()
end if
AsDateTime()
IsDateTime()
转换字符串为 ASP.NET DateTime 类型。 myString="10/10/2012"
myDate=myString.AsDateTime()
AsBool()
IsBool()
转换字符串为布尔值。 myString="True"
myBool=myString.AsBool()
ToString() 转换任何数据类型为字符串。 myInt=1234
myString=myInt.ToString()