ASP.Net/Language Basics/Class Define
Define a class inside page
<source lang="csharp">
<%@ page language="vb" runat="server" %> <script runat="server"> Public Class Book
Private _Title As String Private _Isbn As Integer Private _Price As Decimal Public Sub New() _Title = "title" _Isbn = 999999999 End Sub Public ReadOnly Property TitleInfo As String Get Return _Title & " (ISBN: " & _Isbn & ")" End Get End Property Public ReadOnly Property Title As String Get Return _Title End Get End Property Public ReadOnly Property Isbn As Integer Get Return _Isbn End Get End Property Public Property Price As Decimal Get Return _Price End Get Set(value As Decimal) _Price = value End Set End Property
End Class Sub Page_Load()
Dim MyBook As New Book() Response.Write("New book "MyBook" created.") MyBook.Price = "39.99" Response.Write("
Title info: " & MyBook.TitleInfo) Response.Write("
Price: $" & MyBook.Price & "
")
End Sub </script>
</source>
Define two classes inside a page
<source lang="csharp">
<%@ page language="vb" runat="server" Debug="true"%> <script runat="server"> Public Class Key
Private _Shape As Integer Public Sub New(newshape As Integer) _Shape = newshape End Sub Public ReadOnly Property Shape As Integer Get Return _Shape End Get End Property
End Class Public Class Car
Private _Color As String Private _Gear As Integer Private _Ignition As Integer Private _EngineRunning As Boolean Private Shared _Count = 0 Public Shared ReadOnly Property Count As Integer Get Return _Count End Get End Property Public Property Color As String Get Return _Color End Get Set(value As String) _Color = value End Set End Property Public ReadOnly Property Gear As Integer Get Return _Gear End Get End Property Public ReadOnly Property IsRunning As String Get If _EngineRunning Then Return "The engine is running." Else Return "The engine is not running." End If End Get End Property Overloads Public Sub ChangeGear(direction As Integer) If direction < 0 Then _Gear -= 1 If direction > 0 Then _Gear += 1 If _Gear > 5 Then _gear = 5 If _Gear < -1 Then _gear = -1 End Sub Overloads Public Sub ChangeGear(direction As String) If direction = "down" Then ChangeGear(-1) If direction = "up" Then ChangeGear(+1) End Sub Public Sub Ignition(IgnitionKey as Key) If IgnitionKey.Shape = _Ignition Then _EngineRunning = True End Sub Public Sub EngineOff() _EngineRunning = False End Sub Sub New(IgnitionShape as Integer) _Color = "cold grey steel" _Ignition = IgnitionShape _Count += 1 End Sub
End Class Sub Page_Load()
Dim AlexKey As New Key(0987654321) Dim RobKey As New Key(1861005040) Dim MyKey As New Key(1234567890) Dim MyCar As New Car(1234567890) Response.Write("New object "MyCar" created.")
Response.Write("Response.Write("
Color: " & MyCar.Color) Response.Write("
Gear: " & MyCar.Gear) MyCar.Color = "Black" MyCar.ChangeGear(+1) Response.Write("
Properties updated.") Response.Write("
New color: " & MyCar.Color) Response.Write("
New gear: " & MyCar.Gear) MyCar.ChangeGear("up") Response.Write("
Shifted "up" one gear.") Response.Write("
New gear: " & MyCar.Gear)
Attempting to start MyCar with AlexKey: ")
MyCar.Ignition(AlexKey) Response.Write(MyCar.IsRunning)Response.Write("
Attempting to start MyCar with RobKey: ")
MyCar.Ignition(RobKey) Response.Write(MyCar.IsRunning)Response.Write("
Attempting to start MyCar with MyKey: ")
MyCar.Ignition(MyKey) Response.Write(MyCar.IsRunning) Response.Write("
Attempting to stop MyCar: ") MyCar.EngineOff() Response.Write(MyCar.IsRunning)
End Sub </script>
</source>