• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

C# to VB.NET

JumpyNET

Centurion
Joined
Apr 4, 2005
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:

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
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).
 

JumpyNET

Centurion
Joined
Apr 4, 2005
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?
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
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.
 
Top Bottom