Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:17:24+00:00 2026-06-10T02:17:24+00:00

I have a Form (Form1) that has implemented asynchronous TCP/IP sockets. I have an

  • 0

I have a Form (Form1) that has implemented asynchronous TCP/IP sockets. I have an object(public class MyObject) that I created.

From the Form, I create a array of MyObjects. I then call methods on MyObjects for manipulating data. In MyObjects I have a function:

    private void send(string sPacketData)
    {
        if (SocketState.clientSocket != null && SocketState.clientSocket.Connected)
        {
            byte[] byteData = Encoding.ASCII.GetBytes(sPacket);
            SocketState.clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(Form1.SendCallback), this);
        }
    }

The problem here is that I am trying to access Form1’s SendCallback function with no reference to Form1.

Here is what SendCallback looks like:

    public void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            MyObject mo = (MyObject)ar.AsyncState;
            // Complete sending the data to the remote device.
            int bytesSent = mo.SocketState.clientSocket.EndSend(ar);
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Send Callback: " + e.Message + "\r\n");
        }
    }

One solution would be to make SendCallback static, but this leads to exceptions where the is not set to an instance of an object.

Is there a different way to do this? Am I barking up the wrong tree? Should I be returning the data I want to send from MyObjects’s function calls and then sending from the Form?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:17:25+00:00Added an answer on June 10, 2026 at 2:17 am

    Just have send take the callback as one of it’s parameters, and have form1 pass in the callback method to send. Alternatively, you could add a property to MyClass of type AsyncCallback which is set by Form1 and used in the send method.

    When creating the callback in Form1 to pass to MyObject use this.SendCallback rather than Form1.SendCallback. Or, since this is implied, just put in SendCallback.

    A delegate isn’t just a reference to a method, it’s a reference to a method and an object instance that should call it (which is much more powerful). (The object will be null for static methods.) Fortunately, you don’t need to explicitly pass the reference to the delegate constructor. When you enter this.SomeMethod or someObject.MethodOfThatObject the compiler will translate it into a delegate to that method called on that instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form (Form1) that has a NotifyIcon on it. I have another
I have a form that has label values that I would like to pass
I have a form that has a group of 3 text_fields. I need two
I have a form that has many long-methods. My quest is: What is the
I have a form that has 8 columns and a variable number of rows
I have a form that has a series of check boxes and some line
i have a form that has a multiple select drop down. a user can
I have a form that has two buttons on it, one yes, one no,
I have a form that has 90 buttons on it, one button for each
I have a form that has a group of 13 checkboxes that together make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.