How to organize parent and child .Net forms.

Posted by PeterWokke on 04-Nov-2016 02:12

Dear All,

What is the best practice to organize calling methods between parent and child forms.

For example I would like to call a method in the parent form.

Should I forward the object handle of this parent to the child when I initialize this or is this automatically done and can I obtain this object handle within each child form that opens within the application.

Kind regards,

Peter

All Replies

Posted by bronco on 04-Nov-2016 02:20

Well, if you just pass the object reference to the child you create a rather strong dependency. Say, the child needs to run the Update method in the parent you can do two things: create an IUpdatable interface (with the Update method), implement the IUpdatable in the parent and pass the object reference from parent to child as an IUpdatable. Then you child form can be used for any parent which implement IUpdatable. Second possibility is events. Subscribe the parent to the update events from the child. (I'm using Update as an example obviously).

Posted by Laura Stern on 04-Nov-2016 07:10

Huh?  A parent and child class are one class.  Any method that is public or protected in the parent is callable from the child with no object reference needed (though you can use THIS-OBJECT if you want).  It is just like calling a method in yourself.

Posted by PeterWokke on 04-Nov-2016 09:26

Hello Laura,

The parent class (.Net form) instance the client class which is another .Net form.

The child is not an inherited class of the parent in this case.

Hello Bronco,

Setting the Parent Object as Property in the Child .Net form would not be that difficult.

What kind of class definitions should I use for this?

Then I could assign  THIS-OBJECT of the parent to the Object property into the child.

Calling the procedure like ParentFrom:UpdateDataset(input-output dataset dsData).

Regards,

Peter.

Posted by Peter Judge on 04-Nov-2016 09:40

 
Bronco’s suggestion about interfaces and events is a better approach though since it (a) strongly-types the kinds of interactions you expect the objects to have and (b) decouples them, allowing you to reuse the interfaces elsewhere (ie not just in the parent and child, but maybe in a helper/manager-type).

Posted by bronco on 04-Nov-2016 09:42

Sure that's possible, but my point is that in OO you try keep the dependencies between classes as little as possible so they can be reused easier. One other thing, why don't you pass the dataset from parent to child by-reference?

Posted by bronco on 04-Nov-2016 09:44

PS. I was reacting to the OP, not Peter Judge's post :-)

Posted by PeterWokke on 04-Nov-2016 09:56

Bronco,

Passing the datasets by reference is an option as well.

Need to pass more then one in this case.

Peter

Posted by PeterWokke on 04-Nov-2016 10:15

Bronco,

I have tried to pass them by reference.

But this did not populate the main data set with referential names.

Second time a passed them as input data sets without by reference.

After that the data-set completed with the names.

This helped to solve the issue. But will look into the links Peter Judge mentioned.

Kind regards,

This thread is closed