CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    8

    To Use Delegates or ??

    Hi,
    I am new to C# from Visual C++ so any answers as well as being explicit are very much appreciated.

    I am wondering what is the best way to selectively switch between groups of the same named functions based on a selection?

    Currently, I am switching between different cameras that implement three methods: "connectToCamera", "takeExposure" and "transferData". Each method contains camera specific functionality. I was thinking that dll's could be used: one associated with each camera type. What do you think? Is there a better way to do it? How would this work in practice?

    Thanks,

    Alan

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: To Use Delegates or ??

    You don't need to use separate DLLs (although you can - but that's just packaging; it's the design that matters). You can create an abstract camera base class and derive camera-specific subclasses. Then you would initialize a base class variable with a derived type instance. The camera manipulating code would then work only with the base class variable (via the base class interface - and here I don't mean C#'s interface type, but an interface as a collection of public methods/properties.)
    Alternatively, you could use a delegate, but if this delegate can be publicly set, then this approach is somewhat less safe than the inheritance based approach (since any method with the corresponding signature can be plugged in; with inheritance, you can control who can derive classes).

    Anyway, if you're not familiar with inheritance, I made an example for someone else here, so check out the first project (the one with animals - scroll down). The code contains explanatory comments IIRC. Also, there's a lot of text about classes and inheritance in that post that might be helpful.
    The idea is to have an abstract or virtual method (Vocalize() in the example) that can then be overridden in a derived class to provide some specific behavior, while enabling the client code to treat all different kinds of derived types the same way, via a base class variable.
    Last edited by TheGreatCthulhu; May 31st, 2012 at 12:41 PM.

  3. #3
    Join Date
    May 2012
    Posts
    8

    Re: To Use Delegates or ??

    Perfect and thanks! I will use it. Before I start, wondering if I use the dll's to add cameras to an already installed application, any ideas on how can I determine new camera classes published within the new dll's? I would like to drop in the dll's after install, have the application find the cameras of newly placed dll's and be able to use them. If not, no problem. I can hard code for now the camera class names.

    Best and thanks again,

    Alan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured