Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2008

Tip: Looking for answers? Try searching our database.

Delegates and Interfaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ryan - 04 Jan 2008 20:54 GMT
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.
Wolf Saenger - 04 Jan 2008 21:22 GMT
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

> 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
[quoted text clipped - 17 lines]
>
> I appreciate any help anyone can give.
Peter Duniho - 04 Jan 2008 21:32 GMT
> [...]
> So here's my dilemma.  The control (or controls) could call various
[quoted text clipped - 4 lines]
> 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
Ryan - 07 Jan 2008 14:11 GMT
On Jan 4, 4:32 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > So here's my dilemma.  The control (or controls) could call various
[quoted text clipped - 15 lines]
>
> 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.
Wolf Saenger - 07 Jan 2008 15:05 GMT
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
Ryan - 07 Jan 2008 15:22 GMT
> 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?
Wolf Saenger - 07 Jan 2008 16:30 GMT
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

>> Dear Ryan,
>>
[quoted text clipped - 10 lines]
> is how do I property use a delegate in the interface?  Or how else
> should I do it?
Peter Duniho - 07 Jan 2008 17:59 GMT
> [...]
> 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
Ryan - 07 Jan 2008 19:59 GMT
On Jan 7, 12:59 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > I'm having a hard time getting the delegate part of the interface
[quoted text clipped - 67 lines]
>
> 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
Jon Skeet [C# MVP] - 04 Jan 2008 21:51 GMT
<snip>

> So here's my dilemma.  The control (or controls) could call various
> methods to get a "data refresh".  Therefore, I wanted to give the
[quoted text clipped - 5 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.