Percy Kumah Percy Kumah - 5 months ago 40
Vb.net Question

Using Delegates in vb.net

I'm new to vb.net and trying to build a win CE app in vb.net. The sdk of the handheld device is in C# which I converted with an online converter to vb.net.
Below is the C# code:

public class DecodeEventArgs : EventArgs
{
private string barcode;
private byte type;

public DecodeEventArgs(string barcodeData, byte typeData)
{
barcode = barcodeData;
type = typeData;
}

public string Barcode
{
get { return barcode; }
set { barcode = value; }
}

public byte Type
{
get { return type; }
set { type = value; }
}

}


Converted to vb.net as:

Public Class DecodeEventArgs
Inherits EventArgs
Public barcode As String
Public type As Byte

Public Sub New(ByVal barcodeData As String, ByVal typeData As Byte)
barcode = barcodeData
type = typeData
End Sub

Public Property pBarcode() As String
Get
Return barcode
End Get
Set(ByVal value As String)
barcode = value
End Set
End Property

Public Property pType() As Byte
Get
Return type
End Get
Set(ByVal value As Byte)
type = value
End Set
End Property
End Class


In my conversion from C# I added a 'p' to the name of the Properties as you see in my vb.net code because I had a error that said


Barcode is already declared as a public string in this class


I'm not sure if that is part of my problem but my real issue is, on a form they used .BeginInvoke to call the class with this code:

void scanner_DecodeEvent(object sender, DecodeEventArgs e)
{
Win32.sndPlaySound(Properties.Resources.Scan, Win32.SND_ASYNC | Win32.SND_MEMORY);

this.BeginInvoke((Action<string>)delegate(string barcode)
{
scanCount = 0;
ListViewItem item = new ListViewItem(new string[] { barcode });
lstView.Items.Insert(0, item);

}, e.Barcode);
}


Which I converted to vb.net as:

Private Sub scanner_DecodeEvent(ByVal sender As Object, ByVal e As DecodeEventArgs)
PlaySound()

Me.BeginInvoke(DirectCast(Barcode As String) )
scanCount = 0
Dim item As New ListViewItem(New String() {barcode})

lstView.Items.Insert(0, item)


End Sub


Which gives me an error about Barcode not being declared. This is where I'm stuck. Thanks for your assistance in advance

Answer

That C# snippet creates an anonymous method which it invokes to perform the actions on the UI thread, and it sends e.Barcode as a parameter. Your VB.NET conversion only tries to invoke some strange, and incomplete, use of DirectCast. DirectCast is not needed in the VB.NET conversion as you don't have any delegate keyword which you must cast to a delegate method.

Your most simple solution would be to use a Lambda method:

Me.BeginInvoke(Sub() 'Lambda anonymous method.
    scanCount = 0
    Dim item As New ListViewItem(New String() {e.Barcode})
    lstView.Items.Insert(0, item)
End Sub)

EDIT:

Since you get errors when using the lambda expression I assume you target .NET Framework 3.5 or lower. For that matter it gets a bit more complicated as you must now put the code in a different method:

Private Sub AddBarcode(ByVal Barcode As String)
    scanCount = 0
    Dim item As New ListViewItem(New String() {Barcode})
    lstView.Items.Insert(0, item)
End Sub

Then you must declare your own delegate method which you can use to perform the invocation:

Delegate Sub AddBarcodeDelegate(ByVal Barcode As String)

Private Sub scanner_DecodeEvent(ByVal sender As Object, ByVal e As DecodeEventArgs)
    Me.BeginInvoke(New AddBarcodeDelegate(AddressOf AddBarcode), e.Barcode)
End Sub