Can someone please translate this to VB.Net

M

Mike

public class Broadcaster: MarshalByRefObject, IBroadcaster
{

public event General.MessageArrivedHandler MessageArrived;

public void BroadcastMessage(string msg) {
Console.WriteLine("Will broadcast message: {0}", msg);
SafeInvokeEvent(msg);
}

private void SafeInvokeEvent(String msg) {
// call the delegates manually to remove them if they aren't
// active anymore.

if (MessageArrived == null) {
Console.WriteLine("No listeners");
} else {
Console.WriteLine("Number of Listeners:
{0}",MessageArrived.GetInvocationList().Length);
MessageArrivedHandler mah=null;

foreach (Delegate del in MessageArrived.GetInvocationList()) {
try {
mah = (MessageArrivedHandler) del;
mah(msg);
} catch (Exception e) {
Console.WriteLine("Exception occured, will remove Delegate");
MessageArrived -= mah;
}
}
}
}

public override object InitializeLifetimeService() {
// this object has to live "forever"
return null;
}
}


class ServerStartup
{
static void Main(string[] args)
{
String filename = "server.exe.config";
RemotingConfiguration.Configure(filename);

Console.WriteLine ("Server started, press <return> to exit.");
Console.ReadLine();
}
}
 
G

Guest

(via Instant VB - note that there is no IBroadcaster defined in the original
code or the .NET Framework, so you'll have to add the "Implements" tags to
the methods yourself)

Public Class Broadcaster
Inherits MarshalByRefObject
Implements IBroadcaster

Public Event MessageArrived As General.MessageArrivedHandler

Public Sub BroadcastMessage(ByVal msg As String)
Console.WriteLine("Will broadcast message: {0}", msg)
SafeInvokeEvent(msg)
End Sub

Private Sub SafeInvokeEvent(ByVal msg As String)
' call the delegates manually to remove them if they aren't
' active anymore.

If MessageArrivedEvent Is Nothing Then
Console.WriteLine("No listeners")
Else
Console.WriteLine("Number of Listeners:
{0}",MessageArrivedEvent.GetInvocationList().Length)
Dim mah As MessageArrivedHandler=Nothing

For Each del As System.Delegate In MessageArrivedEvent.GetInvocationList()
Try
mah = CType(del, MessageArrivedHandler)
mah(msg)
Catch e As Exception
Console.WriteLine("Exception occured, will remove Delegate")
RemoveHandler MessageArrived, mah
End Try
Next del
End If
End Sub

Public Overrides Function InitializeLifetimeService() As Object
' this object has to live "forever"
Return Nothing
End Function
End Class


Friend Class ServerStartup
Shared Sub Main(ByVal args As String())
Dim filename As String = "server.exe.config"
RemotingConfiguration.Configure(filename)

Console.WriteLine ("Server started, press <return> to exit.")
Console.ReadLine()
End Sub
End Class

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Mike said:
public class Broadcaster: MarshalByRefObject, IBroadcaster
{

public event General.MessageArrivedHandler MessageArrived;

public void BroadcastMessage(string msg) {
Console.WriteLine("Will broadcast message: {0}", msg);
SafeInvokeEvent(msg);
}

private void SafeInvokeEvent(String msg) {
// call the delegates manually to remove them if they aren't
// active anymore.

if (MessageArrived == null) {
Console.WriteLine("No listeners");
} else {
Console.WriteLine("Number of Listeners:
{0}",MessageArrived.GetInvocationList().Length);
MessageArrivedHandler mah=null;

foreach (Delegate del in MessageArrived.GetInvocationList()) {
try {
mah = (MessageArrivedHandler) del;
mah(msg);
} catch (Exception e) {
Console.WriteLine("Exception occured, will remove Delegate");
MessageArrived -= mah;
}
}
}
}

public override object InitializeLifetimeService() {
// this object has to live "forever"
return null;
}
}


class ServerStartup
{
static void Main(string[] args)
{
String filename = "server.exe.config";
RemotingConfiguration.Configure(filename);

Console.WriteLine ("Server started, press <return> to exit.");
Console.ReadLine();
}
}
 
M

Mike

Thanks David!

David Anton said:
(via Instant VB - note that there is no IBroadcaster defined in the
original
code or the .NET Framework, so you'll have to add the "Implements" tags to
the methods yourself)

Public Class Broadcaster
Inherits MarshalByRefObject
Implements IBroadcaster

Public Event MessageArrived As General.MessageArrivedHandler

Public Sub BroadcastMessage(ByVal msg As String)
Console.WriteLine("Will broadcast message: {0}", msg)
SafeInvokeEvent(msg)
End Sub

Private Sub SafeInvokeEvent(ByVal msg As String)
' call the delegates manually to remove them if they aren't
' active anymore.

If MessageArrivedEvent Is Nothing Then
Console.WriteLine("No listeners")
Else
Console.WriteLine("Number of Listeners:
{0}",MessageArrivedEvent.GetInvocationList().Length)
Dim mah As MessageArrivedHandler=Nothing

For Each del As System.Delegate In MessageArrivedEvent.GetInvocationList()
Try
mah = CType(del, MessageArrivedHandler)
mah(msg)
Catch e As Exception
Console.WriteLine("Exception occured, will remove Delegate")
RemoveHandler MessageArrived, mah
End Try
Next del
End If
End Sub

Public Overrides Function InitializeLifetimeService() As Object
' this object has to live "forever"
Return Nothing
End Function
End Class


Friend Class ServerStartup
Shared Sub Main(ByVal args As String())
Dim filename As String = "server.exe.config"
RemotingConfiguration.Configure(filename)

Console.WriteLine ("Server started, press <return> to exit.")
Console.ReadLine()
End Sub
End Class

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Mike said:
public class Broadcaster: MarshalByRefObject, IBroadcaster
{

public event General.MessageArrivedHandler MessageArrived;

public void BroadcastMessage(string msg) {
Console.WriteLine("Will broadcast message: {0}", msg);
SafeInvokeEvent(msg);
}

private void SafeInvokeEvent(String msg) {
// call the delegates manually to remove them if they aren't
// active anymore.

if (MessageArrived == null) {
Console.WriteLine("No listeners");
} else {
Console.WriteLine("Number of Listeners:
{0}",MessageArrived.GetInvocationList().Length);
MessageArrivedHandler mah=null;

foreach (Delegate del in MessageArrived.GetInvocationList()) {
try {
mah = (MessageArrivedHandler) del;
mah(msg);
} catch (Exception e) {
Console.WriteLine("Exception occured, will remove Delegate");
MessageArrived -= mah;
}
}
}
}

public override object InitializeLifetimeService() {
// this object has to live "forever"
return null;
}
}


class ServerStartup
{
static void Main(string[] args)
{
String filename = "server.exe.config";
RemotingConfiguration.Configure(filename);

Console.WriteLine ("Server started, press <return> to exit.");
Console.ReadLine();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top