One of the things that is sort of hard to do is change the value of
parameters to constructors in my classes. Take following, very simple
example:
PublicClass BaseClass
PublicSubNew(ByVal
param AsInteger)
Console.WriteLine(param)
EndSub
EndClass
PublicClass
DerivedClass
Inherits
BaseClass
PublicSubNew(ByVal
param AsInteger)
MyBase.New(param)
EndSub
EndClass
Now if I want to change the value of param in the constructor in the
DerivedClass I can't simply add change it's constructor to:
PublicSubNew(ByVal
param AsInteger)
param = param + 1
MyBase.New(param)
EndSub
This code won’t compile because the call to the base constructor must be the
first line of code.
However it turns out you can include a call to a shared function in the call
to the base constructor. This allows you to execute any code you want when
the objects is instantiated before the base class constructor is called and
gives you ample opportunity to change the value passed to the base class.
PublicClass
DerivedClass
Inherits
BaseClass
PublicSubNew(ByVal
param AsInteger)
MyBase.New(ChangeParam(param))
EndSub
PrivateSharedFunction
ChangeParam(ByVal param
AsInteger) AsInteger
Return param
+ 1
EndFunction
EndClass
So what restriction do you have? Well the object isn’t instantiated yet so
the function needs to be shared and you cannot pass the object reference to
it and set any properties/fields.
|