Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert "RW XML Files, Config Files, INI files, or the Registry", an old article by Alvaro Mendez, from C# to vb.net. I use a very good program named "InstantVB" for the conversion, but it stumbled on one item out of all the code. It has to do with converting the use of two events. I am not very experienced with using events in general, and don't know where to start in trying to fix this. Any help would be appreciated. I have included the relevant code from the original C# and the VB.net code from the Converter.

Any help would be very much appreciated.

RCTaubert

The C# code in question is as follows (I marked the two culprit lines at the end with an asterisk:
##############################################################
C#
namespace AMS.Profile
{
    /// <summary>
    ///   Abstract base class for all Profile classes in this namespace.
    /// </summary>
    /// <remarks>
    ///   This class contains fields and methods which are common for all the
    ///   derived Profile classes.
    ///   It fully implements most of the methods and properties of its base
    ///   interfaces so that
    ///   derived classes don't have to. </remarks>
    public abstract class Profile : IProfile
    {
        // Fields
        private string m_name;
        private bool m_readOnly;

        /// <summary>
        ///   Event used to notify that the profile is about to be changed.
        /// <summary>
        /// <seealso cref="Changed" />
        public event ProfileChangingHandler Changing;

        /// <summary>
        ///   Event used to notify that the profile has been changed.
        /// </summary>
        /// <seealso cref="Changing" />
        public event ProfileChangedHandler Changed;

        /// <summary>
        ///   Initializes a new instance of the Profile class by setting the
        ///   <see cref="Name" /> to <see cref="DefaultName" />. </summary>
        protected Profile()
        {
            m_name = DefaultName;
        }

        /// <summary>
        /// Initializes a new instance of the Profile class by setting the
        /// <see cref="Name" /> to a value. </summary>
        /// <param name="name">
        ///   The name to initialize the <see cref="Name" /> property with. </param>
        protected Profile(string name)
        {
            m_name = name;
        }

        /// <summary>
        ///   Initializes a new instance of the Profile class based on another Profile object. </summary>
        /// <param name="profile">
        ///   The Profile object whose properties and events are used to initialize the object being constructed. </param>
        protected Profile(Profile profile)
        {
            m_name = profile.m_name;
            m_readOnly = profile.m_readOnly;
*           Changing = profile.Changing;
*           Changed = profile.Changed;
        }

##############################################################

It translates to VB.net as (again I use an asterisk):
##############################################################
C#
Namespace AMS.Profile

    ''' <summary>
    '''   Abstract base class for all Profile classes in this namespace.
    ''' </summary>
    ''' <remarks>
    '''   This class contains fields and methods which are common for all
    '''   the derived Profile classes.
    '''   It fully implements most of the methods and properties of its
    '''   base interfaces so that derived classes don't have to. </remarks>
    Public MustInherit Class Profile

        Implements IProfile

        ' Fields
        Private m_name As String
        Private m_readOnly As Boolean

        ''' <summary>
        '''   Event used to notify that the profile is about to be changed.
        ''' </summary>
        ''' <seealso cref="Changed" />
        Public Event Changing As ProfileChangingHandler Implements IProfile.Changing

        ''' <summary>
        '''   Event used to notify that the profile has been changed.
        ''' </summary>
        ''' <seealso cref="Changing" />
        Public Event Changed As ProfileChangedHandler Implements IProfile.Changed

        ''' <summary>
        '''   Initializes a new instance of the Profile class by setting the
        '''   <see cref="Name" /> to <see cref="DefaultName" />. </summary>
        Protected Sub New()

            m_name = DefaultName

        End Sub

        ''' <summary>
        '''   Initializes a new instance of the Profile class by setting the
        '''   <see cref="Name" /> to a value. </summary>
        ''' <param name="name">
        '''   The name to initialize the <see cref="Name" /> property with. </param>
        Protected Sub New(ByVal name As String)

            m_name = name

        End Sub

        ''' <summary>
        '''   Initializes a new instance of the Profile class based on another
        '''    Profile object. </summary>
        ''' <param name="profile">
        '''   The Profile object whose properties and events are used to
        '''   initialize the object being constructed. </param>
        Protected Sub New(ByVal profile As Profile)

            m_name = profile.m_name
            m_readOnly = profile.m_readOnly
*            AddHandler Changing, profile.Changing

*            AddHandler Changed, profile.Changed

        End Sub
##############################################################

VB.net's complaint about the lines are as follows:

VB
'Public Event Changing(sender As Object, e As ProfileChangingArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

'Public Event Changed(sender As Object, e As ProfileChangedArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Can you possibly tell me how this should be handled in VB.net.

Thank you.
Posted
Updated 9-Aug-12 11:28am
v2
Comments
Sergey Alexandrovich Kryukov 9-Aug-12 17:25pm    
Convert? Usage? Of the event? :-)
--SA
[no name] 9-Aug-12 17:31pm    
http://msdn.microsoft.com/en-us/library/fwd3bwed(v=VS.71).aspx
rctaubert 9-Aug-12 17:56pm    
Thank you for your response Wes. Based on the link you provided I tried this:

Public Event Changing As ProfileChangingHandler Implements IProfile.Changing
Public Sub ProfileChanging()

RaiseEvent Changing(Nothing, Nothing)

End Sub

Public Event Changed As ProfileChangedHandler Implements IProfile.Changed
Public Sub ProfileChanged()

RaiseEvent Changed(Nothing, Nothing)

End Sub

Protected Sub New(ByVal profile As Profile)

m_name = profile.m_name
m_readOnly = profile.m_readOnly
' AddHandler Changing, profile.Changing
' AddHandler Changed, profile.Changed
* AddHandler Changing, ProfileChanging()
* AddHandler Changed, ProfileChanged()

End Sub

Now the lines with the asterisk (ProfileChanging() and ProfileChanged() ) give the following error message:

"Expression does not produce a value"

I know it doesen't return a value but I have no idea what it expects. I am completely lost using Events.
AmitGajjar 9-Aug-12 23:34pm    
Are you converting c# code to Vb ? if so which converter you are using ?
rctaubert 9-Aug-12 23:51pm    
Yes, I am converting from C# to Vb.net using InstantVB.

Also, I am using VS2010 Professional.

Hi,


Try VB to C# Conversion from Here[^]

Thanks
-Amit Gajjar
 
Share this answer
 
Comments
rctaubert 10-Aug-12 11:47am    
Amit, thank you for replying. I tried the converter you suggested. It came up with the exact same solution so was no help. Thank you for trying.
 
Share this answer
 
Comments
rctaubert 10-Aug-12 11:50am    
Thank you for your reply. I looked at the link you gave. It did not lead to any ideas that worked. Thank you for trying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900