0

What I've noticed, is using

Program.cs has

public static List<Client> clients = new List<Client>();

the Button

 private void button1_Click(object sender, EventArgs e)
         {
                         Client client = new Client(combobox1.selecteditem);
                         Program.clients.Add(client);
         }

Client.cs All variables are non-static public. But there is an eventhandler where on packetrecv, a class is called, this class is then filtered and processes

Where it is called is

public void recieved(short op, string str, Client c)
        {
switch (op)
                {
case (short)OpCodes.matches:
                        {
                            c.something(c, str);
                            break;
                        }
}
}

Handler.cs

 public void something(Client c, string movement)
           {
                 if (movement == null)
                 c.coords = movement;
                 c.freeSpot = true;
             }

And in the above ^ the variables would overlap and freespotwould be made true throughout all the instances

will work find with one instance. But I'm trying to compile with multiple instances. So creating a button_onclick would create a new instance using the above.

As the program is running, it runs flawlessly on one instance, but with 2+ instances, the variables in MyClass start to overlap. Is there a way to prevent this?

  • Do you mean that you only created one object and two variables are sharing the same instance of the object? If you don't then I am not certain what you mean. – Patashu Apr 29 '13 at 04:12
  • Your `MyClass` variables are `static` when they should not be. – Blorgbeard Apr 29 '13 at 04:13
  • 2
    You will have to post more code (at least button_onclick) for us to understand exactly what you are trying to do. – Eric J. Apr 29 '13 at 04:13
  • no, none of my variables are static – user2309449 Apr 29 '13 at 04:14
  • Well, please post the minimum amount of code that demonstrates your problem then, otherwise we are guessing. – Blorgbeard Apr 29 '13 at 04:15
  • button_Click(object sender, EventArgs e) { MyClass name = new MyClass(); -defines variables here- name.LoginID = loginID.TextBox; ...... } Public partial class MyClass { public string LoginID; } – user2309449 Apr 29 '13 at 04:17
  • If you need to create a new instance on every button click, you should set up a `List` and collect the instances there – Ayush Apr 29 '13 at 04:17
  • I already have that: public static List clients = new List(); Program.clients.Add(client); Could eventhandlers be jumping classes? o_O – user2309449 Apr 29 '13 at 04:20
  • Edit your question and put the code there please, it's hard to read code in comments – Blorgbeard Apr 29 '13 at 04:21
  • Try -> http://stackoverflow.com/questions/3992921/how-to-share-an-instance-of-a-class-with-many-classes – Elyor Apr 29 '13 at 04:21
  • If `freeSpot` is not static, `something` must be being called for all client instances.. can you post the code where `something` is called? – Blorgbeard Apr 29 '13 at 04:39
  • It is called at the same time for all client instances. Could that be the problem? Is there a way to like, prevent it from colliding and keep the instances separate? – user2309449 Apr 29 '13 at 04:48
  • Please don't create duplicate questions http://stackoverflow.com/q/16271531/81053 – Chris Moutray Apr 29 '13 at 05:32
  • 1
    Can you post the code that calls recieved? That's the place where the Client object is determined, and hence seems to be the likely culprit. – Jonathan Rupp Apr 29 '13 at 06:08
  • i need to have clarification, do mean the problem is occur when multiple client instances is added in (List)clients field or when multiple instance of "program.cs" e.g open same exe file twice or more? – Bharath Apr 29 '13 at 06:28

0 Answers0