본문 바로가기
Visual Basic .NET/Class

how about using Multiple Constructors to Support Different Parameters

by edupicker(체르니) 2008. 6. 24.

how about using Multiple Constructors to Support Different Parameters


To initialize class member variables, programmers make extensive use of constructor methods.

Normally, for a simple class, the constructor method will support a parameter for each key class member variable.
Consider the following Book class that provides support for the title, author,

publisher, price, pagecount, and Indate:

 

source code :

 
  class book

        Public Title As String
        Public Publisher As String
        Public Author As String
        Public Price As Double
        Public PageCount As Integer
        Public InDate As String

        Public Sub New(ByVal Title As String, ByVal Price As Double)
            Me.Title = Title
            Me.Price = Price
            Me.Author = ""
            Me.PageCount = 0
            Me.Publisher = ""
            Me.InDate = ""
        End Sub

        Public Sub New(ByVal Title As String, ByVal Author As String, _
        ByVal Publisher As String, ByVal Price As Double)
            Me.Title = Title
            Me.Author = Author
            Me.Publisher = Publisher
            Me.Price = Price
            Me.PageCount = 0
            Me.InDate = ""
        End Sub

        Public Sub New(ByVal Title As String, ByVal Author As String, _
        ByVal Publisher As String, ByVal Price As Double, _
        ByVal PageCount As Integer, ByVal InDate As Date)
            Me.Title = Title
            Me.Author = Author
            Me.Publisher = Publisher
            Me.Price = Price
            Me.PageCount = PageCount
            Me.InDate = InDate
        End Sub

        .......

 end class

    Sub Main()

        Dim OSBook = New Book("Learning Linux 5th", 29.4)
        Dim ProgBook = New Book("VB.NET", "James Bond", "Prentice Hall", 39.5)
        Dim EconoBook = New Book("social networking", _
        "Gildong,Hong", "Yangji", 12.59, 256, "2008-02-01")

        OSBook.DisplayInfo()
        Console.WriteLine()

        ProgBook.DisplayInfo()
        Console.WriteLine()

        EconoBook.DisplayInfo()
        Console.ReadLine()

    End Sub

execution :

사용자 삽입 이미지


'Visual Basic .NET > Class' 카테고리의 다른 글

A destructor method in a class  (0) 2008.06.26
using MyBase  (0) 2008.06.25
understand the inherited constructors of class  (0) 2008.06.25
how to Initialize Class Member Variables  (0) 2008.06.24
How to Restrict Access to Class Members?  (0) 2008.06.23