Hello all, Suppose I have Class_A with 10 properties and Class_B which overrides Class_A and adds 4 properties.
So Class_B has 14 properties. (I have this working)
But in code I have an instance of Class_A and need to create an instance of Class_B and for this instance add the 4 properties.
My program is a invoice program and Class_A is a product and Class_B is an invoice line.
I am sorry I wasn’t clear.
Class_A is a product. (in a listboxRow.tag) the user selects the listboxRow and now I need to add the Class_A into a Class_B (which contains the extra properties: Price and Quantity)
I believe by “add” you mean having the values you already have in class A object, inside class B object. I think you can achieve this by creating a “copy constructor” method
That is exactly what I mean.
But I googled Copy Constructor and could only find something in Monkeybread software.
Do you have a tip or some link or some help, please.
May the object A die while B still around? If not, why not just linking both and when inspecting B you could just read the linked A.
Public ClassA
Public val1 As String
Public val2 As String
End Class
Public ClassB
Public val1 As Integer
Public val2 As Integer
Public A As ClassA
Public Sub Constructor(objA As ClassA)
A = objA
End Proc
End Class
Var A As New ClassA
A.val1 = "aaaa"
Var B As New ClassB(A)
MessageBox B.A.val1 // show "aaaa"
This is even more beautiful code.
I think this would make my code easier to understand and read after a few years.
Thank you.
(As a joke → To other readers please do not give any other better solutions. This is the 4th time I rewrite this code. Two didn’t work, one is the above answer from Ant and now this one)
I guess his design allows his choice, as holding the reference to it was warned before as you see. As soon as B dies too, A will released, since its lifecycle has ended, where all references to it are gone.