• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

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.
0
gcastong
Asked:
gcastong
1 Solution
 
hongjunCommented:
Try this

class InGameObject
{
    public string Name { get; set; }
    public int XCoord { get; set; }
    public int YCoord { get; set; }

    public InGameObject(string name, int xCoord, int yCoord)
    {
        this.Name = name;
        this.XCoord = xCoord;
        this.YCoord = yCoord;
    }

    public InGameObject()
    {
        this.Name = "HomeButton";
        this.XCoord = 30;
        this.YCoord = 200;
    }

    private void GotoObjectLocation(int x, int y)
    {
    }
}

Open in new window


To access,

 
InGameObject o = new InGameObject();
Console.WriteLine(o.Name);

Open in new window

0
 
regevhaCommented:
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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, you meant this?
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){       
        }
 }

Open in new window


Then outside your class, example in your form:
public MyForm()
{
       InGameObject HomeButton = new InGameObject();
}

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Bill NolanCommented:
You are creating a local copy of the object within the object itself.  Create the object externally.  Ref: hongjun's post.
0
 
gcastongAuthor Commented:
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
0
 
hongjunCommented:
My comment on http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27398561.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);

Open in new window

0
 
gcastongAuthor Commented:
Thanks, appreciate it. Works great now.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now