Define and use constructor : Class Constrctuctor « Language Basics « ASP.Net






Define and use constructor

<%@ 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(newTitle As String, newIsbn As Integer)
    _Title = newTitle
    _Isbn = newIsbn
  End Sub

  Public ReadOnly Property TitleInfo As String
    Get
      Return _Title & " <i>(ISBN: " & _Isbn & ")</i>"
    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("title 0", 99999999999)
  Response.Write("<b>New book 'MyBook' created.</b>")

  MyBook.Price = "39.99"
  Response.Write("<br/>Title info: " & MyBook.TitleInfo)
  Response.Write("<br/>Price: $" & MyBook.Price & "<br/>")

  Dim AnotherBook As New Book("title", 99999999999)

  Response.Write("<b>New book 'AnotherBook' created.</b>")
  AnotherBook.Price = "59.99"
  Response.Write("<br/>Title info: " & AnotherBook.TitleInfo)
  Response.Write("<br/>Price: $" & AnotherBook.Price & "<br/>")


End Sub

</script>

           
       








Related examples in the same category