Peter Allen
asked on
References to Objects: Forms/Classes
Experts,
using the following code I can make references from one Form to another:
frmMain Code:
Creating the reference from a menu option in frmMain:
In the frmDataGridViewProject_Edi tor Form:
and...
Once the forms are opened I can make the needed references. I can create Public Properties and variables and they work.
Right now once I select the menu option from frmMain I open the Editor form and create variables that can be passed to the Catalog via frmMain. This works, but I feel it is kind of inconsistant.
What I would like to do is to use a Class. Here is how I'd like to do it.
1. In frmMain I want to have the Public variables that will hold the Class and Forms for the section of the application I want to work with. In this case, Project.
2. Once I select a menu item on the Project menu subroutines or functions in the DataGridViewProject class get called to create the references to the Forms and Public properties that can be shared among the forms I define.
3. Once I have completed working with the Project the variables would be reset once the Project Form(s) were closed.
I'm just not sure how exactly to code this. I believe it can be done. The advantage would be, in my view a more consistant way to declare Properties/Variables and Shared access to the specific open forms. With this approach I can easily make shorter references to a Form or Class.
'm not sure how involved this would be so I gave it a higher number of points for assistance.
using the following code I can make references from one Form to another:
frmMain Code:
'Define Public variables for Editor/Catalog Form References
Public frmProjectEditor As frmDataGridViewProject_Editor = Nothing
Public frmProjectCatalog As frmDataGridViewProject_Catalog = Nothing
Creating the reference from a menu option in frmMain:
'Define Form References
frmProjectEditor = New frmDataGridViewProject_Editor(Me)
frmProjectCatalog = New frmDataGridViewProject_Catalog(Me)
In the frmDataGridViewProject_Edi
'Define reference to Parent Form
Private f_frmMain As frmMain
and...
Public Sub New(ByVal f_frmMain As frmMain)
Call InitializeComponent()
frmMain = f_frmMain
End Sub
Once the forms are opened I can make the needed references. I can create Public Properties and variables and they work.
Right now once I select the menu option from frmMain I open the Editor form and create variables that can be passed to the Catalog via frmMain. This works, but I feel it is kind of inconsistant.
What I would like to do is to use a Class. Here is how I'd like to do it.
1. In frmMain I want to have the Public variables that will hold the Class and Forms for the section of the application I want to work with. In this case, Project.
2. Once I select a menu item on the Project menu subroutines or functions in the DataGridViewProject class get called to create the references to the Forms and Public properties that can be shared among the forms I define.
3. Once I have completed working with the Project the variables would be reset once the Project Form(s) were closed.
I'm just not sure how exactly to code this. I believe it can be done. The advantage would be, in my view a more consistant way to declare Properties/Variables and Shared access to the specific open forms. With this approach I can easily make shorter references to a Form or Class.
'm not sure how involved this would be so I gave it a higher number of points for assistance.
Agreed with Idle_Mind. I don't think you need holding form instances in memory, unless they take a wile to load (for example, webbrowser control with static url). U can use class with shared methods/properties or modile with public variables/methods (module looks like a 'shared' class). You can add shared/public method Reset to class/module to reset values. The only drawback is that non-instanced objects cannot produce events. I use following trick - one of shared properties points to main form control (say, hidden textbox) and child form send messages to main form via this control text while main form monitoring TextChanged event.
ASKER
Idle_Mind,
The use of SHARED in the class WOULD make things MUCH easier. With respect to using Shared in Public Functions or Subs of the class what would I do if two users accessed the class? How would this situation not cause a conflict between two or more users using the routines?
Once I finished using the Shared resource would its memory be freed up?
The use of SHARED in the class WOULD make things MUCH easier. With respect to using Shared in Public Functions or Subs of the class what would I do if two users accessed the class? How would this situation not cause a conflict between two or more users using the routines?
Once I finished using the Shared resource would its memory be freed up?
Things that are Shared have a lifetime of the app itself. As such, if you store a reference as a Shared member in a Class, it won't go out of scope and be garbage collected unless you explicitly set the reference to Nothing or otherwise cause it to Dispose().
As far as multiple users, can you explain more? How are two users going to be accessing your app at the same time?
As far as multiple users, can you explain more? How are two users going to be accessing your app at the same time?
ASKER
Idle_Mind,
So if I want to declare variables for two forms to be open I could write something like this in the class:
Public Class clsExample
Public Sub subOpenProject
Dim f_Form as new frmEditor
Dim f_Catlg as new frmCatalog
Shared strProperties as new clsCatalogVariables
f_Form.Show(f_Form) <-- Creating the reference to the Form
f_Catlg.Show(f_Catlg) <-- Creating the reference to the Form
End Sub
End Class
So from this class I can open the two forms and shared their properties by reference and access the Shared properties of clsCatalogVariables as required.
From a menu I could make the following call to the class to open the desired forms:
Call clsExample.subOpenProject
So if I want to declare variables for two forms to be open I could write something like this in the class:
Public Class clsExample
Public Sub subOpenProject
Dim f_Form as new frmEditor
Dim f_Catlg as new frmCatalog
Shared strProperties as new clsCatalogVariables
f_Form.Show(f_Form) <-- Creating the reference to the Form
f_Catlg.Show(f_Catlg) <-- Creating the reference to the Form
End Sub
End Class
So from this class I can open the two forms and shared their properties by reference and access the Shared properties of clsCatalogVariables as required.
From a menu I could make the following call to the class to open the desired forms:
Call clsExample.subOpenProject
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just 1 note: Shared is a bit different in Win app and Web app. In win app 2 instances of app doesn't interfere shared members - they are different for each application, while for web app there is only 1 app on server with miltiple users and shared variables are same for all sessions
ASKER
Idle_Mind,
That looks great. I am going to try this one out. In regards to multiple users... If an app was created that can be accessed by multiple users (at the same time) how would I handle the following situation with respect to the variables initialized for a Form.
Situation: A form with SHARED members is opened for User 1. User 1 is working on the form and the SHARED variables are being updated. Nolw User 2 openes the same form. What would happen in this situation? Would a new instance already be created for User 2 so as not to conflict with the variables/properties used by User 1?
That looks great. I am going to try this one out. In regards to multiple users... If an app was created that can be accessed by multiple users (at the same time) how would I handle the following situation with respect to the variables initialized for a Form.
Situation: A form with SHARED members is opened for User 1. User 1 is working on the form and the SHARED variables are being updated. Nolw User 2 openes the same form. What would happen in this situation? Would a new instance already be created for User 2 so as not to conflict with the variables/properties used by User 1?
>>Now User 2 openes the same form<<
Before opening 'same' form user 2 must start NEW instance of application.
Or you mean:
1. User 1 make some changes on form and went away for coffee
2. User 2 came to same machine and continue???
Before opening 'same' form user 2 must start NEW instance of application.
Or you mean:
1. User 1 make some changes on form and went away for coffee
2. User 2 came to same machine and continue???
http://msdn.microsoft.com/en-us/library/zc2b427x(VS.80).aspx
Members that are Shared can be accessed from anywhere and do not need an instance of that class.
For instance:
Open in new window
When can access "Data" from ANYWHERE by simply preceding it with the Class name of "MyData":
Open in new window
Note that we did NOT need to create an instance of "MyData" and store the reference anywhere.