Home > DeveloperSection > Forums > How to subclass member initialization?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    January-27-2015 10:16 PM

 ASP.Net C# 
Ratings:


 1 Reply(s)
 592  View(s)
Rate this:

I've created two classes which replace the function of a Point (both variants) just to test them out, this is how I've created them:

Entity.cs:

namespace Xnagame

{

 

    class Player

    {

 

        public class float2

        {

            public float X { get; set; }

            public float Y { get; set; }

        }

        public class int2

        {

            public int X { get; set; }

            public int Y { get; set; }

        }

        public int2 Position { get; set; }

        public int angle { get; set; }

        public float speed { get; set; }

        public float2 velocity { get; set; }

 

        public Player CreatePlayer()

        {

 

            Position = new Player.int2();

            Player player = new Player();

            return player;

        }

    }

}

As you can see, both Point classes have a X and Y variable (I'm not sure if this is the way to set them up, if it isn't please tell me) and these classes are used in the position and velocity instances which are instantiated in the CreatePlayer() method.

Game1.cs:

namespace Xnagame

{

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        private Texture2D playerimage;

        private Texture2D background;

        Player player = Player.CreatePlayer();

Now the problem is that when CreatePlayer() method tries to return it's "player" to the local player it gives the:

An object reference is required for the non-static field, method, or property Xnagame.Player.CreatePlayer()

error.

I also tried it with the new keyword and it gave me:

Xnagame.Player.CreatePlayer() is a method but is used like a type.



Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    January-27-2015 10:40 PM

The problem is that you're using an instance method to create an instance.

You have two options:

1) Use a default constructor

class Player

{

    // Other subclasses, and properties.

 

    public Player()

    {

        // Note: I added the other instance value just for fun.

        Position = new Player.int2();

        velocity = new Player.float2();

    }

}

2) Use a static constructor method. (These are typically only used in a singleton pattern.)

class Player

{

    // Other subclasses, and properties.

 

    public static Player CreatePlayer()

    {

        // Note how I created the instance variables for the player class,

        // and I used a notation called "object initalizer" to set those properties

        // when I create the Player instance.

        var p = new Player.int2();

        var v = new Player.float2();

        Player player = new Player()

        {

          velocity = v,

          Position = p

        };

        return player;

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick