none
Error when converting code to vb RRS feed

  • Question


  • hi

    I got the C Sharp code

    I want To convert to VB.NET

    get 2 error

    1- ReceiveMsg' is not an event of 'eserver1.Form1'.

    2-Function '<anonymous method>' doesn't return a value on all code paths

    use this web to convert

    http://converter.telerik.com/
    code c#
    
     
    public EventHandler ReceiveMsg ;
         DateTime LastReceive;
     
    InitializeComponent();
             ReceiveMsg += ReceiveMsg_Handler;
             LastReceive = DateTime.Now;
             IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
     
             IPAddress ipAddress = ipHostInfo.AddressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
               .FirstOrDefault();
             lblip.Text = ipAddress.ToString();
              
              private void ReceiveMsg_Handler(object sender, EventArgs e)
         {
            
             LastReceive = DateTime.Now;
             Timer1_Tick(null, null);
             if (sender.ToString().Contains("ping"))
                 return;
             var Barcode = sender.ToString().Split('-');
             txtbarcode.Invoke((MethodInvoker)delegate {
                 // Running on the UI thread
                 txtbarcode.Text = Barcode[0];
                 txtname.Text = Barcode[1].Substring(0, Barcode[1].IndexOf("<EOF>"));
             });
             SendKeys.SendWait(Barcode[0]);
         }
         Thread Serverthread;
          
          private void Timer1_Tick(object sender, EventArgs e)
         {
             bool res = DateTime.Now - LastReceive >= TimeSpan.FromSeconds(2);
             lblStatus.Invoke((MethodInvoker)delegate
             {
                 lblStatus.Text = res ? "Disconnected": "Connected";
                 lblStatus.ForeColor = res ? Color.Red : Color.Green;
             });
                 return;
     
         }
    '===================================
    vb.net convert
    Public ReceiveMsg As EventHandler
      Private LastReceive As DateTime
     
      Public Sub New()
          InitializeComponent()
          AddHandler ReceiveMsg, AddressOf ReceiveMsg_Handler  ' error1
          LastReceive = DateTime.Now
          Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
          Dim ipAddress As IPAddress = ipHostInfo.AddressList.Where(Function(x) x.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault()
          lblip.Text = ipAddress.ToString()
      End Sub
      Public Sub ReceiveMsg_Handler(ByVal sender As Object, ByVal e As EventArgs)
          LastReceive = DateTime.Now
          Timer1_Tick(Nothing, Nothing)
          If sender.ToString().Contains("ping") Then Return
          Dim Barcode = sender.ToString().Split("-"c)
          txtbarcode.Invoke(CType(Function()
                                      txtbarcode.Text = Barcode(0)
                                      txtname.Text = Barcode(1).Substring(0, Barcode(1).IndexOf("<EOF>"))
                                  End Function, MethodInvoker))
          SendKeys.SendWait(Barcode(0))
      End Sub
     
     
      Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
          Dim res As Boolean = DateTime.Now - LastReceive >= TimeSpan.FromSeconds(2)
          lblStatus.Invoke(CType(Function()
                                     lblStatus.Text = If(res, "Disconnected", "Connected")
                                     lblStatus.ForeColor = If(res, Color.Red, Color.Green)
                                 End Function, MethodInvoker))
          Return
      End Sub




    • Edited by monemas Monday, December 2, 2019 8:12 PM
    Monday, December 2, 2019 8:10 PM

Answers

  • Hi,

    I didn't delve into what kind of results your program wants to accomplish, but for the two questions you asked, the answers I gave were like this:

    error 1: The event specified is not declared for the object.

    change

    Public ReceiveMsg As EventHandler

    to

    Event ReceiveMsg(ByVal sender As Object, ByVal e As EventArgs)

    2. You do not have any return value or keyword Return.So it is recommended that you modify the keyword Function to Sub.

    PS:Use a transcoder to some extent and to reduce manual operations, but it does not guarantee that the completed code can be used directly, and often it needs to be modified manually.

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 3, 2019 3:16 AM
    Moderator

All replies

  • Hi,

    I didn't delve into what kind of results your program wants to accomplish, but for the two questions you asked, the answers I gave were like this:

    error 1: The event specified is not declared for the object.

    change

    Public ReceiveMsg As EventHandler

    to

    Event ReceiveMsg(ByVal sender As Object, ByVal e As EventArgs)

    2. You do not have any return value or keyword Return.So it is recommended that you modify the keyword Function to Sub.

    PS:Use a transcoder to some extent and to reduce manual operations, but it does not guarantee that the completed code can be used directly, and often it needs to be modified manually.

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 3, 2019 3:16 AM
    Moderator
  •  i reuse receivemsg in load form

    Public ReceiveMsg As EventHandler

       Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            AsynchronousSocketListener.ReceiveMsg = ReceiveMsg
            Serverthread = New Thread(New ThreadStart(Sub()
                                                          AsynchronousSocketListener.StartListening()
                                                      End Sub))
            Serverthread.Start()
        End Sub

    Tuesday, December 3, 2019 2:43 PM
  • Hi,

    May I ask if your previous question was resolved? If the solution I have proposed can solve your previous problem, please mark the reply as answered as this will help others looking for the same or similar issues down the road.

    So what's wrong with your current code? If you have anything else about this issue, please feel free to contact us.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, December 4, 2019 3:20 AM
    Moderator