0

I have 2 class in VB.net (Item Class and Tax Class). The Item class calls the Tax class in the form of an array.

See Class (Item-TaX)

Public Class Item
    Public Property taxes As Tax()
    Public Property code As String
End Class

Public Class Tax
    Public Property taxCode As String
    Public Property taxAmount As Integer
End Class

I need to add data to the 2 classes to build a JSON file

I'm doing it my way of thinking as follows:

Dim TaxProduct As New Tax
Dim Product As New Item
TaxProduct.taxCode = "01"
TaxProduct.taxAmount = 1000

Even there is going perfectly and the data is added in the class TaxProduct

Product.taxes = TaxProduct  'This line generates an error
Product.code = "10"

I thank you can help me

I hope you can explain to me how to enter the data in the class

2
  • Is the intention to be able to hold one or more Tax instances for an Item? If so, consider changing from an Array to a List: Public Property taxes As New List(Of Tax) Then you can add to the List with: Product.taxes.Add(TaxProduct). Commented Feb 3, 2023 at 19:07
  • You're welcome for the edit. Did you also see my COMMENT above though? It has a possible solution for you. Commented Feb 3, 2023 at 19:23

1 Answer 1

0

A few changes that woudl be worth considering to your class structure below.

  1. You seem to be storing a dollar amount as an integer. This is bad practice as money is frequently dollars and cents. Consider making that a decimal as below.
  2. If you are dynamically adding tax objects to your item taxes array, you should use a List(of Tax)
  3. I can't confirm here, however, it appears that you are storing a lot of int as string. (think code and tax code). Will those always be an integer value? Would you be better off using an enum of some sort instead. Food for thought.
    Public Class Item
        Public Property taxes As New List(of Tax)
        Public Property code As String
    End Class

    Public Class Tax
        Public Property taxCode As String
        Public Property taxAmount As Decimal
    End Class

After making the changes above, your code to new up and fill tax items into your product would look something like the below. Note the use of With is a VB exclusive and is imo just syntactical sugar. It is not an option in most other languages so use it with discretion.

    Dim Product As New Item with {.code = "10" }
    Dim TaxProduct As New Tax with {
        .taxCode = "01",
        .taxAmount = 1000
    }
    
    Product.taxes.add(TaxProduct)
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, just change to New List(of Tax) and add Product.taxes.add(TaxProduct)
Code wise, sure, though its well worth doing some discovery on the other pieces as they will inevitably cause an issue, particularly the taxAmount field being an integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.