Delegates and Interfaces

R

Ryan

I have a dilemma that I'm having a tough time getting through on a VB
2005 issue. I have a custom control for a form to display data from a
windows form. Right now, the main form calls various web service
methods in order to update the data. Since the data to be displayed
depends on the call to the web service, I use a delegate that will
invoke the method when a timer elapses. Works good for now but I want
to create additional custom controls that will display data and have
the main form cycle through the controls. Because of this, I figured
that it would be easier to implement an interface on the custom
control so that the form can instantiate the controls dynamically as
it cycles through.

So here's my dilemma. The control (or controls) could call various
methods to get a "data refresh". Therefore, I wanted to give the
control the delegate to invoke within itself as the main form cycles
through each control on a separate timer. But how do I do that using
an interface? I can't have the delegate on the interface because its
a type. I can't pass in the signature to the c'tor because it's not a
type. What options do I have?

I appreciate any help anyone can give.
 
W

Wolf Saenger

Hello Ryan,

Not clear.

do you mean some stuff like this:

for each _control as Control in FormWindow.Controls
if type.GetInterface("IaInterface") isnot nothing then
Do bla
end if

next
 
P

Peter Duniho

[...]
So here's my dilemma. The control (or controls) could call various
methods to get a "data refresh". Therefore, I wanted to give the
control the delegate to invoke within itself as the main form cycles
through each control on a separate timer. But how do I do that using
an interface? I can't have the delegate on the interface because its
a type. I can't pass in the signature to the c'tor because it's not a
type. What options do I have?

I agree with Wolf, your question isn't very clear about what you're trying
to do.

But as far as declaring a type in an interface goes, that's right...it
wouldn't make sense to declare a type as part of an interface. But you
certainly could declare the type in the same namespace as the interface,
and that's typically how something that like would be done (where a
specific type is necessary as part of the use of a specific interface).

Pete
 
J

Jon Skeet [C# MVP]

So here's my dilemma. The control (or controls) could call various
methods to get a "data refresh". Therefore, I wanted to give the
control the delegate to invoke within itself as the main form cycles
through each control on a separate timer. But how do I do that using
an interface? I can't have the delegate on the interface because its
a type. I can't pass in the signature to the c'tor because it's not a
type. What options do I have?

I appreciate any help anyone can give.

I'm not clear what the problem is. Specify the delegate type
separately, and you can absolutely use it in the interface or in a
constructor. I'd give an example, but I'm afraid I'd get the VB side of
it wrong, and it's not clear to me how you'd want it to appear in the
interface.
 
R

Ryan

[...]
So here's my dilemma.  The control (or controls) could call various
methods to get a "data refresh".  Therefore, I wanted to give the
control the delegate to invoke within itself as the main form cycles
through each control on a separate timer.  But how do I do that using
an interface?  I can't have the delegate on the interface because its
a type.  I can't pass in the signature to the c'tor because it's not a
type.  What options do I have?

I agree with Wolf, your question isn't very clear about what you're trying 
to do.

But as far as declaring a type in an interface goes, that's right...it  
wouldn't make sense to declare a type as part of an interface.  But you  
certainly could declare the type in the same namespace as the interface,  
and that's typically how something that like would be done (where a  
specific type is necessary as part of the use of a specific interface).

Pete

Sorry guys if it seems confusing. Also, sorry for the late response.
Let me use some code samples to see if get my question across. I'll
have several user controls:

Public Class PlanActual
inherits UserControl
implements SignBoardControl
End Class

Public Class QuaterlyProduction
inherits UserControl
implements SignBoardControl
End Class

I thought an interface would be the best because the main form would
have the following...

Dim sbControls as ArrayList = New ArrayList
("PlanActual","QuaterlyProduction")
Dim currentIndex as Integer <-- this will hold what control is
currently being displayed

...so I could do this

Private sub TimerSwitchControl_Tick(sender as object, e as EventArgs)
dim control as SignBoardControl
control = AppDomain.CreateInstance(sbControls(currentIndex))
' *** here the control would be assigned a delegate of the update
method that exist in the main form.
' *** therefore, the control will call the method on the main form
to update the data
Select Case CurrentIndex
case 1:
control.DataSource = dataset1
control.Update = AddressOf Update1
case 2:
control.DataSource = dataset2
control.Update = AddressOf Update2
End Select
currentIndex += 1
End Sub

...following here would be various methods that make calls to the web
service...

Private Sub Update1
call webservice to update dataset1
End Sub

Private Sub Update2
call webservice to update dataset2
End Sub

I'm having a hard time getting the delegate part of the interface
where I can use it in this fashion. I hope this helps.
 
W

Wolf Saenger

Dear Ryan,

sorry, I'm not able to understand where you're problem is.
I'm curious to see, what to the other fellows will read out of you're post.

regards Wolf
 
R

Ryan

Dear Ryan,

sorry, I'm not able to understand where you're problem is.
I'm curious to see, what to the other fellows will read out of you're post..

regards Wolf

Hey Wolf,

You see the .Update method I have in the Timer sub? I want that to be
a delegate so I can set it to a method in the main form. My question
is how do I property use a delegate in the interface? Or how else
should I do it?
 
W

Wolf Saenger

Hello Ryan,

I believe I'm not so clever to get you but may you look for this:

Public Class M1
Implements IM1

End Class

Public Interface IM1
Delegate Sub MyDelegate()
End Interface

Public Class A
Sub SubA()
Dim _i As IM1 = New M1
Dim _new As System.Delegate
_new = System.Delegate.CreateDelegate(GetType(M1), _i, "SubB")
End Sub

Sub SubB(ByVal Bla As String)

End Sub
End Class
 
P

Peter Duniho

[...]
I'm having a hard time getting the delegate part of the interface
where I can use it in this fashion. I hope this helps.

I have the same problem Jon does, which is that I don't know VB well
enough to advise you on the exact syntax.

However, it appears that Wolf has provided a VB sample and I hope that is
close enough to your needs. As far as the specific question goes, you can
do what you are trying to if you make "Update" a property in the
interface, defined using a delegate defined elsewhere (such as the same
namespace in which the interface is declared, as I suggested earlier).
Depending on the signature of the delegate, there may already be a
delegate type defined in .NET that you can use (for example,
"MethodInvoker" has no parameters and no return value).

Each class implementing your SignBoardControl interface would have to
implement that property, by having a private field to store the actual
value and having the property itself with a setter, and possibly a getter
(if you want users of the interface to be able to inspect the current
value of the property), that simply copies the value assigned to the
property to the private field so it can be used later.

By the way, I recommend you follow the .NET naming convention of starting
all interface names with a capital "I". This makes it more clear in the
code that something is an interface. It's less of a problem in VB, since
class declarations already make it clear by using "implements" instead of
"inherits", but elsewhere it will be more obvious as well.

In C# it would look something like this:

interface ISignBoardControl
{
MethodInvoker Update { set; }
}

class PlanActual : ISignBoardControl
{
private MethodInvoker UpdateHandler;

public MethodInvoker Update { set { UpdateHandler = value; } }

// presumably somewhere else in the class you have code that calls
the delegate,
// using the UpdateHandler field or, if you also provide a getter
for the property,
// using the the property itself
}

then somewhere else in code you can assign Update1 or Update2 to the
Update method as needed.

All of this assumes that you are using the same method for multiple
classes that implement the interface. If there's a one-to-one
relationship between the update method and the class it's used for, it
seems to me it would make more sense to just make the method itself part
of the interface, and implement the appropriate method in each class,
rather than messing around with the delegate.

Even if you are using the same method for multiple classes, it may be
better to create an intermediate class that's inherited by any class
sharing the same method. In this context, the delegate is really more
useful when at run-time you may have to reassign a different method
according to some criteria. If the class will always have exactly one
delegate method assigned to the delegate, then that's something that can
be done statically at compile time and without the complication of
managing the delegate.

Hope that helps.

Pete
 
R

Ryan

[...]
I'm having a hard time getting the delegate part of the interface
where I can use it in this fashion.  I hope this helps.

I have the same problem Jon does, which is that I don't know VB well  
enough to advise you on the exact syntax.

However, it appears that Wolf has provided a VB sample and I hope that is  
close enough to your needs.  As far as the specific question goes, you can  
do what you are trying to if you make "Update" a property in the  
interface, defined using a delegate defined elsewhere (such as the same  
namespace in which the interface is declared, as I suggested earlier).  
Depending on the signature of the delegate, there may already be a  
delegate type defined in .NET that you can use (for example,  
"MethodInvoker" has no parameters and no return value).

Each class implementing your SignBoardControl interface would have to  
implement that property, by having a private field to store the actual  
value and having the property itself with a setter, and possibly a getter  
(if you want users of the interface to be able to inspect the current  
value of the property), that simply copies the value assigned to the  
property to the private field so it can be used later.

By the way, I recommend you follow the .NET naming convention of starting  
all interface names with a capital "I".  This makes it more clear in the 
code that something is an interface.  It's less of a problem in VB, since  
class declarations already make it clear by using "implements" instead of  
"inherits", but elsewhere it will be more obvious as well.

In C# it would look something like this:

         interface ISignBoardControl
     {
         MethodInvoker Update { set; }
     }

     class PlanActual : ISignBoardControl
     {
         private MethodInvoker UpdateHandler;

         public MethodInvoker Update { set { UpdateHandler = value; } }

         // presumably somewhere else in the class you have codethat calls  
the delegate,
         // using the UpdateHandler field or, if you also provide a getter  
for the property,
         // using the the property itself
     }

then somewhere else in code you can assign Update1 or Update2 to the  
Update method as needed.

All of this assumes that you are using the same method for multiple  
classes that implement the interface.  If there's a one-to-one  
relationship between the update method and the class it's used for, it  
seems to me it would make more sense to just make the method itself part  
of the interface, and implement the appropriate method in each class,  
rather than messing around with the delegate.

Even if you are using the same method for multiple classes, it may be  
better to create an intermediate class that's inherited by any class  
sharing the same method.  In this context, the delegate is really more  
useful when at run-time you may have to reassign a different method  
according to some criteria.  If the class will always have exactly one  
delegate method assigned to the delegate, then that's something that can  
be done statically at compile time and without the complication of  
managing the delegate.

Hope that helps.

Pete

Pete,

This got me on the right track. Each time I used delegate, the
compiler kept telling I couldn't use it as a type and I didn't know
how to properly use it. Thanks a bunch!!!

Ryan
 

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