ASP.NET Razor - متغيرات VB
المتغيرات هي كيانات مسماة تستخدم لتخزين البيانات.
المتغيرات
تستخدم المتغيرات لتخزين البيانات.
يجب أن يبدأ اسم المتغير بحرف أبجدي ولا يمكن أن يحتوي على مسافة بيضاء أو أحرف محجوزة.
يمكن أن يكون المتغير من نوع معين ، يشير إلى نوع البيانات التي يخزنها. تقوم متغيرات السلسلة بتخزين قيم السلسلة ("مرحبًا بك في W3Schools") ، ومتغيرات الأعداد الصحيحة وقيم رقم المتجر (103) ، ومتغيرات التاريخ وقيم التاريخ المخزنة ، وما إلى ذلك.
يتم الإعلان عن المتغيرات باستخدام الكلمة الأساسية Dim ، أو باستخدام النوع (إذا كنت تريد التصريح عن type) ، ولكن يمكن لـ 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() |