|
-
Aug 19th, 2005, 06:31 PM
#1
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:
Private Sub testDevice(newGD As UserControl)
End sub
'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?
-
Aug 19th, 2005, 06:48 PM
#2
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.
-
Aug 19th, 2005, 06:54 PM
#3
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:
Option Explicit
Private Sub Command1_Click()
Test ucText1, "abc"
End Sub
Public Sub Test(uc As ucText, sText As String)
uc.Text = sText
End Sub
-
Aug 19th, 2005, 07:57 PM
#4
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:
'have to declare as object since this class is used with other types of objects.
Public Sub testDevice(newGD As Object)
newGD.AutoRedraw = True
End Sub
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:
Public Friend Property Get AutoRedraw() As Boolean
AutoRedraw = UserControl.AutoRedraw
End Property
Friend Property Let AutoRedraw(ByVal New_AutoRedraw As Boolean)
UserControl.AutoRedraw() = New_AutoRedraw
PropertyChanged "AutoRedraw"
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:
myClass.testDevice UserControl
and
VB Code:
myClass.testDevice (UserControl)
-
Aug 19th, 2005, 08:15 PM
#5
Re: How do I pass a Usercontrol Object to a subroutine?
You don't have to export every property and/or method - just a few of them but handle what you need internally by say passing certain value - just like SendMessage() function does.
-
Aug 19th, 2005, 08:20 PM
#6
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.
-
Aug 19th, 2005, 09:12 PM
#7
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.
-
Aug 20th, 2005, 01:03 AM
#8
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.
-
Aug 20th, 2005, 01:10 AM
#9
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:
Private mobjParent As Thing
Friend Property Set ParentControl(ByRef pobjControl As Thing)
Set mobjParent = pobjControl
End Property
Friend Property Get ParentControl() As Thing
Set ParentControl = mobjParent
End Property
and in your usercontrol code, wherever you instantiate your class object
VB Code:
Set Classobjectwhatnot.ParentControl = Me
That should work. And then to reference your control from within the class just use
VB Code:
mobjParent.Whatever = Whoever
Unless, I've completely misunderstood
-
Aug 20th, 2005, 10:48 AM
#10
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:
'In a class module - Class1
'have to declare as object since this class is used with other types of objects.
Public Sub testDevice(newGD As Object)
newGD.AutoRedraw = True
End Sub
Here is my control
VB Code:
'In a control module - MyControl
Private myClass As Class1
Public Sub Test()
'I can access properties of the Usercontrol object here
UserControl.AutoRedraw = True
'but I can't pass the object to my class
Call myClass.testDevice(UserControl)
End Sub
And the form
VB Code:
'In a form - Form1
Private myClass As Class1
Private Sub Command1_Click()
Set myClass = New Class1
'this works
myClass.testDevice Me
'this works
myClass.testDevice Picture1
'this doesn't work
MyControl1.Test
End Sub
Clear as mud?
-
Aug 20th, 2005, 10:56 AM
#11
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.
-
Aug 20th, 2005, 10:58 AM
#12
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.
-
Aug 20th, 2005, 11:07 AM
#13
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:
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?
-
Aug 20th, 2005, 11:11 AM
#14
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.
 Originally Posted by moeur
 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:
Public Sub testDevice(newGD As Object)
Dim objControl As MyControl
Set objControl = newGD
' Let's say AutoRedraw is a Friend-scoped property
objControl.AutoRedraw = True
Set objControl = Nothing
End Sub
-
Aug 20th, 2005, 11:22 AM
#15
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.
-
Aug 20th, 2005, 11:33 AM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|