Declaring a user control as a parameter

G

Guest

Hello all....

I'm developing a web app VB 2005. Is it possible to declare/pass a user
control as a parameter in a function?

For example, I create a user control called ucMyControl, and on the page
that uses it I can refer to the instance name ucMyControl1.

However now I want to pass that control to another class. I'd like to create
a method in that class that can receive the user control:

Public Class xyz
....
Public Shared Sub AMethod (byref ucMyControl1 as ucMyControl)
....
End Class

But the 'as ucMyControl' part is generating compile errors. What is the
method signature syntax if this is doable?

Thanks - Hedge
 
C

Chris Dunaway

Tony said:
Public Class xyz
....
Public Shared Sub AMethod (byref ucMyControl1 as ucMyControl)
....
End Class

But the 'as ucMyControl' part is generating compile errors. What is the
method signature syntax if this is doable?

What compile errors are you getting? We can't read your mind! :) Have
you included the correct Imports statement at the top of the code to
import the appropriate namespace for your control?
 
G

Guest

You can't read minds? What kind of superheros are you guys anyway. ;-)

If I type the method as I wrote below, I get the dreaded squiggly lines
under ucMyControl with the message "type 'ucMyControl' is not defined"

So it's a two-fold problem:
a) I don't know the namespace or fully qualified name to use.
b) All pages, user controls, types, etc... are in the same assemby so I
don't know why I would need to use a namespace to begin with.

Is the problem that user controls, like web pages, are now defined as
partial classes?

Thanks!!
 
T

Tom Leylan

Tony: If the class isn't recognized then it must be out of scope. First
though you should note that you probably don't want to use "byref" but
rather "byval". It is easy to make that mistake but an object is a
reference type, if you pass it by value it is a reference, if you pass the
reference by reference it make it possible for AMethod() to actually assign
another object to it. Obviously if that is what you're trying to do that's
fine but otherwise it is a dangerous practice.

Try the following:

Public Shared Sub AMethod(ByVal obj As Button)
End Sub

You will probably observe that that works, so we're pretty sure it is
something to do with your class. But just to confirm try the following

Public Shared Sub AMethod()
Dim obj As ucMyControl
End Sub

Again ucMyControl isn't recognized which confirms it.

Finally try to create a test class in the same file where AMethod is located
named ucMyControl. It doesn't matter what it is just define one. I'll
predict your original code now compiles so we know it just can't see the
definition of your class.

Hope this helps...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top