Class/Constructor Question

Socketboy

Member
Joined
Dec 4, 2006
Messages
12
Programming Experience
Beginner
Class/Constructor Question - RESOLVED

So I am new to VB, and I am struggling through my final project for my VB course.

My question is this:

I have a main form with the main controls, text fields, checkboxes, etc.
I also have a class that I designed (as per the requirements).
I am going off of what they have in the text book, and most of the things I have found are too general. I *think* I have the main values linked ok (from the class to the form), but I am not sure how to handle the checkbox and radio button options.

Basically the user could check 0 - 3 check boxes (each adding cost to a total), as well as any one of 3 radio buttons that will add a different amount to the total. In the example in my text, there is a checkbox; they put it into a subclass and it just called that subclass when the class called.

So do I have to have a different subclass for every possible combination of radio buttons and check boxes? Or can I just work it into the main class in some constructors and call that. If so, all the constructors I add are just called new, how do I call different ones (if that is even something you can do)?

I think this is probably a fairly simple VB program, so if someone could take a look and show/tell me how to implement the input of the checkboxes and radio buttons (currently constants), and/or if this looks OK so far, it would be much appreciated.
As I said, I don't know what the heck I am doing, and right now it is returning 0 for all of the values.

Thanks!

My main form code:
VB.NET:
Try
            With Me
                apurchaseobject = New purchaseobject(Decimal.Parse(.tbxCarSales.Text), Decimal.Parse(.tbxTradein.Text))

                'Calculate and format the result
                .tbxSubtotal.Text = apurchaseobject.ammountduedecimal.ToString("C")
                .tbxTotal.Text = apurchaseobject.totaldecimal.ToString("C")
                .tbxTax.Text = apurchaseobject.taxdecimal.ToString("C")
                .tbxAmmDue.Text = apurchaseobject.ammountduedecimal.ToString("C")
                .tbxAccess.Text = apurchaseobject.accessoriesdecimal.ToString("C")
            End With
        Catch ex As Exception
            MessageBox.Show("enter numeric data.", "VBAuto", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
My class code:
VB.NET:
Public Class purchaseobject

    Public subtotaldecimal As Decimal
    Public totaldecimal As Decimal
    Public ammountduedecimal As Decimal
    Public salepricedecimal As Decimal
    Public allowancedecimal As Decimal
    Public accessoriesdecimal As Decimal
    Public finishdecimal As Decimal
    Public accessfinishtotal As Decimal
    Public taxdecimal As Decimal
    Private Const STEREO_PRICE_Decimal As Decimal = 425.76D
    Private Const LEATHER_PRICE_Decimal As Decimal = 987.41D
    Private Const NAVIGATION_PRICE_Decimal As Decimal = 1741.23D
    Private Const STANDARD_PRICE_Decimal As Decimal = 0D
    Private Const PEARL_PRICE_Decimal As Decimal = 345.72D
    Private Const DETAIL_PRICE_Decimal As Decimal = 599.99D
    Private Const TAX_RATE_Decimal As Decimal = 0.08D


    Sub New(ByVal carsaleprice As Decimal, ByVal tradeinallowance As Decimal)

        'Assign property values
        With Me
            .carsaleprice = carsaleprice
            .tradeinallowance = tradeinallowance
        End With
        GetAccessoriestotal()
        Getsubtotal()
        GetTax()
        GetTotal()
        GetDue()

    End Sub

    Sub New()

    End Sub

    Public Property carsaleprice() As Decimal
        Get
            Return salepricedecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property accessoriesandfinish() As Decimal
        Get
            Return accessfinishtotal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property subtotal() As Decimal
        Get
            Return subtotaldecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property salestax() As Decimal
        Get
            Return taxdecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property total() As Decimal
        Get
            Return totaldecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property tradeinallowance() As Decimal
        Get
            Return allowancedecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property amountdue() As Decimal
        Get
            Return ammountduedecimal
        End Get
        Set(ByVal value As Decimal)

        End Set
    End Property

    Public Property Stereo() As Integer
        Get
            Return STEREO_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property Leather() As Integer
        Get
            Return LEATHER_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property Navigation() As Integer
        Get
            Return NAVIGATION_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    ''' <value></value>
    Public Property Standard() As Integer
        Get
            Return STANDARD_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property Pearlized() As Integer
        Get
            Return PEARL_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property Detailing() As Integer
        Get
            Return DETAIL_PRICE_Decimal
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Sub GetAccessoriestotal()
       
        accessfinishtotal = finishdecimal + accessoriesdecimal
    End Sub

    Public Sub Getsubtotal()
        subtotaldecimal = accessfinishtotal + salepricedecimal

    End Sub

    Public Sub GetTax()
        taxdecimal = subtotaldecimal * TAX_RATE_Decimal
    End Sub

    Public Sub GetTotal()
        totaldecimal = subtotaldecimal + taxdecimal
    End Sub

    Public Sub GetDue()
        ammountduedecimal = totaldecimal - allowancedecimal
    End Sub
End Class
 
Last edited:
Also if it helps, I had already done this code when it was all together. For the check boxes and radio buttons I used (see code), I just don't know how to translate that to work with the class

VB.NET:
With me
 If .RadioButtonStandard.Checked Then
                finishdecimal = STANDARD_PRICE_Decimal
            ElseIf .RadioButtonPearl.Checked Then
                finishdecimal = PEARL_PRICE_Decimal
            ElseIf .RadioButtonDetail.Checked Then
                finishdecimal = DETAIL_PRICE_Decimal
            End If
            'find price for acessories
            If .CheckBoxStereo.Checked Then
                accessoriesdecimal += STEREO_PRICE_Decimal
            End If
            If .CheckBoxInterior.Checked Then
                accessoriesdecimal += LEATHER_PRICE_Decimal
            End If
            If .CheckBoxNavigation.Checked Then
                accessoriesdecimal += NAVIGATION_PRICE_Decimal
            End If
VB.NET:
 
Thanks :)

The project is basically this, but I have to have a class instead of having it all in one. So all calculations etc, have to be in the class - I have to instantiate objects from my class definition by calling the appropriate constructor and passing parameters.

I hope that clears things up a little bit, please let me know if I can give you any more info to help you help me :)
Thanks!
 
That helps a lot! I will read though it, do the exercise myself (so that I am familiar with all of the ins and outs) and then help you with specific areas. It shouldn't take me long to get it figured out. When is this due?
 
It is due on Thursday. Thanks a lot I really appreciate the help!

To speed it up if you need, all the code I believe was at the bottom of that page.

I think it is fairly simple, I am just not sure how to handle the check boxes and such with the class.

I'll continue to work on this as well as the rest of the assignment (that doesn't effect this part).

Thanks again!
 
That helps a lot! I will read though it, do the exercise myself (so that I am familiar with all of the ins and outs) and then help you with specific areas. It shouldn't take me long to get it figured out. When is this due?

Wait! I am sorry I was looking at that for reference, and it is the wrong link

This is the correct link, even simpler I think:
http://www.siue.edu/~dbock/cmis142/vbauto04.htm
 
Just about got a solution for you. I re-wrote your class for you:

VB.NET:
    ' use sentence capitalization for class name
    Public Class PurchaseObject

        ' Changed to Private **********
        Private _salePrice As Decimal
        Private _tradeIn As Decimal
        Private _accessories As Decimal
        Private _finish As Decimal
        ' *********************

        ' Changed to Public
        ' Accessory constants
        Public Const CD_PLAYER As Decimal = 300D
        Public Const LEATHER_INTERIOR As Decimal = 995.95D
        Public Const MAG_WHEELS As Decimal = 600D
        Public Const COMPUTER_NAV As Decimal = 2400D

        ' Finish constants
        Public Const STANDARD_FINISH As Decimal = 0D
        Public Const PEARL_FINISH As Decimal = 350.5D
        Public Const CUSTOM_FINISH As Decimal = 599.99D
        Public Const DECAL_FINISH As Decimal = 450.5D

        ' Tax Rate
        Private Const TAX_RATE As Decimal = 0.08D

        Sub New()
            MyBase.New()
        End Sub

        ' Properties
        Public Property SalePrice() As Decimal
            Get
                Return _salePrice
            End Get
            Set(ByVal value As Decimal)
                _salePrice = value
            End Set
        End Property

        Public Property TradeIn() As Decimal
            Get
                Return _tradeIn
            End Get
            Set(ByVal value As Decimal)
                _tradeIn = value
            End Set
        End Property

        Public ReadOnly Property Accessories() As Decimal
            Get
                Return _accessories
            End Get
        End Property

        Public Property Finish() As Decimal
            Get
                Return _finish
            End Get
            Set(ByVal value As Decimal)
                _finish = value
            End Set
        End Property

        ' Member methods
        Public Sub AddAccessory(ByVal acc As Decimal)
            _accessories += acc
        End Sub

        Public Sub RemoveAccessory(ByVal acc As Decimal)
            _accessories -= acc
        End Sub

        Public Sub AccessoriesReset()
            _accessories = 0D
        End Sub

        Public Function GetAccessoryFinishTotal() As Decimal
            Return _finish + _accessories
        End Function

        Public Function GetSubtotal() As Decimal
            Return (_salePrice - _tradeIn) + _accessories + _finish
        End Function

        Public Function GetTax() As Decimal
            Return Me.GetSubtotal() * TAX_RATE
        End Function

        Public Function GetAmountDue() As Decimal
            Return Me.GetSubtotal() + Me.GetTax()
        End Function
    End Class
 
Last edited:
Ok so now do I replace my .tbxTax.Text = apurchaseobject.taxdecimal.ToString("C") with the .tbxTax.Text = apurchaseobject.GetTax.ToString("C") ?

It is also throwing an error in my main form about too many arguments for apurchaseobject = New purchaseobject(Decimal.Parse(.tbxCarSales.Text), Decimal.Parse(.tbxTradein.Text))
It might just be something like the names are no longer the same...

By the way I really appreciate your help on this! I thought it was nice of you to offer, but I didn't expect you to actually go through and re-create the whole thing. Let me know if there is anything I can help you out with, I consider myself pretty good at graphic design stuff.
 
The problem you are having with your constructor is that I only defined a default constructor (no parameters). If you really want a constructor to initialize sales price and trade-in allowance then add this:

VB.NET:
Public Sub New(ByVal price As Decimal, ByVal trade As Decimal)
     _salePrice = price
     _tradeIn = trade
End Sub

I personally would leave it out, though. You don't really need those values until the compute button is pressed.
 
The problem you are having with your constructor is that I only defined a default constructor (no parameters). If you really want a constructor to initialize sales price and trade-in allowance then add this:

VB.NET:
Public Sub New(ByVal price As Decimal, ByVal trade As Decimal)
     _salePrice = price
     _tradeIn = trade
End Sub
I personally would leave it out, though. You don't really need those values until the compute button is pressed.

OK Cool.
Without that code ( even after I took out the call to the constructor) it was coming up with 0's

With the code:
VB.NET:
  'Calc the price for the sale 
        Try
            With Me
                apurchaseobject = New PurchaseObject(Decimal.Parse(.tbxCarSales.Text), Decimal.Parse(.tbxTradein.Text))

                'Calculate and format the result
                .tbxSubtotal.Text = apurchaseobject.GetSubtotal.ToString("C")

                .tbxTax.Text = apurchaseobject.GetTax.ToString("C")
                .tbxAmmDue.Text = apurchaseobject.GetAmountDue.ToString("C")
                .tbxAccess.Text = apurchaseobject.GetAccessoryFinishTotal().ToString("C")
            End With
        Catch ex As Exception
            MessageBox.Show("enter numeric data.", "VBAuto", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

It seems to work well for the most part, but it seems to be leaving out the accessories and finish...?

From looking over it, I did not see anything that should cause that. I'll continue to look
 
This should get you going. If you have questions, ask and I will explain.

VB.NET:
Public Class Form1

    Private po As PurchaseObject
    Private number_of_sales As Integer = 0
    Private total_sales_amount As Decimal = 0
    Private average_sale As Decimal = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        po = New PurchaseObject
    End Sub

    Private Sub computeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles computeButton.Click
        ' Get the sales price
        po.SalePrice = Convert.ToDecimal(Me.priceTextBox.Text)

        ' Get the trade-in value
        po.TradeIn = Convert.ToDecimal(Me.tradeinTextBox.Text)

        ' Get the options
        ' Accessories
        If Me.cdPlayerCheckBox.Checked Then
            po.AddAccessory(PurchaseObject.CD_PLAYER)
        End If
        If Me.leatherInteriorCheckBox.Checked Then
            po.AddAccessory(PurchaseObject.LEATHER_INTERIOR)
        End If
        If Me.magWheelsCheckBox.Checked Then
            po.AddAccessory(PurchaseObject.MAG_WHEELS)
        End If
        If Me.computerNavCheckBox.Checked Then
            po.AddAccessory(PurchaseObject.COMPUTER_NAV)
        End If
        ' Finish
        If Me.standardFinishRadioButton.Checked Then
            po.Finish = PurchaseObject.STANDARD_FINISH
        ElseIf Me.pearlizedFinishRadioButton.Checked Then
            po.Finish = PurchaseObject.PEARL_FINISH
        ElseIf Me.customDetailingRadioButton.Checked Then
            po.Finish = PurchaseObject.CUSTOM_FINISH
        Else
            po.Finish = PurchaseObject.DECAL_FINISH
        End If

        '*  Compute all values and display them in the appropriate text box controls located in the Totals group box. 
        '* Format ALL output as shown and right-justified.
        Me.accAndFinishTextBox.Text = po.GetAccessoryFinishTotal().ToString("C2")
        Me.subtotalTextBox.Text = po.GetSubtotal().ToString("C2")
        Me.salesTaxTextBox.Text = po.GetTax().ToString("C2")
        Me.amountDueTextBox.Text = po.GetAmountDue().ToString("C2")
        '* Disable the Calculate button; enable the Clear button. 
        Me.computeButton.Enabled = False
        Me.resetButton.Enabled = True
        '* Accumulate the total sale price for all vehicles sold, and the number of vehicles sold.
        Me.number_of_sales += 1
        Me.total_sales_amount += po.GetAmountDue()
        Me.average_sale = Me.ComputeAverage()

    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If result = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub totalsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalsButton.Click
        Dim TOTAL_SALES As String = "Total Vehicle Sales: "
        Dim TOTAL_SOLD As String = "Number Sold: "
        Dim AVERAGE As String = "Average Vehicle Price: "

        MessageBox.Show(TOTAL_SALES & Me.total_sales_amount.ToString("C2") & _
        ControlChars.NewLine & TOTAL_SOLD & Me.number_of_sales.ToString & _
        ControlChars.NewLine & AVERAGE & Me.ComputeAverage().ToString("C2"), _
        "Total, Count, Average", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function ComputeAverage() As Decimal
        If Me.number_of_sales = 0 Then
            Return 0
        Else
            Return Me.total_sales_amount / Me.number_of_sales
        End If
    End Function

    ' don't allow letters, only numbers, backspace and decimal
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tradeinTextBox.KeyPress, priceTextBox.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub resetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetButton.Click
        ' Set all text boxes blank, check boxes unchecked and radio button to standard
        Me.priceTextBox.Clear()
        Me.tradeinTextBox.Text = "0.00"

        Me.accAndFinishTextBox.Clear()
        Me.subtotalTextBox.Clear()
        Me.salesTaxTextBox.Clear()
        Me.amountDueTextBox.Clear()

        Me.cdPlayerCheckBox.Checked = False
        Me.leatherInteriorCheckBox.Checked = False
        Me.magWheelsCheckBox.Checked = False
        Me.computerNavCheckBox.Checked = False

        Me.standardFinishRadioButton.Checked = True

        ' reset PurchaseObject object
        Me.po = New PurchaseObject

        ' enable compute button
        Me.computeButton.Enabled = True
    End Sub
End Class
 
Last edited:
Back
Top