121

I have a warning at the bottom of my screen:

Warning 1 'WindowsFormsApplication2.EventControlDataSet.Events' hides inherited member 'System.ComponentModel.MarshalByValueComponent.Events'. Use the new keyword if hiding was intended. C:\Users\myComputer\Desktop\Event Control\WindowsFormsApplication2\EventControlDataSet.Designer.cs 112 32 eventControl

If i double click on it, it comes up with:

public EventsDataTable Events {
    get {
        return this.tableEvents;
    }

Can anyone tell me how to get rid of this?

dcaswell
  • 3,137
  • 2
  • 26
  • 25
tony b
  • 1,341
  • 2
  • 10
  • 12

5 Answers5

169

Your class has a base class, and this base class also has a property (which is not virtual or abstract) called Events which is being overridden by your class. If you intend to override it put the "new" keyword after the public modifier. E.G.

public new EventsDataTable Events
{
  ..
}

If you don't wish to override it change your properties' name to something else.

wdavo
  • 5,070
  • 1
  • 19
  • 20
  • 26
    [Here is a link](http://stackoverflow.com/a/6576212/3156863) for anyone who wants to know what the *difference* between new and override is. – starsplusplus May 25 '15 at 09:45
  • 1
    The `new` keyword would get rid of the compilation error, but may introduce other unintended behavior. I think you should add a warning to your answer. That would be helpful for understanding the consequences of adding `new`. – ahong Jun 02 '20 at 07:59
13

@wdavo is correct. The same is also true for functions.

If you override a base function, like Update, then in your subclass you need:

new void Update()
{
  //do stufff
}

Without the new at the start of the function decleration you will get the warning flag.

Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • What is the purpose of using 'new' keyword as even without using the 'new' keyword the result is same, i.e., the base class method implementation is not invoked. – Carthic Nov 13 '21 at 10:16
10

In the code below, Class A implements the interface IShow and implements its method ShowData. Class B inherits Class A. In order to use ShowData method in Class B, we have to use keyword new in the ShowData method in order to hide the base class Class A method and use override keyword in order to extend the method.

interface IShow
{
    protected void ShowData();
}

class A : IShow
{
    protected void ShowData()
    {
        Console.WriteLine("This is Class A");
    }
}

class B : A
{
    protected new void ShowData()
    {
        Console.WriteLine("This is Class B");
    }
}
Beltaine
  • 2,721
  • 1
  • 18
  • 22
Joee
  • 1,834
  • 18
  • 19
  • 3
    There's no `override` in your example from your explication and `override` is not needed. – Beltaine Dec 20 '17 at 12:30
  • I think your example is on the right track, but is incomplete. Also your explanation is unclear. Could you edit your answer to be something more like https://dotnetfiddle.net/Iw0OzB? If not I'll probably post my fiddle as another answer – ahong Jun 02 '20 at 08:03
2

The parent function needs the virtual keyword, and the child function needs the override keyword in front of the function definition.

James L.
  • 12,893
  • 4
  • 49
  • 60
1

this warning also triggers when you have:
x:Name="Name1" with Text="{Binding Name1}" the Same Property Name in same Element in your <Xaml>
which can cause a serious conflict at a certain point when your binding process become more complex.

The Doctor
  • 636
  • 1
  • 7
  • 23