Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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

 

Now I have tried many things.

 

This is what a converter provides me with:

AddHandler className.OnProcessUpdates, AddressOf Me.DoSomething

 

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

 

I have also tried this format:

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!!

Posted

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.

Posted

Actually,

 

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:

 

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..

  • Leaders
Posted

In this code:

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

If OnProcessUpdates is an event, this should be the correct VB equivalent:

AddHandler className.OnProcessUpdates, AddressOf Me.DoSomething

However, it's hard to tell what exactly is going on in the C# code. C# syntax treats events very similarly to raw delegates. VB's event syntax, on the other hand, does not extend to normal delegates.

 

If we are dealing with a simple delegate rather than an event, which appears to be the case, the VB equivalent would probably be one of the following:

className.OnProcessUpdates = AddressOf Me.DoSomething
className.OnProcessUpdates = New ClassNameControl.ProcessUpdatesDelegate(AddressOf Me.DoSomething)
className.OnProcessUpdates = [Delegate].Combine(className.OnProcessUpdates, New ClassNameControl.ProcessUpdatesDelegate(AddressOf Me.DoSomething))

The first two are equivalent to each other. The third would be equivalent to C#'s += syntax. What you have, the second option, is not equivalent to the C# code because your code will replace one delegate with another if called multiple times. The original C# code will accumulate the delegates into a MulticastDelegate, which will call all functions that were added instead of only the most recent.

 

Depending on whether the delegate assignment code is called more than once, your code may or may not work as expected.

 

As to the NullReferenceException, you are most likely trying to invoke the delegate before it is assigned. Check to make sure the delegate is not Nothing before trying to invoke it.

[sIGPIC]e[/sIGPIC]
Posted

Your post absolutely makes things much clearer for me now. As you mentioned, yes it is not an event.

 

I have tried your code, and it is correct syntax wise. But it gives me the same NullReferenceException. I trust that the code should be working. It seems logical to me. I believe the error is most probably due to another reason.

 

I have created another thread earlier on in the General forum. It would be great if someone could merge both of these together:

http://www.xtremedotnettalk.com/showthread.php?p=470645

 

I have not worked much with delegates before, so I have attached the project for you to see (in the other thread). The C# equivalent can be downloaded from the link I posted (it is hosted on google code).

 

In the meantime, I will try to figure out where the problem really is...

Posted

Thanks a lot for your help! The problem has been resolved. It seems that the code you provided me with works well. But like I mentioned, it was still giving me the NullReferenceException.

 

I stepped into the code on both the vb.Net and the C# side, and I noticed that at one point, all configurations loaded become Nothing on the vb.Net side.

 

It turns out that one of the functions (which I was not able to convert manually) had missing lines of code, whereby the auto converter just cut them off.

 

For future reference, an example is as follows:

 

C#:

  ExampleConfig eConfig = new ExampleConfig()
           {
               FlipX = AppConfig.FlipX,
               FlipY = AppConfig.FlipY
           };

 

Will be converted simply as:

Dim eConfig As New ExampleConfig()

 

When it should be more like:

Dim eConfig As New ExampleConfig()
       With eConfig 
           .FlipX = AppConfig.FlipX
           .FlipY = AppConfig.FlipY
       End With

 

Thanks again for your help regarding the delegates!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...