Results 1 to 5 of 5

Thread: Need Clarification About PublicNotCreatable

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    Question Need Clarification About PublicNotCreatable

    What is the use of Instancing Property- PublicNotCreatable. I read that we can't create an instance for a PublicNotCreatable. Then how can we access the functions of that. If not possible, what is the use of creating such objects?... Please anyone answer me...

  2. #2
    Lively Member Elvenstone's Avatar
    Join Date
    Feb 2008
    Location
    Rotterdam (Netherlands)
    Posts
    122

    Re: Need Clarification About PublicNotCreatable

    MSDN says:
    "PublicNotCreatable means that other applications can use objects of this class only if your component creates the objects first. Other applications cannot use the CreateObject function or the New operator to create objects from the class."

    You could use this way of instancing when you have a class that is dependent on a parent class, when it cannot "work" by itself. For example if you have an employee object, and you always need the company object (it's parent) for it to work.
    Please be kind enough to RATE a helpful post.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    Re: Need Clarification About PublicNotCreatable

    But In the employee object I cant able to use the functions available in the Company Object(Parent Class). How can I solve this...

  4. #4
    Lively Member Elvenstone's Avatar
    Join Date
    Feb 2008
    Location
    Rotterdam (Netherlands)
    Posts
    122

    Re: Need Clarification About PublicNotCreatable

    Say the Company object has a collection of Employee objects and you want to add a new Employee.

    You will first need create the Company object, then you would have to use the Company object to create an instance of the Employer object, for example:

    Code:
    Dim MyCompany As Company
    Dim MyEmployee As Employee
    
    'Create an instance of the Company object
    Set MyCompany = New Company
    
    'Use the Add_Employee method to create a new Employee object
    Set MyEmployee = MyCompany.Add_Employee
    The Add_Employee method would use "Set = New Employee" to add a new Employee to an array of Employee objects.

    Good luck and ehh...
    Please be kind enough to RATE a helpful post.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Need Clarification About PublicNotCreatable

    This is an example where Company acts as a class factory object.

    Some practical reasons for writing class factories include:
    • Passing initializer data when creating a new object.
    • Setting property values that are meant to be read-only except when they are first created.
    • Creating object hierarchies.
    • Allowing multiple "child" objects to raise events through the "parent" object (since you can't have a WithEvents object array).

    The object hierarchy case is interesting because you can wire things up manually but it might be error prone.

    For example a Company object might have an internal Collection of Employee objects, and an Employee object might have a reference to its Company. You need the "links" set in both directions, and you need termination logic that will tear down the circular object references properly to avoid memory leaks (or a program that never terminates).

    Moving an Employee to a new Company could be done by having two Company methods: Release and Hire. Putting such logic inside the Company class makes it less likely to be done incorrectly in code that uses Company and Employee objects.

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