tags:

views:

53

answers:

3

greetings! im stuck at present due to lack of knowledge. i have the following:

    public void ClientEndConnect(IAsyncResult iar)
    {
        try
        {
            CommSocket = (Socket)iar.AsyncState;
            CommSocket.EndConnect(iar);

            OnNetworkEvents eventArgs = new OnNetworkEvents(true, "Connected: " + CommSocket.RemoteEndPoint.ToString(), string.Empty);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (ArgumentNullException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents(false, "Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
    }

this compiles fine but as this is a callback so it should really be: (static)

    public static void ClientEndConnect(IAsyncResult iar)
    {
        try
        {
            CommSocket = (Socket)iar.AsyncState;
            CommSocket.EndConnect(iar);

            OnNetworkEvents eventArgs = new OnNetworkEvents(true, "Connected: " + CommSocket.RemoteEndPoint.ToString(), string.Empty);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (ArgumentNullException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents(false, "Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
    )

but when i do this i get a bunch of errors like:

Error 1 An object reference is required for the non-static field, method, or property 'NietzscheBattleships.NetworkHelper.CommSocket'

due to my lack of knowledge in C# im unable to make sense of these errors. please help me understand.

im reading up on what is static but your comments would help clearing up my confusion also.

A: 

CommSocket must be static as well if you are trying to access/manipulate it from a static method.

CSharpAtl
Having a non-static callback would probably make more sense than having a static CommSocket.
SelflessCoder
I was just answering the reason for the error...not the design decisions. Which I agree with you, would be better changed.
CSharpAtl
+3  A: 

Why should a callback be static?

You get the specified error because you are accessing CommSocket which is a member of the class.

Just leave your callback non-static and everything will be fine.

You can get more information about what is the static keyword here.

SelflessCoder
The callback method itself must have the signature public static void <callbackMethodName>(IAsyncResult).my book on TCPIP Sockets in C# Practical Guide for Programmers.says this. im not sure why.
iEisenhower
This is probably just an example, depending on your design there is no fundemental reason why it has to be static...
Richard Friend
yea without static it works fine. im not sure why the book says so. i thought that unless the callback is static it wont get called and so i am looking for clarity.
iEisenhower
+2  A: 

You are referencing 'this' inside the callback, this is only valid to instance members.

Is there any reason why you think your callback has to be static ?

Richard Friend