Dao cạo ASP.NET - Biến VB
Biến là các thực thể được đặt tên dùng để lưu trữ dữ liệu.
Biến
Các biến được sử dụng để lưu trữ dữ liệu.
Tên biến phải bắt đầu bằng ký tự chữ cái và không được chứa khoảng trắng hoặc ký tự dành riêng.
Một biến có thể thuộc một loại cụ thể, cho biết loại dữ liệu mà nó lưu trữ. Biến chuỗi lưu trữ giá trị chuỗi ("Chào mừng đến với W3Schools"), biến số nguyên lưu trữ giá trị số (103), biến ngày lưu trữ giá trị ngày, v.v.
Các biến được khai báo bằng từ khóa Dim hoặc bằng cách sử dụng loại (nếu bạn muốn khai báo loại), nhưng ASP.NET thường có thể tự động xác định loại dữ liệu.
Ví dụ
//
Using the Dim keyword:
Dim greeting = "Welcome to W3Schools"
Dim counter = 103
Dim today = DateTime.Today
// Using data types:
Dim greeting As String = "Welcome to W3Schools"
Dim counter As Integer =
103
Dim today As DateTime = DateTime.Today
Loại dữ liệu
Dưới đây là danh sách các kiểu dữ liệu phổ biến:
Type | Description | Examples |
---|---|---|
integer | Integer (whole numbers) | 103, 12, 5168 |
double | 64 bit floating-point number | 3.14, 3.4e38 |
decimal | Decimal number (higher precision) | 1037.196543 |
boolean | Boolean | true, false |
string | String | "Hello W3Schools", "John" |
Toán tử
Một toán tử cho ASP.NET biết loại lệnh nào cần thực hiện trong một biểu thức.
Ngôn ngữ VB hỗ trợ nhiều toán tử. Dưới đây là danh sách các toán tử phổ biến:
Operator | Description | Example |
---|---|---|
= | Assigns a value to a variable. | i=6 |
+ - * / |
Adds a value or variable. Subtracts a value or variable. Multiplies a value or variable. Divides a value or variable. |
i=5+5 i=5-5 i=5*5 i=5/5 |
+= -= |
Increments a variable. Decrements a variable. |
i += 1 i -= 1 |
= | Equality. Returns true if values are equal. | if i=10 |
<> | Inequality. Returns true if values are not equal. | if <>10 |
< > <= >= |
Less than. Greater than. Less than or equal. Greater than or equal. |
if i<10 if i>10 if i<=10 if i>=10 |
& | Adding strings (concatenation). | "w3" & "schools" |
. | Dot. Separate objects and methods. | DateTime.Hour |
() | Parenthesis. Groups values. | (i+5) |
() | Parenthesis. Passes parameters. | x=Add(i,5) |
() | Parenthesis. Accesses values in arrays or collections. | name(3) |
Not | Not. Reverses 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 |
Chuyển đổi kiểu dữ liệu
Việc chuyển đổi từ loại dữ liệu này sang loại dữ liệu khác đôi khi rất hữu ích.
Ví dụ phổ biến nhất là chuyển đổi đầu vào chuỗi sang loại khác, chẳng hạn như số nguyên hoặc ngày.
Theo quy định, thông tin đầu vào của người dùng ở dạng chuỗi, ngay cả khi người dùng đã nhập một số. Vì vậy, các giá trị số đầu vào phải được chuyển đổi thành số trước khi chúng có thể được sử dụng trong tính toán.
Dưới đây là danh sách các phương thức chuyển đổi phổ biến:
Method | Decryptions | 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 an 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() |