0

I have an listbox item in my form that is changed from an class that has to be static. Due to this, I tried putting the script into an different non-static class like this:

  private void addChat(string talk, string user)
   {
           console.Items.Add(user + ": " + talk);
   }

And is ran from here:

   static void OnMessage(object sender, PlayerIOClient.Message m)
    {
     //Code...
     string username = users[m.GetInt(0)]; //public static Dictionary<int, string> users = new Dictionary<int, string>();
     addChat(m.GetString(1), username);
     //More code...
    }

However I then get this error:

An object reference is required for the non-static field, method, or property 'NAME.Form1.addChat(string, string)'

Making this class static gives this error:

An object reference is required for the non-static field, method, or property 'NAME.Form1.console'

How do I make the console (Which is a listbox) static?

  • [see this](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-on). the link is probably your situation. – Old Fox Jul 20 '15 at 04:00

1 Answers1

0

Not quite sure what you are trying to achieve. However, in general you could move your static logic to your static class and use it within your UI class.

In your static class :

private static string formatChat(string talk, string user)
{
   string.Format("{0} : {1}", user, talk);
}

In your UI class:

private void addChat(string talk, string use)
{
    console.Items.Add(formatChat(string talk, string use));
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131