C# to VB.NET

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
I and also the translator on developerfusion have trouble translating the following lines of code to VB.NET. I hope you guys can help me out.

[CS]
operation.Post(new SendOrPostCallback(delegate(object state)
{
EventHandler handler = SomethingHappened;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
}), null);
operation.OperationCompleted();[/CS]

And just in case here is the whole code that I am trying to translate:
[CS]
using System;
using System.Threading;
using System.ComponentModel;

namespace SynchronizationContextExample
{
public class MySynchronizedClass
{
private Thread workerThread;
private AsyncOperation operation;
public event EventHandler SomethingHappened;
public MySynchronizedClass()
{
operation = AsyncOperationManager.CreateOperation(null);
workerThread = new Thread(new ThreadStart(DoWork));
workerThread.Start();
}

private void DoWork()
{
operation.Post(new SendOrPostCallback(delegate(object state)
{
EventHandler handler = SomethingHappened;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
}), null);
operation.OperationCompleted();
}
}
}
[/CS]
 
Last edited:
It looks like the converter is being tripped up by some of the differences between c# and vb.net.

Try
Visual Basic:
Imports System.Threading
Imports System.ComponentModel

Namespace SynchronizationContextExample
	Public Class MySynchronizedClass
		Private workerThread As Thread
		Private operation As AsyncOperation
		Public Event SomethingHappened As EventHandler

		Public Sub New()
			operation = AsyncOperationManager.CreateOperation(Nothing)
			workerThread = New Thread(New ThreadStart(AddressOf DoWork))
			workerThread.Start()
		End Sub

		Private Sub DoWork()
			operation.Post(New SendOrPostCallback(Sub(state As Object)

													  RaiseEvent SomethingHappened(Me, EventArgs.Empty)
												  End Sub), Nothing)
			operation.OperationCompleted()
		End Sub
	End Class
End Namespace
and see if that works (I think it will require .Net 4 though because of the use of a sub rather than a function for a lambda expression).
 
I downloaded VS2010Express and was dissappointed to find out they have made it a 30 trial. :(

How could I modify the code to work on VS2008Express using this lambda expression?
 
Jumpy, it looks to me like it's still free. You have 30 days to register, but no purchase required.
http://www.microsoft.com/express/Downloads/#2010-Visual-CS said:
Although Microsoft Visual Studio 2010 Express products are provided free of charge, we do require that you register your product within 30 days of installation for continued use. If you are not sure how to obtain your free Visual Studio 2010 Express registration key, follow these instructions.
 
Back
Top