开发者

public static void <callbackMethodName>(IAsyncResult)

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(fals开发者_Python百科e, "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.


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.


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 ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜