• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Visual Basic Livestock object for cattle breeding and selling on the property.

I am learning VB.NET, I need lots of help to create an object for cattle breeding. So I need a livestock object I guess. it might hold it's 'parents', DOB, weights, sell date/price, per kg ?? transport and feed costs.
But i dont know how to make this object so if you do it well that is super :)
0
rigidigital
Asked:
rigidigital
  • 5
  • 3
  • 2
4 Solutions
 
Bob LearnedCommented:
When thinking about inheritance and object models, you need to start thinking about what would be common to all classes, and put that into the "base" class.  Then, you need to determine how many different classes that you are going to need that will "derive" from the "base" class, and add additional behaviors, attributes, and actions.

Bob
0
 
rigidigitalAuthor Commented:
Well thanks for the paragraph, but in general terms I see what that means. However I am absolutely not capable of making this object so this is why I have asked for an expert !
0
 
rigidigitalAuthor Commented:
if i could give a thousand points i would ,but it won't let me :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VBRocksCommented:
Well, here's a start for you...

        'To create a new one:
        Dim stock As New LiveStock()
        stock.DateOfBirth = #1/1/2000#
        stock.Price = 500
        'Etc...

Add a class to your project, and name is "LiveStock".  Then open it up, remove the code, and copy and
paste this code into your class:

Public Class LiveStock

    Public Sub New()
    End Sub


    Private m_DateOfBirth As Nullable(Of Date)
    Public Property DateOfBirth() As Nullable(Of Date)
        Get
            Return m_DateOfBirth
        End Get
        Set(ByVal value As Nullable(Of Date))
            m_DateOfBirth = value
        End Set
    End Property


    Private m_Weights As Integer
    Public Property Weights() As Integer
        Get
            Return m_Weights
        End Get
        Set(ByVal value As Integer)
            m_Weights = value
        End Set
    End Property



    Private m_SellDate As Nullable(Of Date)
    Public Property SellDate() As Nullable(Of Date)
        Get
            Return m_SellDate
        End Get
        Set(ByVal value As Nullable(Of Date))
            m_SellDate = value
        End Set
    End Property



    Private m_Price As Decimal
    Public Property Price() As Decimal
        Get
            Return m_Price
        End Get
        Set(ByVal value As Decimal)
            m_Price = value
        End Set
    End Property


    Private m_TransportCost As Decimal
    Public Property TransportCost() As Decimal
        Get
            Return m_TransportCost
        End Get
        Set(ByVal value As Decimal)
            m_TransportCost = value
        End Set
    End Property


    Private m_FeedCost As Decimal
    Public Property FeedCost() As Decimal
        Get
            Return m_FeedCost
        End Get
        Set(ByVal value As Decimal)
            m_FeedCost = value
        End Set
    End Property


    'Etc...

End Class



0
 
Bob LearnedCommented:
Well, that was the introductory paragraph.  Now, you need to tell me what is it about the livestock that is common, and what is unique (behaviors, attributes, etc.)?  What do you want the class to collect and what functions do you want to perform?

In the simple terms, it would be something like this:

Public Class Animal
   Public Type As String
End Class

Public Class Mammal
   Inherits From Animal

   Public NumberOfFeet As Integer
   Public NumberOfEyes As Integer
   Public Overridable Sub Walk()
   End Sub
End Class

Public Class Dog
   Inherits From Mammal

   Public CanBark As Boolean
   Public HasLongHair As Boolean

   Public Overrides Walk()
   End Sub

End Class

Bob
0
 
rigidigitalAuthor Commented:
thanks for the head start. I will need a day or two and get back about the possible functions etc.

mick.L.
0
 
rigidigitalAuthor Commented:
Is there a simpler way to do this line -->  Private m_DateOfBirth As Nullable(Of Date)

I guess Nullable allows it to be null ? but that seems confusing!
0
 
rigidigitalAuthor Commented:
In case you are wondering, I am going to serialize the objects that are instantiated and store them in a text file or sql express. The objects are going to know if they have been weighed ior branded or sold etc.
 I think I will get the livestock object to do all this itself ? rather than create another object to 'check' !

Am I sane ?
0
 
Bob LearnedCommented:
You need to look at the data that you need to collect, and evaluate how you need to collect it.  If you think about it, using a nullable date data type is just a natural thing to do now, but it doesn't really make sense with DOB, since it should never be NULL.

Sanity check--how many of these live stock objects are you looking to store?  If it is 100000 or 1000000, then using that many text files would just be "crazy", in my humble, since that is the beauty of using database systems (storing and retrieving terabytes of data for example).

Bob
0
 
VBRocksCommented:
rigidigital:

You were wondering abut the use of a Nullable(Of Date) datatype.  You don't have to use that datatype,
however, if you use only a Date datatype, then you ALWAYS have to have a date entered in that field,
and that's really not practicle, even with the DOB.

Here's a typical scenario for someone entring data into your database:
Someone brings in a form, filled out, with information about the livestock, and puts it in the inbox of
the secretary, or whoever is entering in the data into the database.  However, the form is not completely
filled out.  Because the person who filled it out didn't know, or wasn't sure of the DOB of the livestock,
they left that field empty.  So, the person that is entering the data into your database enters all of the
other information, but when they come to the DOB field, it's not filled in...  So what do they do?  

If you use a Date datatype, then they either guess at it, or enter some pseudo date, but they HAVE to
enter a date before saving the record, or cancel their entry and lose all of their work.  Which would
be very annoying to anyone.  However, if you use a Nullable(Of Date) datatype, then when the person
that is entering the data into your database gets to that field, if they don't know what it is, then they can
just leave it blank (Null), fill out the rest of the data, save the record, and come back to it later.

In object oriented programming, I strongly recommend using the Nullable(Of Date) datatype whenever
you need to support nulls in a value type, such as an integer, Boolean, or date.

If you are going to be working with data from a database, then when you actually perform the update, you can use a function in your LiveStock class to check your nullable datatypes, that will either return
their value, or DBNull.Value, because you cannot sent a null (Nothing) to the database.  That function
would look like this:

        Private Function ParseValue(ByVal item As Object) As Object
            If IsNothing(item) = True Then
                Return DBNull.Value

            Else
                Return item

            End If
        End Function


'Then, when you go to make the update to your database, you can set the value for your parameter
like this:

Public Class Livestock

    'Return True if save was successful, False if not.
    Public Function Save() As Boollean

        'Code for SQLCommand

        'Create a new parameter for a UpdateCommand for a SQL database
        '    Enter the Parameter name
        '    Enter the value, using the ParseValue function to return DBNull.Value if it is Nothing
        Dim param As New SqlClient.SqlParameter("@DOB", ParseValue(Me.DOB))

        'Etc...

    End Function
End Class

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now