Imagine Peace Imagine Peace - 4 years ago 108
C# Question

c# button click changing other class string value

i need a bit of help with my code if anyone is willing to help me. my code looks like this.
button code ---

private void button6_Click(object sender, EventArgs e)
{ character tom = new character();
pictureBox1.Image = imageList1.Images[2];
tom.location = "Forest";
label10.Text = tom.location;

im trying to turn tom.locations value into Forest after the button press but after i used the debugger it showed me the tom.location is still the default value that i set. what am i doing wrong?

public int currenthp = 50;
public string name;
public bool whileCombat = true;
public int level = 1;
public int strength = 10;
public int gold = 0;
public int xp;
public int totallevel;
public int damage2;
public int potion = 3;
public int escape2;
public bool alive = true;
public string location = "";

Answer Source

First, You did't get a error when you debugged this code? I am not very sure of that, you can't call to tom.localitation, without a method of the Class Tom.

You can do a method of the class Tom, or declarare a static variable, I recommend you the static variable.

public static string localitation = "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download