Reputation: 519
I dont know if is posible, but I need to get recalculated properties of base class after passing parameter to it. Here is an example code:
Public Class BaseClass
Public Property Initial As Integer
Public Property Coeficient As Integer
Public Property Multiplier As Integer = Coeficient * Initial
Public Sub New()
Me.New(0, 1) ' default initialization
End Sub
Public Sub New(ByVal Value1 As Integer, ByVal Value2 As Integer)
Me.Initial = Value1
Me.Coeficient = Value2
End Sub
End Class
Public Class DerivedClass
Inherits BaseClass
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal Value As Integer)
MyBase.New(Value, 3) 'sets the coeficient to 3 and the Initial whatever is passed to it
End Sub
End Class
Public Class TestClas
Public Sub TestDerivedClass()
Dim d As New DerivedClass(5)
Dim result As Integer = d.Multiplier
MsgBox(result) ' should be 15, but is not, because Multiplier need to be re-initialized somehow
End Sub
End Class
I understand where the problem is, but what I need is to obtain kind of a dynamic base class to be inherited differently in multiple derived classes after passing different parameters to it.
Thank you in advance.
Upvotes: 0
Views: 81
Reputation: 19641
The value of your Multiplier
property doesn't get updated. If you want it to always return the multiplication of the other two values, you should convert it to a read-only property which will return the product.
Replace the following line:
Public Property Multiplier As Integer = Coeficient * Initial
with:
Public ReadOnly Property Multiplier() As Integer
Get
Return Coeficient * Initial
End Get
End Property
Hope that helps.
Upvotes: 2
Reputation: 2182
Multiplier is a property, so it's such a Field of your class. When initialize, value is given.
You are expecting a "function behavior".
You can do many things, I don't know what you really need. Change Multiplier :
Simple Example:
Public ReadOnly Property Multiplier As Integer
Get
Return Coeficient * Initial
End Get
End Property
This will give you the "automatic behavior" you are expecting
Upvotes: 0