locked
I do not get Warning Message when hiding a method! RRS feed

  • Question

  • User-1392235324 posted

    I learn from a video and and V.S. should display a warning message is in the picture below, but my V.S doesn't display it. How to change the settings to get this warning?

    MS Visual Studio doesn't show Warning Messages!

    Wednesday, May 27, 2015 5:09 AM

Answers

  • User1577371250 posted

    Hi,

    You are trying to override the method in the base class. SO use NEW Keyword as below.

    public class FullTimeEmployee : Employee
    {
            public new void PrintFullName()
            {
                Console.WriteLine("");
            }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 7:47 AM
  • User-434868552 posted

    @kareemva       ¿ what version of VS are you using ?

    Your version of VS is showing the warning ... that is why your PrintFullName method is underlined.

    also, if you look at the bar to the right of your code, you should see a yellow warning triangle.

    if you add the new keyword as shown above by Lokesh B R, the warning will disappear.

    With, or without the new  keyword in the example below, the output will be the same:

    Bob Smith - any employee
    Jane Doe - full time employee
    void Main()
    {
        Employee bobsmith = new Employee {FirstName = "Bob", LastName = "Smith"};
        bobsmith.PrintFullName();
        FullTimeEmployee janedoe  = new FullTimeEmployee {FirstName = "Jane", LastName = "Doe"};
        janedoe.PrintFullName();
    }

    with new

    public class FullTimeEmployee : Employee
    {
        public new void  PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - full time employee");}
    }

    without new

    public class FullTimeEmployee : Employee
    {
        public  void  PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - full time employee");}
    }

    The difference is that without the new keyword, you get the warning and PrintFulllName is underlined with a green squiggly line. 

    public class Employee
    {
    	public String FirstName { get; set; }
    	public String LastName { get; set; }
    	public void   PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - any employee");}
    }

    EDIT:

    like many programs from Microsoft, you will find Visual Studio setting via the Tools, Options... menu path.

    some options your will find via the Project, <your project name> Properties... menu path.

    click Build in the tree at the left side of the project properties page

    you having setting for "Errors and warnings" on the Build project properties page.

    END EDIT.

    EDIT #2:

    N.B.:  from the menu path, you can choose View, Error List to display the Error List window.

    the Error List window has tabs for Error and Warnings ... if you double click on an error or warning, you will be taken to the corresponding line of code in your VS editor window.

    END EDIT #2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 8:13 AM

All replies

  • User1577371250 posted

    Hi,

    You are trying to override the method in the base class. SO use NEW Keyword as below.

    public class FullTimeEmployee : Employee
    {
            public new void PrintFullName()
            {
                Console.WriteLine("");
            }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 7:47 AM
  • User-434868552 posted

    @kareemva       ¿ what version of VS are you using ?

    Your version of VS is showing the warning ... that is why your PrintFullName method is underlined.

    also, if you look at the bar to the right of your code, you should see a yellow warning triangle.

    if you add the new keyword as shown above by Lokesh B R, the warning will disappear.

    With, or without the new  keyword in the example below, the output will be the same:

    Bob Smith - any employee
    Jane Doe - full time employee
    void Main()
    {
        Employee bobsmith = new Employee {FirstName = "Bob", LastName = "Smith"};
        bobsmith.PrintFullName();
        FullTimeEmployee janedoe  = new FullTimeEmployee {FirstName = "Jane", LastName = "Doe"};
        janedoe.PrintFullName();
    }

    with new

    public class FullTimeEmployee : Employee
    {
        public new void  PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - full time employee");}
    }

    without new

    public class FullTimeEmployee : Employee
    {
        public  void  PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - full time employee");}
    }

    The difference is that without the new keyword, you get the warning and PrintFulllName is underlined with a green squiggly line. 

    public class Employee
    {
    	public String FirstName { get; set; }
    	public String LastName { get; set; }
    	public void   PrintFullName() {Console.WriteLine (FirstName + " "+ LastName + " - any employee");}
    }

    EDIT:

    like many programs from Microsoft, you will find Visual Studio setting via the Tools, Options... menu path.

    some options your will find via the Project, <your project name> Properties... menu path.

    click Build in the tree at the left side of the project properties page

    you having setting for "Errors and warnings" on the Build project properties page.

    END EDIT.

    EDIT #2:

    N.B.:  from the menu path, you can choose View, Error List to display the Error List window.

    the Error List window has tabs for Error and Warnings ... if you double click on an error or warning, you will be taken to the corresponding line of code in your VS editor window.

    END EDIT #2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 8:13 AM
  • User-1910946339 posted

    Would you walk up to a motor mechanic and say "That car over there works fine.  What is wrong with mine?"

    We need to see your faulty code not another persons working code.

    Friday, May 29, 2015 12:40 AM