I’ve got two forms. In the first, I have a textbox and a button, and in the other one I have a label. When you enter text in the textbox, and press button, a new form is opened, and the label has the same text as the textbox in the previous form. How do I do that with get/set?
I made a class “Globals”, and in it get/set:
class Globals
{
public string imena = "";
public string ime
{
get
{
return imena;
}
set
{
imena = value;
}
}
}
and in the first form
private void btnplay_Click(object sender, EventArgs e)
{
//this.Hide();
Game igra = new Game();
igra.Show();
Globals promenljive = new Globals();
promenljive.ime = tbpl1.Text;
}
and in the second one
private void Game_Load(object sender, EventArgs e)
{
Globals promenljive = new Globals();
lblime1.Text = promenljive.ime;
}
But it doesn’t work? What did I do wrong?
Well you’re creating two separate instances of
Globals, to start with… those will have independent variables, which is why you’re not seeing the value you’ve just set. It’s like painting one house red, then looking at the colour of a completely different house.However, using a “globals” class like this is a bad idea. Why not just add a parameter to the
Gameconstructor, and pass in the data that way?It sounds like you may be new to OOP given your initial approach. If this is the case, I would strongly advise you to learn about the basics of C#, .NET and OO in general before starting to write GUI applications. GUIs have their own difficulties (such as threading rules) and are hard enough to develop even when you’re confident of the basics. At the moment, you’ll find it hard to tell the difference between a genuinely GUI-specific problem and simply one of not understanding how C# and .NET work in general.