user9993
user9993

Reputation: 6170

C# Creating an object within a class

I'm making a map loading system that uses chunks so that the entire map data doesn't have to be loaded at once.

I have a "World" class, and within that class I'm creating an instance of a class called "ChunkManager".

I'm unsure if creating an instance inside another class is a good idea/considered a "normal" thing to do etc. I've not been able to find anything about this while searching the internet.

So my question is: Should I be creating an instance of a class within a class in the way I have, or will there be problems with doing it this way?

Here is my code, if it's relevant:

class World
{

    public string WorldName { get; set; }
    ChunkManager chunkManager = new ChunkManager();


    public World(string worldName)
    {
        WorldName = worldName;

    }

    public void AddChunk(int X, int Y)
    {
        //Plus other validation code here that I didn't paste
        chunkManager.AddChunk(X, Y);
    }
}

And ChunkManager:

class ChunkManager
{
    public int TotalGeneratedChunks { get; private set; }

    private List<Chunk> ChunkList = new List<Chunk>();

    public bool CheckIDExists(int IDToCheck)
    {
        foreach (Chunk i in ChunkList)
        {
            if (i.UniqueID == IDToCheck)
            {
                return true;
            }
        }

        return false;
    }

    public void AddChunk(int X, int Y)
    {
        ChunkList.Add(new Chunk(TotalGeneratedChunks++, X, Y));

    }
}

Upvotes: 0

Views: 8936

Answers (5)

sara Sodagari
sara Sodagari

Reputation: 423

yes you can use one class type in another class its like one of filed on this class like when you use string a=new string() you use an object of class string its normal code

Upvotes: 0

Ian Mercer
Ian Mercer

Reputation: 39277

Your code is fine BUT if either class grows to be more complex and you want to be able to test them independently you should instead define an interface IChunkmanager and inject an instance of ChunkManager into World:

class World
{

    public string WorldName { get; set; }
    private readonly IChunkManager chunkManager;


    public World(string worldName, IChunkManager chunkmanager)
    {
        this.chunkManager = chunkManager;
        ...

With this approach you can use a mocking framework to inject a mock IChunkManager and can test the World class independently.

In general classes should be loosely coupled. As soon as you new-up an instance of another class within a class you have tightly-bound them in a way that makes it hard to test them independently (or to reuse them in different situations).

Upvotes: 4

DevEstacion
DevEstacion

Reputation: 1967

If you are going to create a lot of World, i suggest creating an Abstract base that implements the ChunckManager.

That way you can leverage the use of base class, promote code reuse. You can also make your ChunkManager singleton since it only needs to be used by the base, and then use a method to actually instantiate the ChunkManager if you need specific properties from maps.

Use DI to pass the prop from child to base to instantiation of the ChunkManager

Upvotes: 0

PlausibleSarge
PlausibleSarge

Reputation: 2223

Standard practice is to set values inside the constructor because it allows for dependency injection and makes modifying the constructor to use an argument trivially easy.

Upvotes: 1

Tejas Sharma
Tejas Sharma

Reputation: 3440

It's perfectly fine to create an instance of a class inside another. chunkManager is what is known as a field and the syntax for initializing it inline along with its declaration is called an initializer. You can find more information on initializers and how they are different from initializing via the constructor in this blog series by Eric Lippert

Part 1 Part 2

It might some times be a better idea to initialize fields via the constructor though as this lets you use dependency injection (parameter injection to be precise) which can greatly improve the testability and modularity of your code. If you're interested in learning more about dependency injection I suggest purchasing and reading this book.

Upvotes: 2

Related Questions