• Newbie Delegates question (VB NET)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Newbie Delegates question (VB NET)

    Author
    Topic
    #382715

    I used to use vb quite a bit during release 3, but have not used it seriously for quite some time. I am now trying to get back into it, and the whole concept of Delegates is proving quiet difficult for me.

    Can someone please explain what these are, and how they work ? I have a good handle on classes, but delegates are a complete mystery to me at the moment.

    Viewing 0 reply threads
    Author
    Replies
    • #650070

      I too am new to .Net but have used a much newer version of VB and VBA than your last experience. Delegates have nothing to do with classes, they are the way in which you can write your own events (code which gets triggered to run on some action such as a key press or a Windows message).

      Stolen from the book “Coding Techniques for MS VB.Net” by Connell:
      “A delegate formalizes the process of declaring a procedure that will respond to an event. A delegate is used to communicate the message (of the event being triggered) between the source and the listener (your code). Another powerful feature of delegates is multicast functionality, which means that a single sender can be dispatched to several receivers, acting as a one-to-many relationship.”

      An example from a book I have shows a program that monitors changes in certain files on the disk. So if a given file/s has changed (been edited, or a new file of a certain type has been created/written to the disk) it triggers an event which your code then responds to. So if you wrote code that needs to run when a file of type .txt is created in directory C:MyFiles then you’d need to create a ‘delegate’ to monitor that action.
      So a delegate lets you get notified when non-standard events occur (events that aren’t part of the regular Framework such as button clicks in a Windows form app).

      I haven’t used them myself but the book “Coding Techniques for MS VB.Net” by Connell has been very good in giving me a great start at .Net (even if you’re using C#, it doesn’t matter, .Net is the same just minor syntax differences for another language). I’ve read every page of that book and have manually entred most of the code and that’s made a big impact on my understanding of this new development environment.

      HTH, Deb

      • #650947

        I’m getting quite mutlilingual myself, but I must take a bit of an exception to your:

        >>minor syntax differences

        VB.Net and C# syntax differences are minor if C and QBASIC syntax differences are minor.

        Maybe if I could remember the semicolon and all the previously unecessary parenthesis and braces
        the “minor” would not seem so “major”.

        • #650954

          I guess it depends on your point of reference. I did 8 yrs of C and 3 yrs of C++ before doing any VB or VBA so for me the differences aren’t that big of a deal. I found it more annoying going from C++ to VBA and cursed the syntax for a little while. At least the keywords are the same and for the most part the same functionality is offered and that’s what important not whether or not I have to remember to add semicolons or use {} or not. It’s an easier leap than VB to Perl.

          Deb 2cents

    Viewing 0 reply threads
    Reply To: Newbie Delegates question (VB NET)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: