ASP.NETRazor -VB変数
変数は、データを格納するために使用される名前付きエンティティです。
変数
変数はデータを格納するために使用されます。
変数の名前は英字で始まる必要があり、空白や予約文字を含めることはできません。
変数は、格納するデータの種類を示す特定のタイプにすることができます。文字列変数は文字列値( "Welcome to 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() |