What does this warning mean when compiling?

What does this warning mean when compiling?

Old forum URL: forums.lhotka.net/forums/t/1102.aspx


RangerGuy posted on Friday, September 01, 2006

My StatusCategory object is a editable child list contained in a root object. When I compile I get the following warning.

Warning 1 'MyCompany.Library.StatusCategoryList.this[int]' hides inherited member 'System.Collections.ObjectModel.Collection<MyCompany.Library.StatusCategory>.this[int]'. Use the new keyword if hiding was intended. 

 

ramesh replied on Friday, September 01, 2006

This is because the property "this[int index]" is already implemneted in the base class of your class. Since in base class its not marked a svirtula therefore you must use new i.e. "new this[int index]" before the property if you are intentionally overriding this property. Usually you need to override this but I dont know your requirements so you need to decide on this..

- Ramesh

RangerGuy replied on Friday, September 01, 2006

Thanks for the quick reply :) Just so I understand correctly.

The issue is BusinessListBase doesn't declare a virtual (this)  property and the generic list has a default this property which expects an indexer of type int. So in order for me to use this[int index] I need to add a virtual (this) property to business list base?

Is that why in project tracker. The class ResourceAssignments can use this[guid projectid] with out a warning because it expects a guid and ProjectResources has GetItem method because it needed an integer for an index?

ajj3085 replied on Friday, September 01, 2006

Sounds like you have it correct; although you can use the new keywoard on the this[ int index ] on the subclass.  You should avoid it if at all possible though because you're breaking OO principals.


The easiest way to think of it is that this[ int index ] is actually just a method like MyMethod( int index ) behind the scenes.

RangerGuy replied on Friday, September 01, 2006

Ok so I just found a link that explains the "new" keyword it basically just tells the compiler that you intentially want to call this method instead of the one implemented in the base class.

What I don't understand is why using the "new" keyword on a method is breaking OOP principles.

ajj3085 replied on Friday, September 01, 2006

It breaks OO principals because it allows you to change the return type or method parameters.

For example:

    public class Animal {
        public int Walk( int howFar ) {
            return 0;
        }
    }

    public class Cat : Animal {
        public new void Walk( int howFar ) {
          
        }
    }

Obviously that can be a very bad thing because you can have a list of Animals, and call Walk expectinga  return, but things bomb once Cat is encountered.

Copyright (c) Marimer LLC