Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've been at this all afternoon and can't figure a way around it. The method has to be static so that is not an option of not being static. Here's the code:

 

private static void ConnectionFilter(String sRemoteAddress, int nRemotePort, Socket sock)
       {
           lstMsg.Items.Add("Connection request from " + sRemoteAddress + ":" + nRemotePort.ToString());
       }

 

lstMsg is not available but I can't figure out why. I would greatly appreciate any assistance with this.

Posted

Static methods

 

Static methods can only access static class variables. If lstMsg is an instance (non-static) variable (as it would be by default) then it cannot be accessed from a static method. Static methods can be called even when no class instances exist, so cannot access class members without an explicit reference.

 

I assume this is part of a form's code, and that you expect only one instance of the form to be used. In that case you would need to store a reference to the form in a static variable, which static methods can access. For instance:

 

public partial class MyForm : Form
{
   private static MyForm m_theform;

   //Constructor
   public MyForm()
   {
       //Set m_theform to point to this
       MyForm.m_theform = this;
   }

   //Static method
   private static void ConnectionFilter(String sRemoteAddress, int nRemotePort, Socket sock)
   {
       MyForm.m_theform.lstMsg.Items.Add("Connection request from " + sRemoteAddress + ":" + nRemotePort.ToString());
   }
}

 

I notice that the method is private, so I assume you're calling it from another static method, otherwise it might as well be non-static.

 

Good luck :cool:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...