Of course I could keep a list of all the instances and iterate over the list to update all the percentages, but is there a more elegant way of doing this?
Shared methods/properties know nothing about a specific instance…
Use a shared Factory Method in conjunction with a private constructor (so instances can only be created by the factory method) to create an instance and have the factor method register it with a Private Shared array. Create a destructor for the class that removes it from that Array.
Shared methods/computed properties could then work on the array to do calculations
Property Amount As Double
Get
Return mAmount
End Get
Set
mAmount = value
sTotal = sTotal + value
End Set
End Property
Property Percentage As Double
Get
Return mAmount / sTotal * 100
End Get
End Property
Private Shared sTotal As Double
?
Shared Property Total As Double
Get
Return sTotal
End Get
End Property
End Class[/code]
Caution:
With the Double data type you will get in trouble, you’re probably better of using Currency or Integer for (currency) amounts.
With that kind of solution you can’t have more than one “sets” of instances, since sTotal is like a global property. This means you can’t use it in two different windows for example.
[quote=116656:@Eli Ott]
Property Percentage As Double
Get
Return mAmount / sTotal * 100
End Get
End Property.[/quote]
Ah, yes - I see where I went wrong. I tried to set the percentage for later use but that is unnecessary.
[quote]Caution:
With the Double data type you will get in trouble, you’re probably better of using Currency or Integer for (currency) amounts.
With that kind of solution you can’t have more than one “sets” of instances, since sTotal is like a global property. This means you can’t use it in two different windows for example.[/quote]
It’s not currency but amount of peptide but the point is well taken.