Reputation: 1149
Hi I have a working very small vb.net project however it uses a proprietary com object which is wired in the VB project to handle events from it!
I have changed the name of the com object but syntactical the the following code is correct:
Public Class Form1
Private WithEvents PM As comObj.PManager
Delegate Sub SetTextCallback([text] As String)
Private Function PreTransmit(ByVal s As String) As String Handles PM.PreTransmit
Me.SetText(s)
Return s
End Function
Private Function PreResponse(ByVal s As String) As String Handles PM.PreResponse
Return s
End Function
Private Sub SetText(ByVal [text] As String)
If Me.textBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.textBox1.Text = [text]
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PM = CreateObject("ObjName.PManager")
End Sub
End Class
The above code creates the object when the button is clicked and whenever the object gets data it raises an event passing data into either pretransmit or prereceive.
I just cant seem to work out how to do this c#
vb
Private WithEvents PM As comObj.PManager
c#
private comObj.PManager PM;
but what about the WithEvents?
vb
Private Function PreResponse(ByVal s As String) As String Handles PM.PreResponse
Return s
End Function
c#
private string PreResponse(string s)
{
return s;
}
but what about the Handles PM.PreResponse?
vb
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PM = CreateObject("ObjName.PManager")
End Sub
c#
private void Button1_Click(System.Object sender, System.EventArgs e)
{
PM = Interaction.CreateObject("ObjName.PManager");
}
not sure were i'm going wrong!?
Could I write a vb.net class and use it in c#?
Upvotes: 2
Views: 2677
Reputation: 5150
but what about the Handles PM.PreResponse?
PM.PreResponse += PreResponse;
private void Button1_Click ...
PM = CreateObject("ObjName.PManager")
is the same as
PM = new comObj.PManager();
Upvotes: 1
Reputation: 37533
WithEvents is a given in C#. It is not necessary as a special consideration as it is in VB.Net. To add the handles for the PreResponse, in your load or init event (depending on if you're using asp.net or winforms), you need to add the event handler
PM.Response += PreResponse;
EDIT: To appease @Adam Robinson's semantic needs (rightly so since we live in a semantic world).
WithEvents gives the developer access to the Handles
keyword. This structure in fact does not exist in C# because event handling is only handled one way and that is with the explicit definition of the delegate (which is also available in VB.Net without the WithEvents keyword).
Upvotes: 1
Reputation: 273244
C# does not have the handles
syntax.
You can hook up your events in code (in FormLoaded):
PM.PreResponse += this.PreResponse;
Upvotes: 2
Reputation: 185643
VB.NET's WithEvents
keyword does not have an analog in C#. IIRC, VB.NET turns all WithEvents
variables into properties, then in the setter it attaches/detaches the event handlers explicitly like you'd see in C#.
Since C# does not have this feature, you'll have to keep track of this yourself. Since you're creating the object explicitly, this shouldn't be difficult to do. All you'll have to do for each event that you want to handle is:
PM.EventName += new EventHandlerType(EventHandlerMethodName);
In most cases, EventHandlerType
can be inferred by the compiler, so you don't need to include it. This means your code can turn into:
PM.PreResponse += this.PreResponse
Upvotes: 4