gcastong
asked on
Creating a class and accessing new objects within the class
I have create a somewhat simple class, in a seperate class file from my main form and program class called InGameObject.
class InGameObject
{
string name;
int xCord, yCord;
public InGameObject(string Name, int XCord, int YCord){
name = Name;
xCord = XCord;
yCord = YCord;
}
public InGameObject() {
InGameObject HomeButton = new InGameObject();
HomeButton.name = "HomeButton";
HomeButton.xCord = 30;
HomeButton.yCord = 200;
}
private void GotoObjectLocation(int x, int y){
}
}
However, I can't seem to access the net new object, HomeButon, outside of the class. i know I am missing something really simple in regards to the constructor.
Can you please assist.
Tks
G.
class InGameObject
{
string name;
int xCord, yCord;
public InGameObject(string Name, int XCord, int YCord){
name = Name;
xCord = XCord;
yCord = YCord;
}
public InGameObject() {
InGameObject HomeButton = new InGameObject();
HomeButton.name = "HomeButton";
HomeButton.xCord = 30;
HomeButton.yCord = 200;
}
private void GotoObjectLocation(int x, int y){
}
}
However, I can't seem to access the net new object, HomeButon, outside of the class. i know I am missing something really simple in regards to the constructor.
Can you please assist.
Tks
G.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should define HomeButton as a public variable on the class level and set it a value in the constructor . You better name this variable buttonHome as capital letters are used for class names and not for methods or variables.
Hello, you meant this?
Then outside your class, example in your form:
class InGameObject
{
string name;
int xCord, yCord;
public InGameObject(string Name, int XCord, int YCord){
name = Name;
xCord = XCord;
yCord = YCord;
}
public InGameObject() {
name = "HomeButton";
xCord = 30;
yCord = 200;
}
private void GotoObjectLocation(int x, int y){
}
}
Then outside your class, example in your form:
public MyForm()
{
InGameObject HomeButton = new InGameObject();
}
You are creating a local copy of the object within the object itself. Create the object externally. Ref: hongjun's post.
ASKER
Will give it a try. Do I create the instance of the new object within the Main Method of the application? I have a lot of new objects to create for that class so looking for the best option.
Thanks
Thanks
My comment on https://www.experts-exchange.com/questions/27398561/Creating-a-class-and-accessing-new-objects-within-the-class.html#36973923 will be good enough.
//Default constructor
InGameObject oDefault = new InGameObject();
Console.WriteLine(oDefault.Name);
//Overloaded constructor
InGameObject oOverloaded1 = new InGameObject("myname1", 1, 1);
Console.WriteLine(oOverloaded1.Name);
//Overloaded constructor
InGameObject oOverloaded2 = new InGameObject("myname2", 1, 1);
Console.WriteLine(oOverloaded2.Name);
ASKER
Thanks, appreciate it. Works great now.