T.XL T.XL - 5 months ago 15
Vb.net Question

Using a base class and derived class in Visual basic

I am attempting to learn VB and am stuck on how to make my classes work. I am trying to create an imaginary pet shop store. The Pet class is my base class and the Cat is my derived class. When trying to run this I have errors in debugging and do not know how to get my information to display correctly from the form once it is running. If anyone has time to look at my code and offer advice on how to move forward, it would be much appreciated.

Pet:

Public Class Pet

Private animal As String
Private breed As String
Private gender As String
Private age As Integer


Public Property AnimalType() As String
Set(ByVal Value As String)
animal = Value
End Set
Get
Return animal
End Get
End Property

Public Sub New(ByVal animal As String)
Me.animal = animal
End Sub

Public Property PetBreed() As String
Set(ByVal Value As String)
breed = Value
End Set
Get
Return Breed
End Get
End Property

Public Sub New(ByVal Breed As String)
Me.breed = Breed
End Sub

Public Property PetGender() As String
Set(ByVal Value As String)
gender = Value
End Set
Get
Return Gender
End Get
End Property

Public Sub New(ByVal Gender As String)
Me.gender = Gender
End Sub

Public Property PetAge() As Integer
Set(ByVal Value As Integer)
age = Value
End Set
Get
Return Age


Cat:

Public Class Cat
Inherits Pet
End Class


PetShop:

Public Class PetShop
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim pet1 As New Pet("Boggle")
Console.WriteLine("Animal: " & pet1.AnimalType & "Breed: " & pet1.PetBreed & "Gender: " & pet1.PetGender & "Age: " & pet1.PetAge)
Console.ReadLine()

End Sub
End Class

Answer

Here is a full example for you to see and examine.

Module Module1

    Sub Main()
        Dim cat As Pet = New Cat("Hulk", "none - street cat", "male", #2/1/2005#)
        Console.WriteLine(cat.ToString)
        Console.ReadLine()
    End Sub


    Public MustInherit Class Pet

        Public Property Breed As String
        Public Property Gender As String
        Public Property DateOfBirth As DateTime

        Public ReadOnly Property Age As Integer
            Get
                Dim a As Integer = DateTime.Now.Year - DateOfBirth.Year
                Return Convert.ToInt32(If(DateOfBirth.Date > DateTime.Now.Date.AddYears(-a), --a, a))
            End Get
        End Property

        Public Sub New(breed As String, gender As String, dateOfBirth As DateTime)
            Me.Breed = breed
            Me.Gender = gender
            Me.DateOfBirth = dateOfBirth
        End Sub

    End Class

    Public Class Cat
        Inherits Pet

        Public Property Name As String

        Public Sub New(name As String, breed As String, gender As String, dateOfBirth As DateTime)
            MyBase.New(breed, gender, dateOfBirth)
            Me.Name = name
        End Sub

        Public Overrides Function ToString() As String
            Return String.Format("Name:{0}, Breed:{1}, Gender:{2}, DateOfBirth:{3:dd/MM/yyy}, Age:{4}", Name, Breed, Gender, DateOfBirth, Age)
        End Function

    End Class
End Module
Comments