Converting c# to VB.net error

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 10681 Views - Last Post: 26 May 2011 - 01:02 AM Rate Topic: -----

#1 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Converting c# to VB.net error

Posted 23 May 2011 - 01:59 PM

Hi.

when i'm converting some code from c# to vb.net i get error.

Here is the c# code
 public event CommandReceivedEventHandler CommandReceived;

        protected virtual void OnCommandReceived(CommandEventArgs e)
        {
            if ( CommandReceived != null )
            {
                Control target = CommandReceived.Target as Control;
                if ( target != null && target.InvokeRequired )
                    target.Invoke(CommandReceived , new object [] { this , e });
                else
                    CommandReceived(this , e);
            }
        }


And here is the convert-result:
Public Event CommandReceived As CommandReceivedEventHandler
        Protected Overridable Sub OnCommandReceived(ByVal e As CommandEventArgs)
            If Not CommandReceived = Nothing Then

                Dim target As Control = TryCast(CommandReceived.Target, Control)
                If target IsNot Nothing AndAlso target.InvokeRequired Then
                    target.Invoke(CommandReceived, New Object() {Me, e})
                Else
                    RaiseEvent CommandReceived(Me, e)
                End If
            End If
        End Sub


This is the error i'm getting
'Public Event CommandReceived(sender As Object, e As CommandEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.


I didn't get the error in c#
Hope someone can help me :)


Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Converting c# to VB.net error

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Converting c# to VB.net error

Posted 23 May 2011 - 06:23 PM

Which line is it on? Its kind of hard to tell which is the error.

EDIT:
Then again, you could change this line:
target.Invoke(CommandReceived, New Object() {Me, e})



to the following:

target.Invoke(Sub()
        RaiseEvent CommandReceived(New Object() {Me, e})
    End Sub)



I cannot remember if that is the correct syntax but it SHOULD work.

This post has been edited by Amrykid: 23 May 2011 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Converting c# to VB.net error

Posted 23 May 2011 - 07:23 PM

If Not CommandReceived = Nothing Then

Should be:
If Not CommandReceived Is Nothing Then

Or:

If CommandReceived IsNot Nothing Then
Was This Post Helpful? 1
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 23 May 2011 - 10:22 PM

hm. no, didn't help. Nether of your suggestions.

If we go back to the code:
Protected Overridable Sub OnCommandReceived(ByVal e As CommandEventArgs)
            If Not CommandReceived = Nothing Then
                Dim target As Control = TryCast(CommandReceived.Target, Control)
                If target IsNot Nothing AndAlso target.InvokeRequired Then
                    target.Invoke(CommandReceived, New Object() {Me, e})
                Else
                    RaiseEvent CommandReceived(Me, e)
                End If
            End If
        End Sub

These are the lines i get error on:
Error 1: If Not CommandReceived = Nothing Then  @ "CommandReceived"
Error 2: Dim target As Control = TryCast(CommandReceived.Target, Control)  @ "CommandReceived.Target"
Error 3: target.Invoke(CommandReceived, New Object() {Me, e})  @ "CommandReceived"


And they are all saying the same thing:
'Public Event CommandReceived(sender As Object, e As CommandEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Was This Post Helpful? 0
  • +
  • -

#5 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 06:00 AM

Hi,

You could try this:

Public Event CommandReceived As CommandReceivedEventHandler
        Protected Overridable Sub OnCommandReceived(ByVal e As CommandEventArgs)
            If Not CommandReceived = Nothing Then
                RaiseEvent CommandReceived(Me, e)

                Dim target As Control = TryCast(CommandReceived.Target, Control)
                If target IsNot Nothing AndAlso target.InvokeRequired Then
                    target.Invoke(CommandReceived, New Object() {Me, e})
                Else
                                        RaiseEvent Finished()
                End If
            End If
        End Sub


This post has been edited by Luc001: 24 May 2011 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Converting c# to VB.net error

Posted 24 May 2011 - 06:40 AM

Try changing
If Not CommandReceived = Nothing Then
to
If CommandReceived IsNot Nothing Then

Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 07:29 AM

Sorry, non of it works =/
Thanks anyway =/

If you need more code, just tell me.
But there is not much more that's relevant to this =/
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Converting c# to VB.net error

Posted 24 May 2011 - 08:10 AM

Public Event CommandReceived As CommandReceivedEventHandler is not valid.

Public Event CommandReceived(sender As Object, e As EventArgs is.
Was This Post Helpful? 0
  • +
  • -

#9 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 08:15 AM

No, sorry. Not working =/
Get same error.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Converting c# to VB.net error

Posted 24 May 2011 - 08:34 AM

EndlessMind,

Quote

No, sorry. Not working =/
is not a fair statement without futher understanding your complete application. The simple code i supplied is exactly how you make an event with the typical event signature(sender,e).
Was This Post Helpful? 0
  • +
  • -

#11 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 10:08 AM

Okej. It's a TCP IP text-chat application.
Me and my friend did it in C# because that where we got the most knowledge and it worked good.
But in order to learn more about Vb.net and TCP and UDP via socket's, i started to convert it to vb.net

So. Here is all the code that's related to this issue first in C# and then in vb.net.
as posted before, this is in a class called CommandClient:
 /// <summary>
        /// Occurs when a command received from a remote client.
        /// </summary>
        public event CommandReceivedEventHandler CommandReceived;
        /// <summary>
        /// Occurs when a command received from a remote client.
        /// </summary>
        /// <param name="e">The received command.</param>
        protected virtual void OnCommandReceived(CommandEventArgs e)
        {
            if ( CommandReceived != null )
            {
                Control target = CommandReceived.Target as Control;
                if ( target != null && target.InvokeRequired )
                    target.Invoke(CommandReceived , new object [] { this , e });
                else
                    CommandReceived(this , e);
            }
        }



This is in a class called EventArgs:
 /// <summary>
    /// Occurs when a command received from the server.
    /// </summary>
    /// <param name="sender">Sender.</param>
    /// <param name="e">The information about the received command.</param>
    public delegate void CommandReceivedEventHandler(object sender , CommandEventArgs e);




And what i got in VB.net:

As posted before, again in class called CommandClient:
 ''' <summary>
        ''' Occurs when a command received from a remote client.
        ''' </summary>
        Public Event CommandReceived(ByVal e As CommandReceivedEventHandler)
        '  Public Event CommandReceived As CommandReceivedEventHandler

        ''' <summary>
        ''' Occurs when a command received from a remote client.
        ''' </summary>
        ''' <param name="e">The received command.</param>
        Protected Overridable Sub OnCommandReceived(ByVal e As CommandEventArgs)
            If Not CommandReceived = Nothing Then
                Dim target As Control = TryCast(CommandReceived.Target, Control)
                If target IsNot Nothing AndAlso target.InvokeRequired Then
                    target.Invoke(CommandReceived, New Object() {Me, e})
                Else
                    RaiseEvent CommandReceived(Me, e)
                End If
            End If
        End Sub


And the EventArgs class in vb.net:

''' <summary>
    ''' Occurs when a command received from the server.
    ''' </summary>
    ''' <param name="sender">Sender.</param>
    ''' <param name="e">The information about the received command.</param>
    Public Delegate Sub CommandReceivedEventHandler(ByVal sender As Object, ByVal e As CommandEventArgs)


That's about it.
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Converting c# to VB.net error

Posted 24 May 2011 - 10:35 AM

That's great, but i get "target is not a member of public event..." so how does that convert an Event to a control?
Was This Post Helpful? 0
  • +
  • -

#13 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 10:51 AM

Shall i sent you the project instead? quite alot of code, and i don't really want to publish it until it's complete.
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Converting c# to VB.net error

Posted 24 May 2011 - 10:56 AM

The concepts seems a little awkward. The base class CommandClient receives the data. The CommandReceivedEventArgs class inherits from EventArgs and defines more properties specific to your program. Then when the base class parses the data it raises the event with the New CommandReceivedEventArgs object with the properties set for that object. This way when they check the <someValue> = e.<someProperty> it will have said value as specified by the base class. Your EventArgs does not need a delegate it just carries values with the raised event.

This post has been edited by hawkvalley1: 24 May 2011 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#15 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Converting c# to VB.net error

Posted 24 May 2011 - 10:59 AM

no, the CommandReceivedEventHandler byval e as CommandEventArgs
and CommandEventArgs is:
public class CommandEventArgs : EventArgs
    {
        private Command command;
        /// <summary>
        /// The received command.
        /// </summary>
        public Command Command
        {
            get { return command; }
        }
        /// <summary>
        /// Creates an instance of CommandEventArgs class.
        /// </summary>
        /// <param name="cmd">The received command.</param>
        public CommandEventArgs(Command cmd)
        {
            this.command = cmd;
        }
    }


And that, goes to my:
    public class Command
    {
        private IPAddress senderIP;
        /// <summary>
        /// [Gets/Sets] The IP address of command sender.
        /// </summary>
        public IPAddress SenderIP
        {
            get { return senderIP; }
            set { senderIP = value; }
        }

        private string senderName;
        /// <summary>
        /// [Gets/Sets] The name of command sender.
        /// </summary>
        public string SenderName
        {
            get { return senderName; }
            set { senderName = value; }
        }
        
        private CommandType cmdType;
        /// <summary>
        /// [Gets/Sets]  The type of command to send.If you wanna use the Message command type,create a Message class instead of command.
        /// </summary>
        public CommandType CommandType
        {
            get { return cmdType; }
            set { cmdType = value; }
        }

        private IPAddress target;
        /// <summary>
        /// [Gets/Sets] The targer machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.
        /// </summary>
        public IPAddress Target
        {
            get { return target; }
            set { target = value; }
        }
        private string commandBody;
        /// <summary>
        /// [Gets/Sets] The body of the command.This string is different in various commands.
        /// <para>Message : The text of the message.</para>
        /// <para>ClientLoginInform,ClientLogOffInform : The IP of loged in/out machine.</para>
        /// <para>***WithTimer : The interval of timer in miliseconds.The default value is 60000 equal to 1 min.</para>
        /// <para>IsNameExists : The name of client you want to check it's existance.</para>
        /// <para>Otherwise pass the "" or null.</para>
        /// </summary>
        public string MetaData
        {
            get { return commandBody; }
            set { commandBody = value; }
        }
        /// <summary>
        /// Creates an instance of command object to send over the network.
        /// </summary>
        /// <param name="type">The type of command.If you wanna use the Message command type,create a Message class instead of command.</param>
        /// <param name="targetMachine">The targer machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.</param>
        /// <param name="metaData">
        /// The body of the command.This string is different in various commands.
        /// <para>Message : The text of the message.</para>
        /// <para>ClientLoginInform : "RemoteClientIP:RemoteClientName".</para>
        /// <para>***WithTimer : The interval of timer in miliseconds..The default value is 60000 equal to 1 min.</para>
        /// <para>IsNameExists : The name of client you want to check it's existance.</para>
        /// <para>Otherwise pass the "" or null or use the next overriden constructor.</para>
        /// </param>
        public Command(CommandType type , IPAddress targetMachine , string metaData)
        {
            this.cmdType = type;
            this.target = targetMachine;
            this.commandBody = metaData;
        }

        /// <summary>
        /// Creates an instance of command object to send over the network.
        /// </summary>
        /// <param name="type">The type of command.If you wanna use the Message command type,create a Message class instead of command.</param>
        /// <param name="targetMachine">The targer machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.</param>
        public Command(CommandType type , IPAddress targetMachine)
        {
            this.cmdType = type;
            this.target = targetMachine;
            this.commandBody = "";
        }
    }

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2