Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call function on inherited form from another form

Status
Not open for further replies.

daves22406

Technical User
Oct 11, 2003
5
US
I have a form (NewForm) that is inherited from a base form (BaseForm). There are several types of NewForm, all of which inherit from BaseForm. Each NewForm contains a method called Calculate. When I click on a button on NewForm, it opens another form (GraphForm) and passes NewForm as a property (PassedForm) of GraphForm so that the Calculate method can be used (along with several other properties of NewForm). My question is, how do I call the Calculate method of NewForm from GraphForm? The code below is obviously not complete, but you should be able to get the idea:


in BaseForm:

Protected Overridable Function Calculate() as Single
' override in NewForm
End Function

in NewForm:

Protected Overrides Function Calculate() as Single
'do calculations here
End Function


Button_Click event:

Dim frm as New GraphForm
frm.PassedForm = Me
frm.Show


in GraphForm:
Private Property PassedForm as BaseForm

Dim answer as single = PassedForm.Calculate


When Calculate is called from the GraphForm, the Calculate function in BaseForm is exectuted. Is this because GraphForm.PassedForm is declared as type BaseForm, so only public methods in BaseForm are available? How would I execute the Calculate function in NewForm from GraphForm? Is there a better way to do this than passing NewForm to GraphForm?

 
if you are using more the same function over again, declare it as a shared routine. Or put the function in a module.

When your function is based on parameters of the forms, which I suspect, you might consider to capture all the parameters in a class, and pass these parameters to your function. This prevents having to drag heavy' form objects to be passed from one form to another.

The gap between theory and practice is not as wide in theory as it is in practice.
 
Thanks for responding. If I declare the Calculate Function in BaseForm as MustOverride then everything seems to work fine.

In BaseForm:
Protected Overrides Function Calculate(ByVal Params() As Single, ByRef BaseFR As Single) As CalcRtn

There are many Subforms that are inherited from BaseForm, and each has a different calculate function, so it's not just one function that I can place in a module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top