0

I have made the following code:

public class AppearanceDefinition<VertexInfo> where VertexInfo : struct
{
    public readonly ShaderProperty<bool> HasBorder; // READONLY VARIABLE
    public readonly ShaderProperty<float> BorderSize;

    internal readonly Shader[] Shaders;

    private readonly string VertexShaderCode;
    private readonly string FragmentShaderCode;

    public AppearanceDefinition(string vertexCode, string fragmentCode)
    {
        this.VertexShaderCode = vertexCode;
        this.FragmentShaderCode = fragmentCode;

        HasBorder = new ShaderProperty<bool>("HasBorder", ResourceKind.UniformBuffer, ShaderStages.Fragment, 0);
        BorderSize = new ShaderProperty<float>("BorderSize", ResourceKind.UniformBuffer, ShaderStages.Fragment, 0);
    }
}

public class ShaderProperty<T> where T : struct
{
    public readonly string Name;
    public readonly ResourceKind InternalDataType;
    public readonly ShaderStages ShaderStage;
    public readonly uint GroupId;

    private T _value;
    public T Value 
    { 
        get 
        {
            return _value;
        }
        set 
        {
            _value = value;
            // requires buffer update. 
        }
    }

    public ShaderProperty(string name, ResourceKind internalDataType, ShaderStages stage, uint group) 
    {
        this.Name = name;
        this.InternalDataType = internalDataType;
        this.ShaderStage = stage;
        this.GroupId = group;
        this._value = default(T);
    }
}

And I use it like so:

class Program
{
    static AppearanceDefinition<bool> test;

    static void Main(string[] args)
    {
        test = new AppearanceDefinition<bool>("", "");
        test.HasBorder.Value = true;
    }
}

This works just fine, the value of HasBorder changes from false to true.
This is great since I don't want people to re-assign the value HasBorder but I do want people to change the value of HasBorder.Value. But it feels weird that I in a way can change a readonly variable.
Could some one explain me why this is possible and if there is maybe a better way of doing this?

FutureCake
  • 2,614
  • 3
  • 27
  • 70
  • 3
    Let's say you have a box. By adding `readonly`, you glue the box to the floor. But by gluing the box to the floor, you don't prevent things from being added to or removed from the box. You can assign a new value to `HasBorder.Value`, but you can't assign anything to `HasBorder` once the constructor is done. – madreflection Apr 14 '20 at 16:48

1 Answers1

2

You can change HasBorder.Value but you can’t change HasBorder because HasBorder is readonly. Try equating HasBorder to something else and it will fail.

It’s important to keep in mind that when a readonly data member is of a reference type (i.e. instance of a class and not either a struct or a base data type) the reference is constant, but the instance it points to is not. So the internal status of a readonly instance can change and can be changed

YankTHEcode
  • 634
  • 4
  • 8