Concerning pure class

  1. Why do we add to parameters of our Node constructor isWalkable, but isExplored, isPath etc… we lieve aside. We use all those variables later on, but how is it possible if they are not parametrized?

  2. Could you please provide more “Easy to understand” information about pure classes and costructors, it is difficult to understand the details of how it works. Thx.

public class Node
{
    public Vector2Int coordinates;
    public bool isWalkable;
    public bool isExplored;
    public bool isPath;
    public Node connectedTo;

    public Node(Vector2Int coordinates, bool isWalkable){
        this.coordinates = coordinates;
        this.isWalkable = isWalkable;
    }
}
1 Like

Hi,

Admittedly, the code is a bit confusing but Vector2Int coordinates and bool isWalkable do not refer to the variables of the same name at the top of your class.

The reason is the so-called scope. The top-level is the instance. And the instance is behind the this keyword. You can regard it as a variable referencing the current instance/object of the class.

The variables in the parentheses of a method are the so-called method parameters. They act as local variables. These variables exist only within the scope of the method code block. Other methods cannot access them.

this.coordinates = coordinates; are two different variables. To be able to distinguish between them, we use the this keyword. this.coordinates refers to the variable at the top of your code while coordinates (in the context of the method code block) refers to the parameter of the same name.

If you used, for example isPath in the method code block, you do not need this. this would be implicite since there is no other variable within the current scope. You may add this, though.


Regarding pure classes and constructors, what exactly would you like to know? The constructor just creates an object of a class. The class could be empty.

public class Test
{
    // constructor
    public Test()
    {
    }
}

// In another method somewhere else
Test test1 = new Test();
Test test2 = new Test();

That’s the simplest pure class. No inheritence (apart from the basic C# stuff like the Object class, which is always available), no dependencies on anything but the basic C# stuff which is always available.

And if you use C# built-in types (also called “primitive data types”) only, the class remains pure.

Is this what you wanted to know?


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms