user517406
user517406

Reputation: 13773

overriding properties

I am inheriting from a class that has this property :

public bool isAuthorized
{
    get { return _Authorized; }
    set { _Authorized = value; }
}

I am trying to override this in the derived class, but it doesn't recognise isAuthorized. Can someone help me with my syntax?

public override bool isAuthorized()
{

}

This is the error I get :

cannot override because 'CDBase.isAuthorized' is not a function

EDIT : So if I want the override to always set isAuthorized to true, I would use this syntax?

private bool _Authorized = false;

    public override bool isAuthorized 
    {
        get { return _Authorized; }
        set { _Authorized = true; }
    }

Upvotes: 0

Views: 2176

Answers (10)

Mutation Person
Mutation Person

Reputation: 30498

The original class should have a virtual, but I suspect you might also have an issue with the fact that you are trying to override a base class property with a derived class method.

If your derived class method looked like this:

    public override bool isAuthorized
    {
        get { return _Authorized; }
        set { _Authorized = value; }
    }

Then you would need this in your base class:

    public virtual bool isAuthorized
    {
        get { return _Authorized; }
        set { _Authorized = value; }
    }

Can you maybe desrive more what you are trying to achieve?

Upvotes: 2

Kang
Kang

Reputation: 545

About your error and syntax : Simply dont use the '()' . You are using property as method.

About how to do it :

There are two ways to implement it in base class. 'new' and 'override'. But their implementations varies regarding to base class.

http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

The new modifier instructs the compiler to use your implementation instead of the base class implementation. Any code that is not referencing your class but the base class will use the base class implementation.

public bool isAuthorized
 {
     get { return _Authorized; }
     set { _Authorized = value; }
 }


public new bool isAuthorized
 {  //someth    }

http://msdn2.microsoft.com/en-us/library/ebca9ah3.aspx

The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.

public virtual bool isAuthorized
 {
     get { return _Authorized; }
     set { _Authorized = value; }
 }


public override bool isAuthorized
 {  //someth    } 

Upvotes: 0

Justin
Justin

Reputation: 686

In base class you can mark it as virtual, and you can override. Actually, use reflector we can see the properties are methods, too!

Upvotes: 0

VMAtm
VMAtm

Reputation: 28355

To set your value always to true you can simply write this code:

  public override bool isAuthorized
  {
      get { return true; }
  }

Upvotes: 2

Algem Mojedo-MCPD-EA
Algem Mojedo-MCPD-EA

Reputation: 61

Hi I think you must declare this as Virtual in able to override

Like:

public virtual bool isAuthorized
{
get { return _Authorized; }
set { _Authorized = value; }
}

Regars,

Upvotes: 0

John Nicholas
John Nicholas

Reputation: 4836

this

   class A
    {
        public virtual string prop { get; set; }
    }

    class B : A
    {
        public override string prop
        {
            get
            {
                return "overridden";
            }
            set
            {
                base.prop = value;
            }
        }
    }

Upvotes: 0

FIre Panda
FIre Panda

Reputation: 6637

In base class mark the property as virtual which you want to be overridden and in derive class use override keyword for the property that needs to be overriden.

Upvotes: 0

danyolgiax
danyolgiax

Reputation: 13086

public virtual bool isAuthorized
{
    get { return _Authorized; }
    set { _Authorized = value; }
}



public override bool isAuthorized
{
 ...
}

Upvotes: 0

anishMarokey
anishMarokey

Reputation: 11397

its a property, you used as method isAuthorized()

() - used for methods

you have to do in your derived class

public override bool isAuthorized
    {

    }

Upvotes: 4

m0sa
m0sa

Reputation: 10940

Same as when you want your method to be overridable, the property also has to be declared virtual in the base class:

public virtual bool isAuthorized ...

Also you cannot override a propeprty with a method. You can only override the getters and setters in the derived class:

public override bool isAuthorized
{
   get { return base.isAuthorized; }
   set { base.isAuthorized = value; } 
}

Upvotes: 2

Related Questions