---
title: "How to subclass member initialization?"  
description: "How to subclass member initialization?"  
author: "Jayden Bell"  
published: 2015-01-27  
updated: 2015-01-27  
canonical: https://www.mindstick.com/forum/12908/how-to-subclass-member-initialization  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to subclass member initialization?

I've created two classes which replace the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) of a Point (both [variants](https://www.mindstick.com/news/2657/innova-hycross-suv-prices-variants-colour-options-revealed)) 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](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) (I'm not sure if this is the way to set them up, if it isn't [please tell me](https://www.mindstick.com/forum/33900/please-tell-me-what-are-the-technique-to-content-optimization)) and these classes are used in the position and velocity [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-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](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object) is [required](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) for the non-static field, method, or [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) Xnagame.Player.CreatePlayer()

error.

I also tried it with the new [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) and it gave me:

Xnagame.Player.CreatePlayer() is a method but is used like a type.

## Replies

### Reply by Anonymous User

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](http://en.wikipedia.org/wiki/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;    }}
```


---

Original Source: https://www.mindstick.com/forum/12908/how-to-subclass-member-initialization

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
