Can anyone explain Delegates in vb.net ?

  • Thread starter Thread starter Brad_1234
  • Start date Start date
  • Tags Tags
    Explain
Click For Summary
The discussion centers on the confusion surrounding the use of delegates in VB.NET, particularly due to the lack of clear explanations in existing examples. Delegates are described as references to methods, akin to function pointers in C/C++, and are essential for calling functions at runtime, handling events, and managing callbacks in asynchronous programming. The original poster seeks clarity on how to invoke a method from one class in a different namespace using delegates, expressing frustration over overly complex examples with long variable names that do not adequately explain the concept. A suggestion is made to explore basic examples online for better understanding, and it's noted that accessing methods from another namespace typically involves using a "using namespace" statement if the class and method are public.
Brad_1234
Messages
31
Reaction score
0
In all the examples there is no explanation just code segments.

There is the delegate constructor and somewhere a variable is dimmed to the newly defined delegate type, then a second variable is dimmed, and it somehow works.

The variables most examples use have long names, the instantiated delegate sounds the same, the examples I find just don't explain the concept or how to use them.

Lets say there is a namespace with one class, and a method within that class that I want to call. from another namespace, different class, I want to run a method that can invoke a delegate and run the method in the first class

if there is a good explanation for vb.net delegates? Id appreciate hearing it

thanks
 
Technology news on Phys.org
The OP hasn't been around for about 10 years, but in case someone else has a similar question...
Brad_1234 said:
In all the examples there is no explanation just code segments.
The short explanation is that a delegate is a reference to a method (in C/C++ parlance, a function pointer). The delegate concept is an advanced programming concept for which the main uses are being able to call a function at run time, as well is in responding to events. Delegates are also used in callbacks -- these are arguments in asynchronous methods where the callback is invoked when the asynchronous method has finished. This action let's the caller of the async function know that it has completed its task.
Brad_1234 said:
There is the delegate constructor and somewhere a variable is dimmed to the newly defined delegate type, then a second variable is dimmed, and it somehow works.

The variables most examples use have long names, the instantiated delegate sounds the same, the examples I find just don't explain the concept or how to use them.

Lets say there is a namespace with one class, and a method within that class that I want to call. from another namespace, different class, I want to run a method that can invoke a delegate and run the method in the first class

if there is a good explanation for vb.net delegates? Id appreciate hearing it
I'm not sure why you need a delegate for what you described. I haven't done any VB programming for a very long time, but the way it works in C# ought to be similar.

To use a method that belongs to a class in namespace NS, you would write a "using namespace NS;" statement in your program. If the class and its method are public, you would then have access to the class and its method. There would be a couple of ways to do this, depending on whether the class was defined as static or not.

For some examples, do a web search for "VB delegate examples" or "VB callback examples." That should get you some links to simple examples that you can play with.
 
  • Like
Likes jim mcnamara
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
63
Views
5K
  • · Replies 36 ·
2
Replies
36
Views
4K
  • · Replies 0 ·
Replies
0
Views
1K
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K