How to add a method to a user control and use it in another

  • Thread starter Thread starter USNET
  • Start date Start date
U

USNET

I need to add a method to a user control that can be called from another
usercontrol. Adding the method is not difficult and its already done.
However, the problem is that the method is not available from the second
user control.

For simplicity, here is the basic method:

Public Function ChangePaletteData(ByVal p_DataType As String) As Integer

me.lblPaletteName.text = p_DataType

' more code'
End Function

This method can not be seen from outside this user control. Is this
expected behavior? If so, why can I see a property I created outside of it,
but not the method?

Any help would be appreciated.

Luis Barnes
 
This method can not be seen from outside this user control. Is this
expected behavior? If so, why can I see a property I created outside
of it, but not the method?

You need an instance of the control to access the method.

If you wish to call a method from anywhere in the application without an
instance (or reference) to the control, you can created a Shared Function
(add the keyword SHARED to the function declaration).
 
* "USNET said:
I need to add a method to a user control that can be called from another
usercontrol. Adding the method is not difficult and its already done.
However, the problem is that the method is not available from the second
user control.

For simplicity, here is the basic method:

Public Function ChangePaletteData(ByVal p_DataType As String) As Integer

me.lblPaletteName.text = p_DataType

' more code'
End Function

This method can not be seen from outside this user control. Is this
expected behavior? If so, why can I see a property I created outside of it,
but not the method?

You will have to instantiate the usercontrol. Let's assume that the
name of the usercontrol class is 'UserControl1', then you will need this
code:

\\\
Dim foo As New UserControl1()
foo.ChangePaletteData(...)
///

If your reference is stored in a variable of type 'Object' or
'UserControl', for example, you will need to cast:

\\\
DirectCast(goo, UserControl1).ChangePaletteData(...)
///

BTW: Don't use the 'p_*' prefix for method parameters ;-).
 
Back
Top