Invoice class error

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2766 Views - Last Post: 15 April 2012 - 07:32 PM Rate Topic: -----

#1 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Invoice class error

Posted 15 April 2012 - 03:46 PM

"Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables--a part number (type String), a part description (type String), a quantity of the item being purchased (type Integer) and a price per item (Type Integer). Your class should have a constructor that initialize for each of the four instance variables. Provide a property for each instance variable. If the quantity is not positive it should be set to zero. If the price per item is not positive it should be set to zero."

I attached the code I have. obj is underlined and says "Argument not specified for parameter 'Invoice4' of the 'Public Sub New (Invoice1 As Object, Invoice2 As Object, Invoice3 As Object, Invoice4 As Object)' and so the program won't run. What am I doing wrong?




Public Class Form1

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim obj1 As New Invoice(Val(TextBox1.Text))
        TextBox1.Text &= obj1.Invoice1 & vbNewLine
        TextBox1.Text &= obj1.Invoice2 & vbNewLine
        TextBox1.Text &= obj1.Invoice3 & vbNewLine
        TextBox1.Text &= obj1.Invoice4 & vbNewLine
    End Sub
End Class

Public Class Invoice
    Public Property Invoice1 As Double
    Public Property Invoice2 As Double
    Public Property Invoice3 As Double
    Public Property Invoice4 As Double

    Public Sub New(ByVal Invoice1, ByVal Invoice2, ByVal Invoice3, ByVal Invoice4)
        Invoice1 = "Original invoice information" & "Part number: 1234" & "Description: Hammer" & "Quantity: 2" & "Price Per Item: 14"
        Invoice2 = "Updated invoice information" & "Part number: 001234" & "Description: Yellow(Hammer)" & "Quantity: 3" & "Price Per Item: 19"
        Invoice3 = "Original invoice " & "Part number: 5678" & "Description: Paint(Brush)" & "Quantity: 0" & "Price Per Item: 0"
        Invoice4 = "Updated invoice information" & "Part number: 5678" & "Description: Paint(Brush)" & "Quantity: 3" & "Price Per Item: 9"
    End Sub
End Class

This post has been edited by AdamSpeight2008: 15 April 2012 - 04:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Invoice class error

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Invoice class error

Posted 15 April 2012 - 04:28 PM

In the New constructor for your class you have specified 4 parameters (arguments) and you are only using 1.
Ie:
Sub New (ByVal p As String, ByVal foo As Integer)

I call that like this:
Dim s As New MyClass("lol jk")

I have only specified one item out of the required two. If they are not required, you can instantiate them as Optional ByVal and instead give them a default value to use if none are specified.

This post has been edited by AdamSpeight2008: 15 April 2012 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Re: Invoice class error

Posted 15 April 2012 - 04:32 PM

I don't understand your example
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Invoice class error

Posted 15 April 2012 - 04:38 PM

Hmm. Ok. I'll put it into your context.
Think of the parameters in the New sub as mandatory fields on a web form. Without being filled in, you cannot proceed.
Public Sub New(ByVal Invoice1, ByVal Invoice2, ByVal Invoice3, ByVal Invoice4)

You have 4 fields in your 'web form'. When you do this:
Dim obj1 As New Invoice(Val(TextBox1.Text))

You only end up filling 1. (invoice1 with the value of TextBox1.Text)
Since only 1 of the 4 fields has been filled you cannot proceed.
Hopefully this is a bit clearer? :)
Was This Post Helpful? 0
  • +
  • -

#5 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Re: Invoice class error

Posted 15 April 2012 - 04:42 PM

So do I have to add 3 more dim? or is there something wrong with the sub new? I'm sorry I'm a bit confused.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Invoice class error

Posted 15 April 2012 - 04:46 PM

When you call new is the problem. If you did this:
Dim foo As New Invoice(5, 6, 7, 8)

It would end up working because you have met the required values (fields in relation to my previous web form example).
Was This Post Helpful? 0
  • +
  • -

#7 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Re: Invoice class error

Posted 15 April 2012 - 04:52 PM

Thank you! I appreciate it. Now I just have one more problem. I fixed my code to look like this but now the val(textbox1.text) is underlined and it says value of type double cannot be converted.

P
ublic Class Form1

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim obj1 As New Invoice(1, 2, 3, 4)
        obj1 = Val(TextBox1.Text)
        

        TextBox1.Text &= obj1.Invoice1 & vbNewLine
        TextBox1.Text &= obj1.Invoice2 & vbNewLine
        TextBox1.Text &= obj1.Invoice3 & vbNewLine
        TextBox1.Text &= obj1.Invoice4 & vbNewLine
    End Sub
End Class

Public Class Invoice
    Public Property Invoice1 As Double
    Public Property Invoice2 As Double
    Public Property Invoice3 As Double
    Public Property Invoice4 As Double

    Public Sub New(ByVal Invoice1, ByVal Invoice2, ByVal Invoice3, ByVal Invoice4)
        Invoice1 = "Original invoice information" & "Part number: 1234" & "Description: Hammer" & "Quantity: 2" & "Price Per Item: 14"
        Invoice2 = "Updated invoice information" & "Part number: 001234" & "Description: Yellow(Hammer)" & "Quantity: 3" & "Price Per Item: 19"
        Invoice3 = "Original invoice " & "Part number: 5678" & "Description: Paint(Brush)" & "Quantity: 0" & "Price Per Item: 0"
        Invoice4 = "Updated invoice information" & "Part number: 5678" & "Description: Paint(Brush)" & "Quantity: 3" & "Price Per Item: 9"
    End Sub
End Class

This post has been edited by AdamSpeight2008: 15 April 2012 - 05:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Invoice class error

Posted 15 April 2012 - 05:04 PM

I didn't mean for you to take my example literally! ROFL!
You're trying to set an object of type Invoice to type Double!
The code I gave you was just to show you a working idea. You're supposed to adapt it.
Let's assume you have 4 TextBoxes. You were using TextBox1 already. Try this:
Dim I As New Invoice(Val(TextBox1.Text), Val(TextBox2.Text), Val(TextBox3.Text), Val(TextBox4.Text))

Using the code I gave you you would receive the same results regardless of what you inputted.
And those public properties aren't done right, I think. They should be of type String and you really should use proper private variables.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Invoice class error

Posted 15 April 2012 - 05:06 PM

Will you two start using the code tags.
Was This Post Helpful? 0
  • +
  • -

#10 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Invoice class error

Posted 15 April 2012 - 05:07 PM

First of all, please use code tags when you post code(there is a button in the editor).

Now to the first problem, DimitriV explained you the error, but you should have a look at Optional parameters

About your second problem. It seems that you are confused about OOP. The problem in your code is that you try to assign a double value to an object. An object is an instance of a class(in your case, obj1 is the object). The assignment between two objects can be done if both objects are the same type or the object you try to assign has the type derived from the type of the object you assign to(it could be a little confusing).
Remaining only with your problem, I think you want to assign the value from the textbox to one of the properties of type double. This means:
obj1.Invoice1 = val(textBox1.Text)


I would recommand you to read something about OOP because you will enter in some concepts you will find very hard to understand only by coding.
Was This Post Helpful? 0
  • +
  • -

#11 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Re: Invoice class error

Posted 15 April 2012 - 05:20 PM

Thank you for bearing with me. So
obj1.Invoice1 = val(textBox1.Text)
is not the problem?
Was This Post Helpful? 0
  • +
  • -

#12 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Invoice class error

Posted 15 April 2012 - 05:24 PM

it's not a problem. The problem is
obj1 = Val(textbox1.Text)


as I explained in my previous post.

Plus, now I see. On the constructor of the Invoice class, you assign a string value to a variable of type double.
Invoice1 = "Original invoice information" & "Part number: 1234" & "Description: Hammer" & "Quantity: 2" & "Price Per Item: 14"


You may want to also rethink this.
Was This Post Helpful? 0
  • +
  • -

#13 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Invoice class error

Posted 15 April 2012 - 05:25 PM

I'm thinking it would be best if you declared Invoice1-4 as String - due to the code in the New sub where you add String value to them.
Was This Post Helpful? 0
  • +
  • -

#14 ekb9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-March 12

Re: Invoice class error

Posted 15 April 2012 - 05:33 PM

But don't I need to change
 Dim obj1 As New Invoice(Val(TextBox1.Text))
to fill all four fields?

I changed my Invoice 1-4 as strings now. Now just working with my original issue which I still don't understand.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Invoice class error

Posted 15 April 2012 - 05:35 PM

Wouldn't you take the hint of four variable with names Invoice1, Invoice2, Invoice3, Invoice4 suggest you need a collection class, or a custom class.

I would go towards the custom class approach, to utilise type safety.
' A Class to encompass the notion of an Invoice.'
Public Class Invoice

End Class

' A class to encompass the notion of a collection of Invoice. '
Public Class Invoices
  Private _Invoices As New List(Of Invoice)

  Public ReadOnly Property Invoices As IEnumerable(Of Invoice)
    Get
      Return _Invoices.AsEnumerable
    End Get
  End Property
End Class



The cause of the original problem, is that not every possible String is a valid Double. You need to check by using something like Double.TryParse

This post has been edited by AdamSpeight2008: 15 April 2012 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2