มีดโกน ASP.NET - ตัวแปร VB
ตัวแปรมีชื่อเอนทิตีที่ใช้ในการจัดเก็บข้อมูล
ตัวแปร
ตัวแปรใช้เพื่อเก็บข้อมูล
ชื่อของตัวแปรต้องขึ้นต้นด้วยตัวอักษรและต้องไม่มีช่องว่างหรืออักขระที่สงวนไว้
ตัวแปรอาจเป็นประเภทเฉพาะ โดยระบุประเภทของข้อมูลที่เก็บไว้ ตัวแปรสตริงเก็บค่าสตริง ("ยินดีต้อนรับสู่ W3Schools") ตัวแปรจำนวนเต็มเก็บค่าตัวเลข (103) ตัวแปรวันที่เก็บค่าวันที่ ฯลฯ
ตัวแปรถูกประกาศโดยใช้คีย์เวิร์ด Dim หรือโดยใช้ประเภท (หากคุณต้องการประกาศ ชนิด) แต่โดยปกติแล้ว ASP.NET สามารถกำหนดชนิดข้อมูลได้โดยอัตโนมัติ
ตัวอย่าง
//
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
ประเภทข้อมูล
ด้านล่างนี้เป็นรายการประเภทข้อมูลทั่วไป:
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" |
ผู้ประกอบการ
ตัวดำเนินการบอก ASP.NET ว่าต้องดำเนินการคำสั่งประเภทใดในนิพจน์
ภาษา VB รองรับตัวดำเนินการจำนวนมาก ด้านล่างนี้คือรายการตัวดำเนินการทั่วไป:
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 |
การแปลงประเภทข้อมูล
การแปลงจากประเภทข้อมูลหนึ่งไปเป็นอีกประเภทหนึ่งอาจมีประโยชน์ในบางครั้ง
ตัวอย่างที่พบบ่อยที่สุดคือการแปลงอินพุตสตริงเป็นประเภทอื่น เช่น จำนวนเต็มหรือวันที่
ตามกฎแล้ว การป้อนข้อมูลของผู้ใช้จะเป็นสตริง แม้ว่าผู้ใช้จะป้อนตัวเลขก็ตาม ดังนั้น ค่าอินพุตที่เป็นตัวเลขจะต้องแปลงเป็นตัวเลขก่อนจึงจะสามารถนำมาใช้ในการคำนวณได้
ด้านล่างนี้คือรายการวิธีการแปลงทั่วไป:
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() |