views:

69

answers:

1

I have carried the method here on almost all of the areas where I have had overridable methods and managed to fix them but there is one part where the method doesnt work in the same way on a different contexted piece of code:

    public Employee()
    {
        this.InitMembers();
    }

    private void InitMembers()
    {
        // Init the collection so it's never null
        this.Territories = new List<Territory>();
    }
    public Employee(string firstName, string lastName): this()
    {
        this.reffirstName = firstName;
        this.reflastName = lastName;
    }
>   public virtual IList<Territory> Territories { get; protected set; }

Where again the > is the code causing the error, I do however get an intellisense option to "Convert to auto property", which simply reverts the code to when it was started and not fixing the problem. Anyone know what modifications need to be made to this part to elimiate the fxcop violation?

A: 

The error appears because your private constructor is calling a method that can be overridden from a derived class. To fix the warning, you need to remove any calls to virtual methods from within the constructor.

In the example you list, InitMembers uses 'this.Territories', which is causing the violation. According to your later comment you have added a private member - use that instead.

Pedro