C# To Vb.net

kaizen

Member
Joined
Mar 12, 2008
Messages
8
Programming Experience
5-10
Hi guys,

I am trying to convert some C# code into Vb.NEt and it is giving me some hassles.

The C# code is below.

VB.NET:
using System;
using System.Collections.Generic;
using System.Text;
using RightEdge.Common;

using fxClientAPI;
using System.Diagnostics;
using System.Threading;

[Serializable]
public class OrderEventHandler : fxAccountEvent
{

	private OandaPlugin _plugin;

	public OrderEventHandler(OandaPlugin plugin)
	{
		_plugin = plugin;
	}

	private void Handle(fxAccountEventInfo info)
	{
		bool matched;
		OandaPlugin.PendingItem item = new OandaPlugin.PendingItem(info);
		_plugin.HandleTransaction(item, out matched);
		if (!matched)
		{
			_plugin._pendingTransactions.Add(item);
			_plugin._synchContext.Post(delegate
			{
				_plugin.NewTick();
			}, null);
		}
	}

	public void DoEvent(fxEventInfo ei)
	{
		if (ei is fxAccountEventInfo)
		{
			fxAccountEventInfo info = (fxAccountEventInfo)ei;

			string msg = "Oanda: " + info.Transaction.Description + "  Trans: " + info.Transaction.TransactionNumber +
				"  Link: " + info.Transaction.Link + "  Qty: " + info.Transaction.Units;

			if (Thread.CurrentThread != _plugin.mainThread)
			{
				//msg = "Off main thread: " + msg;
				//Trace.WriteLine(msg);
				_plugin._synchContext.Post(delegate
				{
					Handle(info);
					
				}, null);
			}
			else
			{
				//Trace.WriteLine(msg);
				Handle(info);
			}
		}
		else
		{
			if (Thread.CurrentThread != _plugin.mainThread)
			{
				_plugin._synchContext.Post(delegate
				{
					_plugin.Log("Unknown argument type for match: " + ei.GetType().ToString());
				}, null);
			}
			else
			{
				_plugin.Log("Unknown argument type for match: " + ei.GetType().ToString());
			}
		}
	}
	public override bool match(fxEventInfo ei)
	{
		DoEvent(ei);
		return base.match(ei);
	}

	public override void handle(fxEventInfo ei, fxEventManager em)
	{
		DoEvent(ei);
		base.handle(ei, em);
	}
}

Any my converted Vb.Net is below.

VB.NET:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports RightEdge.Common

Imports fxClientAPI
Imports System.Diagnostics
Imports System.Threading

Public Class OrderEventHandler
    Inherits fxAccountEvent

    Private _plugin As OandaPlugin

    Public Sub New(ByVal plugin As OandaPlugin)
        _plugin = plugin
    End Sub

    Private Sub Handle(ByVal info As fxAccountEventInfo)
        Dim matched As Boolean
        Dim item As OandaPlugin.PendingItem = New OandaPlugin.PendingItem(info)
		_plugin.HandleTransaction(item,out matched)
        If Not matched Then
            _plugin._pendingTransactions.Add(item)
           [COLOR="Red"] _plugin._synchContext.Post(delegate	{ _plugin.NewTick()}, Nothing)[/COLOR]
        End If
    End Sub

    Public Sub DoEvent(ByVal ei As fxEventInfo)
        If TypeOf ei Is fxAccountEventInfo Then
            Dim info As fxAccountEventInfo = CType(ei, fxAccountEventInfo)

            Dim msg As String = "Oanda: " + info.Transaction.Description + "  Trans: " + info.Transaction.TransactionNumber + _
    "  Link: " + info.Transaction.Link + "  Qty: " + info.Transaction.Units

            If [COLOR="Red"]Thread.CurrentThread <> _plugin.mainThread[/COLOR] Then
                'msg = "Off main thread: " + msg;
                'Trace.WriteLine(msg);
				[COLOR="Red"]_plugin._synchContext.Post(delegate	{  Handle(info)}, Nothing)[/COLOR]
            Else
                'Trace.WriteLine(msg);
                Handle(info)
            End If
        Else
            If [COLOR="Red"]Thread.CurrentThread <> _plugin.mainThread[/COLOR] Then
				[COLOR="Red"]_plugin._synchContext.Post(delegate	{_plugin.Log("Unknown argument type for match: " + ei.GetType().ToString())}, Nothing)[/COLOR]
            Else
                _plugin.Log("Unknown argument type for match: " + ei.GetType().ToString())
            End If
        End If
    End Sub
    Public Overrides Function match(ByVal ei As fxEventInfo) As Boolean
        DoEvent(ei)
        Return MyBase.match(ei)
    End Function

    Public Overrides Sub handle(ByVal ei As fxEventInfo, ByVal em As fxEventManager)
        DoEvent(ei)
        MyBase.handle(ei, em)
    End Sub
End Class


The code in red is the code I am not sure about. It has to do with multi threading and delegates which I am not very clued up on.

Any ideas?

Thanks
 
This will need some tweaking:

VB.NET:
Imports System 
Imports System.Collections.Generic 
Imports System.Text 
Imports RightEdge.Common 

Imports fxClientAPI 
Imports System.Diagnostics 
Imports System.Threading 

<Serializable()> _ 
Public Class OrderEventHandler 
    Inherits fxAccountEvent 
    
    Private _plugin As OandaPlugin 
    
    Public Sub New(ByVal plugin As OandaPlugin) 
        _plugin = plugin 
    End Sub 
    
    Private Sub Handle(ByVal info As fxAccountEventInfo) 
        Dim matched As Boolean 
        Dim item As New OandaPlugin.PendingItem(info) 
        _plugin.HandleTransaction(item, matched) 
        If Not matched Then 
            _plugin._pendingTransactions.Add(item) 
            _plugin._synchContext.Post(AddressOf ConvertedAnonymousMethod1, Nothing) 
        End If 
    End Sub 
    
    Public Sub DoEvent(ByVal ei As fxEventInfo) 
        If TypeOf ei Is fxAccountEventInfo Then 
            Dim info As fxAccountEventInfo = DirectCast(ei, fxAccountEventInfo) 
            
            Dim msg As String = "Oanda: " & info.Transaction.Description & " Trans: " & info.Transaction.TransactionNumber & " Link: " & info.Transaction.Link & " Qty: " & info.Transaction.Units 
            
            If Thread.CurrentThread <> _plugin.mainThread Then 
                'msg = "Off main thread: " & msg; 
                'Trace.WriteLine(msg); 
                    
                _plugin._synchContext.Post(AddressOf ConvertedAnonymousMethod2, Nothing) 
            Else 
                'Trace.WriteLine(msg); 
                Handle(info) 
            End If 
        Else 
            If Thread.CurrentThread <> _plugin.mainThread Then 
                _plugin._synchContext.Post(AddressOf ConvertedAnonymousMethod3, Nothing) 
            Else 
                _plugin.Log("Unknown argument type for match: " & ei.[GetType]().ToString()) 
            End If 
        End If 
    End Sub 
    Public Overloads Overrides Function match(ByVal ei As fxEventInfo) As Boolean 
        DoEvent(ei) 
        Return MyBase.match(ei) 
    End Function 
    
    Public Overloads Overrides Sub handle(ByVal ei As fxEventInfo, ByVal em As fxEventManager) 
        DoEvent(ei) 
        MyBase.handle(ei, em) 
    End Sub 
    Private Sub ConvertedAnonymousMethod1() 
        _plugin.NewTick() 
    End Sub 
    Private Sub ConvertedAnonymousMethod2() 
        Handle(info) 
    End Sub 
    Private Sub ConvertedAnonymousMethod3() 
        _plugin.Log("Unknown argument type for match: " & ei.[GetType]().ToString()) 
    End Sub 
End Class

VBN doesnt do anonymous methods.. You know you can just have me compile this C# code into a DLL that you can call, right? If you have Visual Studio (not just VB) you can even do that yourself and you can add the project as C# and debugger will step between them.. There isnt actually any difference between C# and VB under the hood.. its just keywords

Or, you could learn C#.. mmm.. much cleaner, nicer, stricter language! :)
 
Last edited:
The code you posted if from another C# to Vb.net converter. I have tired that but did not have any luck either.

For some reason you can not use

VB.NET:
If Thread.CurrentThread <> _plugin.mainThread Then

in vb.net. Any ideas?

The main problem is with the _synchContext.Post(...) parts. The rest I can convert.


I could go learn C# but I have no desire to learn another language. And using the code in the Dll is not an option either.

Thanks.
 
The code you posted if from another C# to Vb.net converter.
Yep, they all have their frailties. Pick the one that gets closest, then tweak it.
Given that this one made 3 anonymous methods and AddressOfed them, i figured it was closer than the previous attempt

I have tired that but did not have any luck either.
Well if it was 100% automated then we wouldnt really need more than one language.. SOrry it wasnt perfect, but you will need to put some code in

For some reason you can not use
What reason? I'm fairly sure Microsoft's compiler wouldnt highlight this line and say "Error: We arent quite sure why, but you cant do that"
If it results in error, what's the error?
If it results in weird behaviour, what's the weird behaviour?


VB.NET:
If Thread.CurrentThread <> _plugin.mainThread Then

in vb.net. Any ideas?
Given that I dont *really* know why it's there at all, erm.. no ideas at this time, sorry

The main problem is with the _synchContext.Post(...) parts. The rest I can convert.
I do beleive the DF converter had a reasonably good go at this part, so convert the rest yourself, and just copy and paste those delegated methods?

If you can convert the rest yourself, and the Thread.CurrentThread <> _plugin.Thread issue isnt present in the rest of the code that you can convert , I'm a bit puzzled as to why you cant fix the problem when you just said you can write code that doesnt have the problem?


I could go learn C# but I have no desire to learn another language.
Good job you didnt say that about VB.NET when you only spoke English! :D Such learning is always good, especially professionally; sad that C# guys get paid more than VB guys :/ but that's vanity, skew perception and ignorance for you!


Thanks.[/QUOTE]
 
Thread is a class and you can compare instances with the instance comparison method Equals or using the instance comparison operators Is/IsNot.
 
Back
Top