Create a project that contains a Pet class. Each object will contain pet name, animal type, breed and color. The form should contain text boxes to enter the information for the pets. A button or menu item should display the pet information on a separate form. Hint: Use a ReadOnly property on the input form to pass the object to the second form.

What I have done so far is create a new class with the name Pet and have instantiated each object with readonly property. I have also created another form where the information entered by the user will be displayed. Guess I kinda need to know what would be the next step? And I want to ensure what I have done so far is correct.
Thanks.

Public Class Pet
    Private nameString, typeString, breedString, colorString As String
    'Public Sub New(ByVal name As String, ByVal type As String, ByVal breed As String, color As String)
    ' Assign the property values.

    ' With Me
    '    .name = name
    '   .type = type
    '   .breed = breed
    '   .color = color
    'End With
    'End Sub
    ReadOnly Property name() As String
        Get
            Return nameString
        End Get
    End Property
    ReadOnly Property type() As String
        Get
            Return typeString
        End Get
    End Property
    ReadOnly Property breed() As String
        Get
            Return breedString
        End Get
    End Property
    ReadOnly Property color() As String
        Get
            Return colorString
        End Get
    End Property
End Class

Recommended Answers

All 7 Replies

Hint: Use a ReadOnly property on the input form to pass the object to the second form.

Looks ok to me. But the line above says that you should use a ReadOnly property when passing the object to form. Not that the properties of the Pet class should be ReadOnly.

There has to be a way to set a Pet's (instance of the Pet class) properties. I suggest using both Get/Set for properties and removing ReadOnly keyword from the props. You had commented out the New constructor. You could remove those comment marks. Also, add a constructor w/o any parameters. If the user (i.e. programmer) of the Pet class uses a parameterless constructor, she has to provide Pet's features through it's properties. The code would be similar to this:

Public Class Pet

    Private nameString As String
    Private typeString As String
    Private breedString As String
    Private colorString As String

    Public Sub New()
        ' Assign the default values.
        With Me
            .nameString = String.Empty
            .typeString = String.Empty
            .breedString = String.Empty
            .colorString = String.Empty
        End With
    End Sub

    Public Sub New(ByVal name As String, ByVal type As String, ByVal breed As String, ByVal color As String)
        ' Assign the property values.
        With Me
            .nameString = name
            .typeString = type
            .breedString = breed
            .colorString = color
        End With
    End Sub

    Property name() As String
        Get
            Return (nameString)
        End Get
        Set(ByVal value As String)
            nameString = value
        End Set
    End Property

    ' ... here's the rest of the properties

End Class

The next step is to create the user interface (i.e. both forms) and use some collection to hold the Pets, for example Public PetCollection As List(Of Pet) .

HTH

Thank you so much for looking into it. I have designed the first form and I believe the summary or the second form will be inheriting the first form. Should the collection to hold the pets go under the first main user interface form?

The form should contain text boxes to enter the information for the pets. A button or menu item should display the pet information on a separate form. Hint: Use a ReadOnly property on the input form to pass the object to the second form.

Should the collection to hold the pets go under the first main user interface form?

That would be logical since the second form gets one Pet object through the property and displays its information. You'll need in the first form also some way to select a Pet object to be shown in the second form (ListBox with Pet names?), but this wasn't clearly stated.

HTH

Since the requirement is to contain the text box for user to enter information on the pet, I suppose I couldn't implement listbox since there are no pre-set information on the pets. I have created four text boxes, each for name, type, breed and color. But really unsure how I get all of this to play together. Do I use

imports

under the first form for it to recognize the objects from the class? I received an error stating "Imports statement must precede any declaration"
Not sure where to go from here, if you are able to shed some light.
' not sure if this is something I should be doing?

petNameTextbox.text = .nameString

Do I use imports under the first form for it to recognize the objects from the class?

You don't have to use Imports statement if the Pet class library file is in the same project as your forms. However, if you create the Pet class library as a separate project and the forms as another project (i.e. you have a VB solution with two projects), you'll have to first add a reference to Pet class library project in the forms project. After that you'll need Imports Pet in the forms project. Like the error message said Imports statement has to be at the beginning of the file (with the exception of Option statements):

Option Strict On
Option Explicit On

Imports Pet

Public Class Form1
' Class' code
End Class

not sure if this is something I should be doing?
petNameTextbox.text = .nameString

Assignment(s) should be in the opposite way, from a textbox to object's property:

Private PetCollection As List(Of Pet)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Create a new Pet object instance
    Dim newPet As New Pet()

    ' You could check if any of the textboxes is empty, show a messagebox telling user to enter Pet information and exit sub

    With newPet
        .name = petNameTextbox.Text
        ' color etc. properties
    End With
    ' Add a new Pet instance to the List
    PetCollection.Add(newPet)
    ' Clear the textboxes
    petNameTextbox.Text = ""
    ' color etc. textboxes

End Sub

The code above declares the List to hold Pet objects. You'll need also a (Save/Store/Ok...) button which user presses after filling the textboxes.

HTH

commented: Very very helpful.... +10

Sorry, due to final week, I have been occupied with other class. Once I get a chance to work on this, and if no other problem arises, I will most certainly come back and mark the thread as solved. Thank you again for all of your assistance. :)

my main form has 4 text boxes, to enter each characteristics like name, breed, color and type etc.. and has button for 'Info' which the user can click so it is supposed to bring up another form displaying that same information user just typed in on the main form.
What part of the codes are supposed to go on the second form? Sorry.. this is all new concept to me and without any working example, it has a bit confusing to understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.