CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2011
    Posts
    4

    RaiseEvent from VB.net to CSharp problem

    RaiseEvent from VB.net to CSharp problem

    I am converting my vb.net project to CSharp, everything work fine except the RaiseEvent. I totally have no idea how to do it, can anybody help me?
    Thank so much.

    I have a custom button control and following is part of the code:

    Code:
    Public Class EnquiryButton
        Inherits System.Windows.Forms.Button
    
        Public Event EnquiryReturned(ByVal sender As Object, ByVal e As System.EventArgs)
        Private Event ReturnObjectChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    
    
        Private Sub BindReturnObjectToTargetControl()
            ' Assign Values to Control
            If Not Me.ReturnValue_TargetControl Is Nothing Then
                CallByName(Me.ReturnValue_TargetControl, Me.ReturnValue_TargetControlPropertyName, CallType.Set, Me.ReturnObject.ReturnValue)
            End If
            If Not Me.ReturnDescription_TargetControl Is Nothing Then
                CallByName(Me.ReturnDescription_TargetControl,   Me.ReturnDescription_TargetControlPropertyName, CallType.Set, Me.ReturnObject.ReturnDescription)
            End If
    
        End Sub
    
    
        Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
            Dim retObj As Enquiry.ReturnObject = FetchReturnObject(Type)
            If Not retObj Is Nothing Then
                _ReturnObject = retObj
                OnReturnObjectChanged(System.EventArgs.Empty)
            End If
            OnEnquiryReturned(System.EventArgs.Empty)
            MyBase.OnClick(e)
        End Sub
    
    
        Protected Sub OnReturnObjectChanged(ByVal e As EventArgs)
            BindReturnObjectToTargetControl()
            RaiseEvent ReturnObjectChanged(Me, System.EventArgs.Empty)
        End Sub
    
        Protected Sub OnEnquiryReturned(ByVal e As EventArgs)
            RaiseEvent EnquiryReturned(Me, System.EventArgs.Empty)
        End Sub
    
    End Class
    Last edited by mansonho; July 11th, 2011 at 08:54 PM.

  2. #2
    Join Date
    Jul 2011
    Posts
    4

    Re: RaiseEvent from VB.net to CSharp problem

    Sorry, forgot to post my .net version

    my .net version v4.0.30319

    VS 2008 Express

    Thank
    Last edited by mansonho; July 11th, 2011 at 08:55 PM.

  3. #3
    Join Date
    Aug 2005
    Posts
    198

    Re: RaiseEvent from VB.net to CSharp problem

    Just drop the 'RaiseEvent' keyword.

    Note that 'CallByName' is not available in C# unless you set a reference to the Microsoft.VisualBasic assembly.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: RaiseEvent from VB.net to CSharp problem

    C# doesn't use a special keyword to raise an event - it just treats it as if it were a delegate.

    So, the C# equivalent to
    Code:
        Protected Sub OnReturnObjectChanged(ByVal e As EventArgs)
            BindReturnObjectToTargetControl()
            RaiseEvent ReturnObjectChanged(Me, System.EventArgs.Empty)
        End Sub
    would be

    Code:
    protected void OnReturnObjectChanged(EventArgs e)
    {
        BindReturnObjectToTargetControl();
        ReturnObjectChanged(this, System.EventArgs.Empty);
    }
    However, with the earlier version of the framework, a good practice was to check if there are any methods subscribed to the event before raising it, and although I think I read somewhere that this is no longer necessary, I'm not sure - so the compete version would be:

    Code:
    protected void OnReturnObjectChanged(EventArgs e)
    {
        BindReturnObjectToTargetControl();
        
        if(ReturnObjectChanged != null)
            ReturnObjectChanged(this, System.EventArgs.Empty);
    }
    I'm a bit curious though: why are you converting from VB.NET to C#? Your .NET code should be able to work with pretty much any other .NET language. If this is a DLL, you can reference it from a C# project.

    EDIT: ninja'd

  5. #5
    Join Date
    Jul 2011
    Posts
    4

    Re: RaiseEvent from VB.net to CSharp problem

    Hi David, TheGreatCthulhu

    Thank very much for your reply.

    Actually, I don't know why the event is add here, since there is not handler method for the event.
    And how can I instantiate it in CSharp without the handler method?
    Can you explain?

    Thank.

    For the CallByName problem, I use Reflection to solve it.

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: RaiseEvent from VB.net to CSharp problem

    Quote Originally Posted by mansonho View Post
    Actually, I don't know why the event is add here, since there is not handler method for the event.
    If I understood your question well, you're asking why the code has the OnReturnObjectChanged() method?
    If so, it's simple - it's true that this is not a handler, this is the method that is used to raise the corresponding event. The class that contains it is the only one that "knows" when it's appropriate to raise the event, and it uses this method to do so, and also to perform some additional processing.

    Look what OnClick() does:
    Code:
            Dim retObj As Enquiry.ReturnObject = FetchReturnObject(Type)
            If Not retObj Is Nothing Then
                _ReturnObject = retObj
                OnReturnObjectChanged(System.EventArgs.Empty)
            End If
    Quote Originally Posted by mansonho View Post
    And how can I instantiate it in CSharp without the handler method?
    Can you explain?
    VB.NET is a .NET language. All .NET code compiles to the same Intermediate Language (IL) code, no matter in what high-level language it was written. This IL is then, at appropriate time, compiled by the .NET runtime into a platform specific format. This enables two things: (1) code written in any .NET language can cooperate (VB.NET code can work with C# code or C++/CLI code, and vice versa), more or less without the need to make any modifications; and (2) any .NET application is portable to any platform that has some form of .NET runtime installed (for example, it can run on Mono, an open source implementation of Common Language Runtime (CLR) for Linux, Windows, and Mac OS X).

    Basically, all you need to do is to add a reference to your VB assembly as you would do with any other .NET assembly. Usually, some of these references are added automatically by the IDE, based on your project type, but it's not hard to add them yourself.

    In C#, expand the Solution Explorer tree, and right-click on References, then choose "Add Reference...". When the window shows up, go to the Browse tab, and navigate to your assembly (an .exe or, preferably a .dll file, created from your VB.NET code).

    Now you can use all the publicly exposed types in that assembly, the same way you use all other .NET types (remember, the whole .NET library is just a bunch of DLLs).

    You'll also want to add the using directives at the top of your .cs file:
    using Whatever.VB.Namespace.YouDefined;
    Last edited by TheGreatCthulhu; July 21st, 2011 at 11:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured