Delegates in C# and Vb.Net

breakpoint

Newcomer
Joined
May 1, 2010
Messages
11
I posted this over at the Syntax Forum, but after reading the main thread I believe I should have posted it here. It would be great if someone could help me delete the previous thread. ( http://www.xtremedotnettalk.com/showthread.php?t=103414 )

This is what I wrote over there:

Hi! I used to be an active member on this forum under a different username, but I have forgotten the login details.. Glad to be back!!

Anyway, I have been trying to "decipher" what this piece of C# code means in Vb.Net:

Code:
className.OnProcessUpdates += new ClassNameControl.ProcessUpdatesDelegate(this.DoSomething);

Now I have tried many things.

This is what a converter provides me with:
Code:
AddHandler className.OnProcessUpdates, AddressOf Me.DoSomething

Which is completely wrong. Converting back gives me System.EventHandler in C#

I have also tried this format:
Code:
className.OnProcessUpdates = New ClassNameControl.ProcessUpdatesDelegate(AddressOf Me.DoSomething)

It would be great if someone could enlighten me on how to work this problem out. It has been delaying my work for about 2 days now..

Thank you!!

----
I know this is not good forum practise, but this problem has been puzzling me for a very long time. I have been looking all over the internet without any luck.

I have asked fellow programmers, and all of them have no idea (perhaps because they work only with C#).

If someone has any idea or opinion, please do not hesitate to help out. Thanks.
-----
Actually,

Code:
className.OnProcessUpdates = New ClassNameControl.ProcessUpdatesDelegate(AddressOf Me.DoSomething)

does not give a syntax error, but it still gives an error from the DLL (which is a compiled C# project) at this line:

Code:
this.Owner.Dispatcher.Invoke((InvokeDelegate)delegate() { this.ProcessUpdates(); });

saying: Object reference not set to an instance of an object.

Basically the C# code in the first post does not cause this errror to occur. The problem i'm facing now is how to fix this in Vb.Net..
------

I apologise for the confusion. Hopefully here I will be able to obtain more help!

Thanks!
 
Code:
className.OnProcessUpdates += new ClassNameControl.ProcessUpdatesDelegate(this.DoSomething);
In the C# code it looks like you have a variable called className which has an event called OnProcessUpdates. The code is then creating an instance of a delegate called ProcessUpdatesDelegate and pointing it to a method in the current class called DoSomething and adding this as the event handler for the event.

The VB.Net code suggested by the converter does look correct however, when you say it is completely wrong does it fail in some way or give a compiler error?
 
If I use the code provided by the converter, it gives me this syntax error:
'OnProcessUpdates' is not an event of 'TFramework.ClassNameControl'

If you like, I could send the project to you (privately). It is small. But there are many dependencies..

EDIT:

OnProcessUpdates is a part of one of the libraries.
This is the declaration used:
Private className As ClassNameControl


I think it would not be clear for you unless you see more code.. It would be great if we could chat somehow, and I will post the outcome here for everyone to have a look at.
 
Last edited:
If it is possible to remove any binaries from the project then feel free to attach it here.

In fact if there is no way to remove the binaries then post it here anyway and I will be able to grab a copy and will then delete to attachment if that is ok.
 
In C#, the line of code I mentioned is in the Window1 code.

In Vb.Net, please take a look at MainMenu.. Inside, I have commented out everything I have tried, except for the last one, which is correct by syntax, but when the program is run gives the error:

Code:
this.Owner.Dispatcher.Invoke((InvokeDelegate)delegate() {this.ProcessUpdates(); });

Object reference not set to an instance of an object.

....

Please note that the above error will only show when a multitouch surface tracker is running. When the same tracker is running along with the C# project, no error shows. I'm pretty sure it's something to do with my translation of the code.

This means that the vb.net project will actually run as it is in the attachment, but that is not the desired result. It is supposed to work with the tracker running at the same time (which gives the above-mentioned error).

(The tracker keeps track of multi touch cursors/objects touching down and moving on a multitouch surface, and then sends that information using a protocol to the framework)..


EDIT: everytime I try to attach the zip file, it "uploads" for a very long time and suddenly just goes to an invalid page. This has been going on for the past 30 minutes and perhaps longer......
 
Last edited:
How big is the file? Shouldn't take too long at all to attach a file.

Does the C# version of the file work fine then and it is the VB conversion that fails? Is it failing on the C# version of the line above?

If it is have you tried checking in the debugger which of the variables in the line are null?
 
It is about 400 KB. It's very strange since I'm using a high speed internet connection. I will try again.

Yes the C# version works well. The Vb.Net is a manual conversion. I compiled the libraries from C#, and then referenced them in Vb.Net. The only thing I am converting to VB is the demo App.

I have checked which value is null, and it shows "this.owner" (in the C# library). I set breakpoints to check for the values of, and in the C# example, this.owner was Forms.Canvas (canvas1 of Window1). I coded that into the Vb.Net example, which in turn fixes the this.owner problem, but showing up another error, leading me to believe that there is actually a bigger problem somewhere else, where a function is failing to set all these properties.

When I get back home I will try to attach the project file again.

Thanks for the help so far!


Edit: Uploaded successfully...

[Removed attachment]
 
Last edited:
Back
Top