We know when a class inherits from another class, it gets all the member of that parent class. But how does parent class can access that child without having to create a object of that child ?
Printable View
We know when a class inherits from another class, it gets all the member of that parent class. But how does parent class can access that child without having to create a object of that child ?
Here's what I am trying to do. Please let me know if this is the correct way of doing this.
I have a base Class called 'FormManager' which inherits from System.Windows.Forms.Form
This class is suppose to build the screen dynamically based on a parameters to its constructor. What that parameter says is whether to build the screen for data entry or to build it for Searching.
Now I am writing two more classes .. FormBuilder and SearchBuilder. Both inheriting from FormManager. Now FormManager have all the methods for loading controls on the form dynamically. So if these two classes are inheriting from FormManager they will by default get those methods. But how do I invoke the Child classes based on Constructor Parameter of FormManager, so that If parameter says Build the Search screen it automatically invokes the SearchBuilder class. I do not want to create there objects. Just based on parameter it should invoke the respective class. Is there a better way ??
Never seen or heard of this scenario before !Quote:
Originally posted by spoiledkid
But how does parent class can access that child without having to create a object of that child ?
Okay, forget it, just tell me ... I need the base class object in one of the childs. How Do I get the form object.Quote:
Originally posted by Pirate
Never seen or heard of this scenario before !
I know MyBase is there. But how Do I get the object from Mybase. It always says you have to type . after mybase.
I want to use this in the child class ..
Ctype(Mybase, Form) .. to get the base class instance .. I have to pass this as a parameter to a function ...
Umm , why don't you create instance of the base class and use in CType function ?
Thats another option, even though less elegant. Does this requirements never happened before or what ? I would think it should be common enough to get the instance of the base class.Quote:
Originally posted by Pirate
Umm , why don't you create instance of the base class and use in CType function ?
OK, so let me get this straight, you have a class FormBuilder that inherits from FormManager. You have another class SearchBuilder that also inherits FormManager. So, what you want to know is how to call FormManager with the right constructor depending on if the current class is FormBuilder or SearchBuilder. Am I right so far? If so, keep reading, if not, then stop right there and ignore the rest.Quote:
Originally posted by spoiledkid
Here's what I am trying to do. Please let me know if this is the correct way of doing this.
I have a base Class called 'FormManager' which inherits from System.Windows.Forms.Form
This class is suppose to build the screen dynamically based on a parameters to its constructor. What that parameter says is whether to build the screen for data entry or to build it for Searching.
Now I am writing two more classes .. FormBuilder and SearchBuilder. Both inheriting from FormManager. Now FormManager have all the methods for loading controls on the form dynamically. So if these two classes are inheriting from FormManager they will by default get those methods. But how do I invoke the Child classes based on Constructor Parameter of FormManager, so that If parameter says Build the Search screen it automatically invokes the SearchBuilder class. I do not want to create there objects. Just based on parameter it should invoke the respective class. Is there a better way ??
Seems to me that in the constructor for FormBuilder, you *know* you are in FormBulder, so you would just call the appropriate builder. Then again, I haven't exported too far with this kind of capabilites, so I may be completely off base. Technically you can't get to the child via the parent because the parent doesn't know about the child; IE: Your FormManager is ignorant of FormBuilder and SearchBulder, but the child does know about the parent, since FormBuilder and SearchBuilder inherit from FormManager, they must know some thing about it.
TG
thanks guys. I was doing a conceptual mistake here and finally sorted it out myself.
As tech said, its the FormBuilder or SearchBuilder that needs to be invoked not the FormManager. However, to get the instance of FormManager in FormBuilder/SearchBuilder .. I had to write this in FormManager and call from Child as Mybase.Instance
VB Code:
Protected ReadOnly Property Instance() As Object Get Return Me End Get End Property
Is there no elegant way to get the instance of Parent class in child classes ????
Tx.
Here is another idea. (Set LoadForms as the startup form)
VB Code:
Public Class LoadForms Inherits Windows.Forms.Form Friend WithEvents Button1 As System.Windows.Forms.Button Public Sub New() InitializeComponent() End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FM As FormManager If MsgBox("Load for search?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then FM = New FormManager.SearchBuilder Else FM = New FormManager.FormBuilder End If FM.ShowDialog() End Sub Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(0, 0) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Click" ' 'FormManager ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Button1) Me.Name = "FormManager" Me.ResumeLayout(False) End Sub End Class '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Class FormManager Inherits System.Windows.Forms.Form Friend WithEvents Label1 As System.Windows.Forms.Label Public Sub New() InitializeComponent() End Sub Public Class SearchBuilder Inherits FormManager Implements FormsManagerInterface Public Sub New() InvokeMethods() End Sub Public Shadows Sub InvokeMethods() Implements FormsManagerInterface.InvodeMethods MsgBox("SearchBuider Routines Invoked" & " ") Label1.Text = "Search Form Created" 'do somehting...design screen End Sub End Class Public Class FormBuilder Inherits FormManager Implements FormsManagerInterface Public Sub New() InvokeMethods() End Sub Public Shadows Sub InvokeMethods() Implements FormsManagerInterface.InvodeMethods MsgBox("FormBuilder Routines Invoked") Label1.Text = "Builder Form Created" 'do something...design screen End Sub End Class Public Interface FormsManagerInterface Sub InvodeMethods() End Interface Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(16, 48) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(256, 23) Me.Label1.TabIndex = 1 Me.Label1.Text = "Label1" ' 'FormManager ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Label1) Me.Name = "FormManager" Me.ResumeLayout(False) End Sub End Class
As far as the instance from the child back to the parent, using me in FormBuilder or SearchBuilder gives you the instance of FormManager because it is inherited.
well, actually me does not gives you the instance of the parent. I've tried it and it returns the child form instance. You should try it to see for yourself.
We must be doing something different because using 'me' works fine in my code. If you create separate instance of FormBuilder/SearchBuilder then yes using me will not work to get the other instance of your form. But using FormBuilder/SearchBuilder as the instance for your form then it will work. Ex:
VB Code:
Dim FM as FormManager FM = new FM.SearchBuilder 'me will be the actual instance of the form shown in your search builder code. See my previous post. FM.ShowDialog()
I think the reason its working for you because you have FormBuilder/SearchBuilder as nested classes inside FormManager class. While I have those classes separate from FormManager Class and I do not intend to embed those classes inside FormManager. Even though I do not know if there is any difference in the two methodologies, somehow I do not quite feel comfortable in using Nested Classes. I would like my FormManager not to worry about its implementation. Its a base class and should remain unaware of the way its been implemented.
I won't drag out the point since you seem like you are okay now. But, even if you do not nest the classes it will work. The reason it works is because the instance of FormManager and the other two classes become the same when created. A parameter or anything can be used to determine which class is used. If you do not do that at creation, then 'me' will in fact not be the instance you want.
Anyhow, I wish you well.
VB Code:
Dim frm as FormManager frm = new SearchBuilder() 'no need to nest frm.ShowDialog() public Class FormManager inherits Form '[vb generated code from designer...] Sub DoWhatEver() End Sub End Class public Class SearchBuilder inherits FormManager Sub New() DoSomething() End Sub Sub DoSomething() 'assume you have a label on the form me.Label1.Location = New Point(3, 2) 'me is the form displayed 'even add controls to the displayed instance of FormManager dim L as new Label me.controls.add(L) 'access formmanager routines of displayed form me.DoWhatEver() End Sub End Class