0

Say I have a class

class Object
{
    Vector2 positon;
}

This position is editable in the propert grid, how would I be able to set this as not browasable / read only in a class that inherits from object. I know the [Browsable(false)] and [ReadOnly(true)] tags but this will set the it for all Objects, which I do not desire.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Questioning
  • 1,903
  • 1
  • 29
  • 50

2 Answers2

1

Declare position property as virtual and override it on derived types.

public class Class1
{
    public virtual string Lol { get; set; }
}

class Class1Impl1 : Class1
{
    [Browsable(false)]
    [ReadOnly(false)]
    public override string Lol
    {
        get
        {
            return base.Lol;
        }
        set
        {
            base.Lol = value;
        }
    }
}

class Class1Impl2 : Class1
{
    [Browsable(true)]
    [ReadOnly(true)]
    public override string Lol
    {
        get
        {
            return base.Lol;
        }
        set
        {
            base.Lol = value;
        }
    }
}

Doing it at runtime is a different question, IsBrowsable and IsReadOnly are readonly properties. You should google to know if there is posible to change the instances of this attributes at runtime, which I think is not.

Community
  • 1
  • 1
JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
  • I thought it might have to be something like that, I was just wondering if there might be a more elegant way. Is it possible to set this per object? Say if I have two objects and I want to allow one of them to have an editable position and the other to not be editable? – Questioning Jan 22 '13 at 09:55
  • As you can see in my example: Class1Impl1 is not readonly whereas Class1Impl2 is. Both inherit from Class1 so yes, you can. – JoanComasFdz Jan 22 '13 at 10:02
  • Sorry, that's not quite what I meant. What I mean is; is it possible to change the read only / browse value at run time for an object that has a specific value. – Questioning Jan 22 '13 at 10:18
1

I don't think it's possible to change the browseable attribute at runtime (and I don't understand the point of doing that), but you can have a method check your conditions and allow/disallow writing of the property. If that's good enough, I'll be glad to mock something up if you want.

EDIT:

class SomeClass
{
    private Object _foo;
    private Object _bar;

    public Object Foo
    {
        get
        {
            return _foo;
        }
        set
        {
            if (_bar != _foo) // replace with your test
            {
                _foo = value;
            }
        }
    }

}
  • This is definitely something I'd be interested in seeing as it will likely be what I am after - checking the actual attributes is also something that would be good to know. – Questioning Jan 23 '13 at 21:35