Define two classes inside a page : Class Define « Language Basics « ASP.Net






Define two classes inside a page


<%@ 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("<b>New object 'MyCar' created.</b>")


  Response.Write("<br/>Color: " & MyCar.Color)
  Response.Write("<br/>Gear: " & MyCar.Gear)

  MyCar.Color = "Black"
  MyCar.ChangeGear(+1)
  Response.Write("<br/><b>Properties updated.</b>")

  Response.Write("<br/>New color: " & MyCar.Color)
  Response.Write("<br/>New gear: " & MyCar.Gear)

  MyCar.ChangeGear("up")
  Response.Write("<br/><b>Shifted 'up' one gear.</b>")

  Response.Write("<br/>New gear: " & MyCar.Gear)

  Response.Write("<hr/>Attempting to start MyCar with AlexKey: ")
  MyCar.Ignition(AlexKey)
  Response.Write(MyCar.IsRunning)

  Response.Write("<hr/>Attempting to start MyCar with RobKey: ")
  MyCar.Ignition(RobKey)
  Response.Write(MyCar.IsRunning)

  Response.Write("<hr/>Attempting to start MyCar with MyKey: ")
  MyCar.Ignition(MyKey)
  Response.Write(MyCar.IsRunning)
  Response.Write("<br/>Attempting to stop MyCar: ")
  MyCar.EngineOff()
  Response.Write(MyCar.IsRunning)

End Sub
</script>

           
       








Related examples in the same category

1.Define a class inside page