I have this Checked List Box, listPlayers. I would like to have it add (or remove) names when asked. These names are in string input, naturally.
Here is the code in question:
namespace TakoBot
{
static class Program
{
public static Form1 MainForm { get; private set; }
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
Application.Run(new Form1());
}
public static void OnMessage(object sender, PlayerIOClient.Message m)
{
if (m.Type == "add")
{
NamesInt[m.GetString(1)] = m.GetInt(0);
NamesString[m.GetInt(0)] = m.GetString(1);
Program.MainForm.listPlayers.Add("PlayersName");
}
}
}
}
Upon calling the action Form1.listPlayers.Add("PlayersName");, we get the error:
"'MyProgram.Form1.listPlayers' is inaccessible due to its protection level"
..Okay, my error-handling skills aren’t the best. Like I said, everything is public.
If I’m using entirely wrong actions, don’t hesitate to show me correct ones.
Form1is a type, not an instance.In your
Programdo something like thisNow you can reference the form like this (
listPlayersmust be public)As an alternative, you could expose the player list as a static property in
Form1Now you can access it like
because it is static, i.e.
PlayerListbelongs to the type (class)Form1, not to an instance (object) ofForm1. This works only if you have only one instance ofForm1open at any time.Given
You can do this
The static variable
MyClass.Scan have only one value at a given time. It will be"Two"at the end of this code.The instance variable
Ican have a different value in each instance (a,b). At the end of this codea.Iwill be"Hello"andb.Iwill be"World".