Results 1 to 13 of 13

Thread: Inheritance ..

  1. #1

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437

    Inheritance ..

    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 ?

  2. #2

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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 ??

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by spoiledkid
    But how does parent class can access that child without having to create a object of that child ?
    Never seen or heard of this scenario before !

  4. #4

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Originally posted by Pirate
    Never seen or heard of this scenario before !
    Okay, forget it, just tell me ... I need the base class object in one of the childs. How Do I get the form object.

    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 ...

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Umm , why don't you create instance of the base class and use in CType function ?

  6. #6

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Originally posted by Pirate
    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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 ??
    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.

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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:
    1. Protected ReadOnly Property Instance() As Object
    2.         Get
    3.             Return Me
    4.         End Get
    5.     End Property

    Is there no elegant way to get the instance of Parent class in child classes ????

    Tx.

  9. #9
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    Here is another idea. (Set LoadForms as the startup form)

    VB Code:
    1. Public Class LoadForms
    2.     Inherits Windows.Forms.Form
    3.     Friend WithEvents Button1 As System.Windows.Forms.Button
    4.     Public Sub New()
    5.         InitializeComponent()
    6.     End Sub
    7.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Dim FM As FormManager
    9.         If MsgBox("Load for search?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
    10.             FM = New FormManager.SearchBuilder
    11.         Else
    12.             FM = New FormManager.FormBuilder
    13.         End If
    14.         FM.ShowDialog()
    15.     End Sub
    16.     Private Sub InitializeComponent()
    17.         Me.Button1 = New System.Windows.Forms.Button
    18.         Me.SuspendLayout()
    19.         '
    20.         'Button1
    21.         '
    22.         Me.Button1.Location = New System.Drawing.Point(0, 0)
    23.         Me.Button1.Name = "Button1"
    24.         Me.Button1.TabIndex = 0
    25.         Me.Button1.Text = "Click"
    26.         '
    27.         'FormManager
    28.         '
    29.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    30.         Me.ClientSize = New System.Drawing.Size(292, 273)
    31.         Me.Controls.Add(Me.Button1)
    32.         Me.Name = "FormManager"
    33.         Me.ResumeLayout(False)
    34.  
    35.     End Sub
    36.  
    37. End Class
    38. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    39. Public Class FormManager
    40.     Inherits System.Windows.Forms.Form
    41.     Friend WithEvents Label1 As System.Windows.Forms.Label
    42.     Public Sub New()
    43.         InitializeComponent()
    44.     End Sub
    45.  
    46.     Public Class SearchBuilder
    47.         Inherits FormManager
    48.         Implements FormsManagerInterface
    49.         Public Sub New()
    50.             InvokeMethods()
    51.         End Sub
    52.         Public Shadows Sub InvokeMethods() Implements FormsManagerInterface.InvodeMethods
    53.             MsgBox("SearchBuider Routines Invoked" & " ")
    54.             Label1.Text = "Search Form Created"
    55.             'do somehting...design screen
    56.         End Sub
    57.     End Class
    58.     Public Class FormBuilder
    59.         Inherits FormManager
    60.         Implements FormsManagerInterface
    61.         Public Sub New()
    62.             InvokeMethods()
    63.         End Sub
    64.         Public Shadows Sub InvokeMethods() Implements FormsManagerInterface.InvodeMethods
    65.             MsgBox("FormBuilder Routines Invoked")
    66.             Label1.Text = "Builder Form Created"
    67.             'do something...design screen
    68.         End Sub
    69.     End Class
    70.     Public Interface FormsManagerInterface
    71.         Sub InvodeMethods()
    72.     End Interface
    73.  
    74.  
    75.  
    76.     Private Sub InitializeComponent()
    77.         Me.Label1 = New System.Windows.Forms.Label
    78.         Me.SuspendLayout()
    79.         '
    80.         'Label1
    81.         '
    82.         Me.Label1.Location = New System.Drawing.Point(16, 48)
    83.         Me.Label1.Name = "Label1"
    84.         Me.Label1.Size = New System.Drawing.Size(256, 23)
    85.         Me.Label1.TabIndex = 1
    86.         Me.Label1.Text = "Label1"
    87.         '
    88.         'FormManager
    89.         '
    90.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    91.         Me.ClientSize = New System.Drawing.Size(292, 273)
    92.         Me.Controls.Add(Me.Label1)
    93.         Me.Name = "FormManager"
    94.         Me.ResumeLayout(False)
    95.  
    96.     End Sub
    97. 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.
    I can do all things with VB.

  10. #10

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

  11. #11
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    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:
    1. Dim FM as FormManager
    2. FM = new FM.SearchBuilder  'me will be the actual instance of the form shown in your search builder code.  See my previous post.
    3. FM.ShowDialog()
    I can do all things with VB.

  12. #12

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

  13. #13
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    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:
    1. Dim frm as FormManager
    2. frm = new SearchBuilder() 'no need to nest
    3. frm.ShowDialog()
    4.  
    5. public Class FormManager
    6.   inherits Form
    7.   '[vb generated code from designer...]
    8.   Sub DoWhatEver()
    9.   End Sub
    10. End Class
    11.  
    12. public Class SearchBuilder
    13.    inherits FormManager
    14.    Sub New()
    15.       DoSomething()
    16.    End Sub
    17.    Sub DoSomething()
    18.        'assume you have a label on the form
    19.        me.Label1.Location = New Point(3, 2)  'me is the form displayed
    20.        'even add controls to the displayed instance of FormManager
    21.        dim L as new Label
    22.        me.controls.add(L)
    23.        'access formmanager routines of displayed form
    24.        me.DoWhatEver()
    25.    End Sub
    26. End Class
    I can do all things with VB.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width