"Use the new keyword if hiding was intended" warning

C#Winforms

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

@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.

Solution 3 - C#

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");
    }
}

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontony bView Question on Stackoverflow
Solution 1 - C#wdavoView Answer on Stackoverflow
Solution 2 - C#AggressorView Answer on Stackoverflow
Solution 3 - C#JoeeView Answer on Stackoverflow
Solution 4 - C#James L.View Answer on Stackoverflow