2

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#?

3
  • 2
    have you tried a converter like: developerfusion.com/tools/convert/vb-to-csharp Commented Jan 21, 2011 at 17:47
  • Thanks this has apparently got most of it but one line I still have an issue with that stops me compiling it. PM = Interaction.CreateObject("ObjName.PManager"); this remains the same and google said its in the Microsoft.VisualBasic name space However I get Error 1 The name 'Interaction' does not exist in the current context Commented Jan 21, 2011 at 18:29
  • Got it working now I missed the VB reference! Commented Jan 21, 2011 at 20:03

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you and all the others who have posted similar comments they have helped and in the VB -> c# translate given above it did as you say. however I still have one problem before it will compile as above comment.
2

C# does not have the handles syntax.

You can hook up your events in code (in FormLoaded):

 PM.PreResponse += this.PreResponse;

Comments

1

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).

3 Comments

Err, no, WithEvents does not exist in C#.
"Does not exist" v "is a given". They both boil down to: "It's not necessary."
I disagree...one boils down to "You don't need to worry about it", the other boils down to "You can't do it, so you have to do something else."
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.