Reputation: 111
In C# Windows Form Application,
.
Race class:
class Race
{
int player;
int position;
}
Object creation:
Race Obj = new Race();
Accessing Object:
private void button1_Click(object sender, EventArgs e)
{
Obj.position++;
}
The question is, where to create Race class Object, so i can access it when button is clicked?
Upvotes: 1
Views: 253
Reputation: 166476
You can add it as a private member in the form class and instanciate it there.
So something like
public class Form1
{
private Race Obj = new Rate();
private void button1_Click(object sender, EventArgs e)
{
Obj.position++;
}
}
Also make sure that the class is accessible.
Upvotes: 1
Reputation: 149558
You'll need to declare it at the class level. In your case, that means it needs to be a field in your form:
public class Form1 : Form
{
private Race race;
public Form1()
{
race = new Race();
}
public void button1_Click(object sender, EventArgs e)
{
race.position++;
}
}
Upvotes: 1