Results 1 to 2 of 2

Thread: Inheriting Object from parent form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Inheriting Object from parent form

    Hi,

    I have a main form and I also have an additional form that is loaded from the main form. I would like to reuse an object that is declared and initialized in the main form. How can I do this? I set the child form to inherit from the main form but when I try to use the object, I receive errors indicating that it has not been declared.

    Here is an example of what I have:

    Code:
    public class Mainform
    
    dim myObject as theObject = nothing
    myObject = ctype(function(),theObject)
    
    End class
    
    public class Childform inherits Mainform
    
    myObject.function()
    
    End class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Inheriting Object from parent form

    You don't inherit objects. You inherit members. If ChildForm inherits MainForm then it will inherit all the properties, methods, events, etc. of MainForm. That doesn't mean that creating an instance of MainForm and an instance of ChildForm will magically give the ChildForm instance access to the data from the MainForm instance though. Both objects will have the same members but those members will have different values.

    This is not a job for inheritance at all. It's purely and simply a case of passing data from one object to another. The MainForm instance creates an instance of ChildForm and passes to that instance the appropriate data, either by setting a property or by calling a method and passing an argument. It's just like creating a Button and setting its Text property. That's how you pass data into an object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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