ASP.NET Razor - VB veranderlikes
Veranderlikes is benoemde entiteite wat gebruik word om data te stoor.
Veranderlikes
Veranderlikes word gebruik om data te stoor.
Die naam van 'n veranderlike moet met 'n alfabetiese karakter begin en kan nie witspasie of gereserveerde karakters bevat nie.
'n Veranderlike kan van 'n spesifieke tipe wees, wat die soort data aandui wat dit stoor. Stringveranderlikes stoor stringwaardes ("Welkom by W3Schools"), heelgetalveranderlikes stoor getalwaardes (103), datumveranderlikes stoor datumwaardes, ens.
Veranderlikes word verklaar deur die Dim sleutelwoord te gebruik, of deur die tipe te gebruik (as jy die tipe), maar ASP.NET kan gewoonlik datatipes outomaties bepaal.
Voorbeelde
//
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
Datatipes
Hieronder is 'n lys van algemene datatipes:
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" |
Operateurs
'n Operator vertel ASP.NET watter soort opdrag om in 'n uitdrukking uit te voer.
Die VB-taal ondersteun baie operateurs. Hieronder is 'n lys van algemene operateurs:
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 |
Omskakeling van datatipes
Omskakeling van een datatipe na 'n ander is soms nuttig.
Die mees algemene voorbeeld is om string invoer om te skakel na 'n ander tipe, soos 'n heelgetal of 'n datum.
As 'n reël kom gebruikersinvoer as stringe, selfs al het die gebruiker 'n nommer ingevoer. Daarom moet numeriese invoerwaardes na getalle omgeskakel word voordat dit in berekeninge gebruik kan word.
Hieronder is 'n lys van algemene omskakelingsmetodes:
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() |