-2

I am a C# programmer and have made a C# library for handling a tcp/ip chat. Now I have the necessity to use it from a Server VB Winform program. I am pretty sure that this is an easy solution problem but I have been struggling for days. So in the C# I have that class:

public class AsynchronousServer
{
    public AsynchronousServer()
    {
    }

    public delegate void ChangedEventHandler(string strMessage);
    public static event ChangedEventHandler OnNotification;
    public static event ChangedEventHandler OnAnswerReceived;
    ...
}

Now I have to focus on the server program: if that VB program would have been in C# I would have written the code below for connecting the server on a button click

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        AsynchronousServer.OnNotification += AsynchronousServer_OnNotification;
        AsynchronousServer.OnAnswerReceived += AsynchronousServer_OnAnswerReceived;
        AsynchronousServer.StartServer(tbxIpAddress.Text,tbxPort.Text);
    }

The same program in VB:

Imports SocketLibrary

    Public Class Form1
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        AsynchronousServer.OnNotification                       <--- member not present
        AsynchronousServer.OnAnswerReceived                     <--- member not present
        AsynchronousServer.StartServer(tbxIpAddress.Text, tbxPort.Text);<-----OK
    End Sub
End Class

the problem is that the member AsynchronousServer.OnNotification is not present at all so I can't add the event. I am aware that I might have to add the WithEvents keyword but try as I might I couldn't succed.

In short I have to connect that VB winform program to a C# library event which I can't see from my VB class.

Thanks for any help

Patrick

Patrick
  • 3,073
  • 2
  • 22
  • 60

2 Answers2

2

Visual Basic has AddHandler and RemoveHandler which are roughly equivalent to C#s += and -= when the item to the left is an event.

However, you also have the option to declare some fields (so, more relevant for instance events on class instances) as WithEvents1. Whenever you assign a reference to a WithEvents field, it will automagically remove previous event handlers it installed on the old instance and then install new handlers on the new instance. This works in concert with the Handles clause which indicates which event handling methods should be hooked up. This lets you take a more "declarative" approach to your event handling. But it will not work with static events.

(Note, also, that of course it's generally easy to consume up C# events from VB code - since WinForms, etc, were implemented in C# but usable from both languages)


1I mention this here not because I necessarily recommend it but because you will encounter a fair bit of VB.NET code that uses this form, including form-designer generated code. And in pre-.NET VB, it was the way of wiring up event handlers.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

I'm assuming you never used VB thus will answer you:
You need to use AddHandler:

AddHandler AsynchronousServer.OnNotification, AddressOf YourMethod

BTW, static events are pretty bad IMHO.

Haytam
  • 4,643
  • 2
  • 20
  • 43