StrataFrame Forum

How can I access another form's BO in a form?

http://forum.strataframe.net/Topic9317.aspx

By Ben Hayat - 6/5/2007

I must be missing something that I can't see or reference a form or variable from another form's BO.



In my Main form, I have a BO that acts as the parent to several other BOs that are in other forms. My question is in two fold:

a) In the employee form, when I drop a BO, how can I tell it in designer that the "ParentBusinessObject" is the one in Main Form?

b) By the same token, in this employee form, how do I access the BO in the Main form and other public variables I had set.



Somehow I'm need to tell Employee form to reference to Main form.



I think Randy had the same problem, and I had given him a solution that doesn't even work for myself.



Thanks!
By Ben Hayat - 6/5/2007

Ok, I went ahead and approached it this way, and tell me if I'm on right direction.



a) In main form, I set the MainBO.modifier=Public



b) in employee form.laod, I created an instance of main form:

MainForm F1=new MainForm();



c) Now when I use F1. I can see my variables and my public BO.



d) In employeeBO.ParentBusinessObject=F1.MainBO;



Is this the proper way of assigning ParentBusinessObject in child form at load time?



Thanks SF Team!
By Greg McGuffey - 6/5/2007

I'm not sure of the best way (which of course depends on the application), but I'd look into Shared DataTables property, so you could just share the data between the parent BO on the main form and an instance of that same BO dropped on the form.


By Ben Hayat - 6/5/2007

Greg McGuffey (06/05/2007)
I'm not sure of the best way (which of course depends on the application), but I'd look into Shared DataTables property, so you could just share the data between the parent BO on the main form and an instance of that same BO dropped on the form.




Hey Greg; How are you?



What I want to do, is to have one parent BO in the main form and in other forms, the child can hook up only to that one. I may have to take it in different approach.



I hope SF team get to see this thread!



Thanks!


By Greg McGuffey - 6/5/2007

Well, I'd still look into Shared Datatables or into BO translations.



Have you thought of maybe making a static (shared in VB) class with a property to hold this BO? I.e. rather than dropping the BO on the main form, configure this static class with the BO. Then you could set that BO in any form you wanted using the BusinessObjects.Add() method of the form.



E.g.

// Static class that holds the instance of the BO that will be used.

public static class MasterBOAccessor

{

private static MyMasterBO _businessObject;

public static property MyMasterBO BusinessObject()

{

get

{

return _businessObject;

}

set(MyMasterBO value)

{

_businessObject = value;

}

}

}



// Main form class that reference the main BO

public class MyMainForm : Microfour.StrataFrame.UI.Windows.Forms.StandardForm

{

private sub form_load(object sender, EventArgs e)

{

this.BusinessObjects.Add(MasterBOAccessor.BusinessObject);

}

}



// Child form that will use the main bo as the parent of a BO loaded onto

// this form (myChildBO)

public class MyChildForm : Microfour.StrataFrame.UI.Windows.Forms.StandardForm

{

private sub form_load(object sender, EventArgs e)

{

this.myChildBO.ParentBusinessObject = MasterBOAccessor.BusinessObject;

}

}




NOTE: I don't know that this will work, but I think it should. Also, if you see things in this code that looks wrong...well it probably is, I don't know C# that well Tongue


By Ben Hayat - 6/5/2007

Greg; Thank you very much for your time and effort;



I'll be getting up 5:00 AM tomorrow morning to drive to Orlando for TechEd07. After I get back, I'll look into this as well. Perhaps SF Team will also add in some suggestions.



I think to summarize my point, is that, I need one BO (StoreBO) that should be set in the mainForm, with proper filter. Then In Employee, customer, tax, Order, etc. forms, all those BOs should use that StoreBO as their parentBusinessObject.



I tried it today, but when I set the EmployeeBO.ParentBusinessObject in Employee FormLOad Event, I then get error that the Employee.ParentBO is Null.



That's the idea!
By StrataFrame Team - 6/6/2007

By my guess, it looks like the main form is going to be creating and showing the instances of the other forms.  There isn't a way within the designer to tell a component on one form to reference a component on another form (the form designer does not know that the other form exists.  So, you will have to set the parent programmatically.  Your best bet would be to add a parameter to your child forms' constructors that accepts the parent business object; then set the ParentBusinessObject property on the child BOs after the InitializeComponent has been called.  Then, all you need to do is pass over the parent business object when you create new instances of the child forms.
By Ben Hayat - 6/6/2007

Ben Chase (06/06/2007)
By my guess, it looks like the main form is going to be creating and showing the instances of the other forms. There isn't a way within the designer to tell a component on one form to reference a component on another form (the form designer does not know that the other form exists. So, you will have to set the parent programmatically. Your best bet would be to add a parameter to your child forms' constructors that accepts the parent business object; then set the ParentBusinessObjectproperty on the child BOs after the InitializeComponent has been called. Then, all you need to do is pass over the parent business object when you create new instances of the child forms.




Ben, I'll look into this further probably this weekend and if I have more questions, I'll ask next week. But you gave me a good starting point!

Thanks!