Results 1 to 16 of 16

Thread: How do I pass a Usercontrol Object to a subroutine?

  1. #1

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    How do I pass a Usercontrol Object to a subroutine?

    If I have a subroutine how can I pass a usercontrol to that routine?
    VB Code:
    1. Private Sub testDevice(newGD As UserControl)
    2. End sub
    3. 'call it using test Usercontrol
    Gives me an "Invalid use of Base Class name" Error when called
    I tried As Variant and As Object: same error.

    The only way I can think of is to expose all the properties and methods of the Usercontrol object in the final control and pass myUserControl as Object. Any other way?

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How do I pass a Usercontrol Object to a subroutine?

    Did you use the UserControl name, not UserControl? Because when I tried that (using UserControl1, UserControl11 was what I passed to it), I got no error at all.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do I pass a Usercontrol Object to a subroutine?

    Yes, user control's name is the problem - you have to pass exact name (in my test it's a ucText):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Test ucText1, "abc"
    5. End Sub
    6.  
    7. Public Sub Test(uc As ucText, sText As String)
    8.     uc.Text = sText
    9. End Sub

  4. #4

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I pass a Usercontrol Object to a subroutine?

    This is the option I stated above, but if I do that then I have to export every property and method of the Usercontrol That I want to access. For instance If I use
    VB Code:
    1. 'have to declare as object since this class is used with other types of objects.
    2. Public Sub testDevice(newGD As Object)
    3.    newGD.AutoRedraw = True
    4. End Sub
    5. End sub
    Then I would have to create a Public Property in the control called AutoRedraw which calls the AutoRedraw property of the UserControl
    VB Code:
    1. Public Friend Property Get AutoRedraw() As Boolean
    2.     AutoRedraw = UserControl.AutoRedraw
    3. End Property
    4.  
    5. Friend Property Let AutoRedraw(ByVal New_AutoRedraw As Boolean)
    6.     UserControl.AutoRedraw() = New_AutoRedraw
    7.     PropertyChanged "AutoRedraw"
    8. End Property
    The control contains a class which needs to access the Usercontrol Object and it seems kind of silly if I have a class in the usercontrol itself that needs to access Properties of the Usercontrol, but before using that class, I have to export all the properties of the Usercontrol.
    I can't even export them as Friend so that they cannont be seen from outside the usercontrol.

    I though I had been able to do this once before, but maybe my alzheimer's is kicking in

    What is the difference between
    VB Code:
    1. myClass.testDevice UserControl
    and
    VB Code:
    1. myClass.testDevice (UserControl)

  5. #5

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How do I pass a Usercontrol Object to a subroutine?

    Seeing from there, you're trying to call the class from the UserControl itself? You just can't have a line like myClass.testDevice UserControl, because UserControl is a reserved word. I don't know if Me works any better... before I make a fool of myself too badly: what is the exact code you use to call the class and where it is placed in?

    There is no difference with those calls, (UserControl) and UserControl would be 100% the same.

  7. #7

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I pass a Usercontrol Object to a subroutine?

    There is no difference with those calls, (UserControl) and UserControl would be 100% the same.
    But there is a difference (UserControl) allows me to pass the user control object without error, but I still can't access any if its properties from within the subroutine.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    Enclosing a function parameter in brackets forces it to be passed ByVal. I'm still trying to get my head around the rest of your problem.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    OK, maybe I'm thick, but I have no idea what you're on about

    You have a class object defined in your UserControl that needs to access properties of the usercontrol?

    How about (in the class, let's assume your UserControl's name is Thing):
    VB Code:
    1. Private mobjParent As Thing
    2.  
    3. Friend Property Set ParentControl(ByRef pobjControl As Thing)
    4.     Set mobjParent = pobjControl
    5. End Property
    6.  
    7. Friend Property Get ParentControl() As Thing
    8.     Set ParentControl = mobjParent
    9. End Property

    and in your usercontrol code, wherever you instantiate your class object
    VB Code:
    1. Set Classobjectwhatnot.ParentControl = Me

    That should work. And then to reference your control from within the class just use
    VB Code:
    1. mobjParent.Whatever = Whoever

    Unless, I've completely misunderstood

  10. #10

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I pass a Usercontrol Object to a subroutine?

    OK maybe this will make it more clear.
    Here is my Class
    VB Code:
    1. 'In a class module - Class1
    2. 'have to declare as object since this class is used with other types of objects.
    3. Public Sub testDevice(newGD As Object)
    4.    newGD.AutoRedraw = True
    5. End Sub
    Here is my control
    VB Code:
    1. 'In a control module - MyControl
    2. Private myClass As Class1
    3.  
    4. Public Sub Test()
    5.     'I can access properties of the Usercontrol object here
    6.     UserControl.AutoRedraw = True
    7.     'but I can't pass the object to my class
    8.     Call myClass.testDevice(UserControl)
    9. End Sub
    And the form
    VB Code:
    1. 'In a form - Form1
    2. Private myClass As Class1
    3.  
    4. Private Sub Command1_Click()
    5.     Set myClass = New Class1
    6.     'this works
    7.     myClass.testDevice Me
    8.     'this works
    9.     myClass.testDevice Picture1
    10.    
    11.     'this doesn't work
    12.     MyControl1.Test
    13.  End Sub
    Clear as mud?

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    Don't pass UserControl. That is the base class. You want to pass Me which is an object instance reference for the current control object.
    VB Code:
    1. myClass.testDevice Me

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    Also, I saw a mention of Friend procedures somewhere above. I don't know if you still have problems with them but if you want to access Friend-scoped methods of a class you need to cast your late-bound reference to an early-bound reference because the Friend methods are not part of the class interface which late-binding relies on.

  13. #13

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I pass a Usercontrol Object to a subroutine?

    Don't pass UserControl. That is the base class. You want to pass Me which is an object instance reference for the current control object.
    This is what confuses me. If UserControl were truly a base class then I wouldn't be able to do something like
    VB Code:
    1. UserControl.AutoRedraw = True
    just as I can't act on Class1 but have to act on MyClass. But UserControl is an object. But I guess I will export all the properties and methods of the Usercontrol object so I can access them.
    you need to cast your late-bound reference to an early-bound reference
    Can you explain how one does this?

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    Well it confuses me too! Have you tried passing Me instead? That should work.

    Quote Originally Posted by moeur
    Quote Originally Posted by penagate
    you need to cast your late-bound reference to an early-bound reference
    Can you explain how one does this?
    VB Code:
    1. Public Sub testDevice(newGD As Object)
    2. Dim objControl As MyControl
    3.     Set objControl = newGD
    4.     ' Let's say AutoRedraw is a Friend-scoped property
    5.     objControl.AutoRedraw = True
    6.     Set objControl = Nothing
    7. End Sub

  15. #15

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How do I pass a Usercontrol Object to a subroutine?

    Have you tried passing Me instead?
    Me passes a refernce to the control itself not the Usercontrol Object. So, if you pass Me you will only see properties exposed in the control. I've resigned myself to the fact that I am going to have to expose all those properties and in a public manner too since early-binding doesn't seem to be an option.

  16. #16
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I pass a Usercontrol Object to a subroutine?

    Aaaaahhhh... Now I get ya

    You are going to have to expose the properties yes.

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