PDA

Click to See Complete Forum and Search --> : Big confusion with virtual/overrides..I dont get it..


PT Exorcist
Dec 17th, 2003, 01:43 PM
why should i set a function virtual and then use override to replace it if i can simply make:


//base class
public void LOL() {
}

//derived class
public new void LOL() {
}

Memnoch1207
Dec 17th, 2003, 04:53 PM
Good point.
You wouldn't need to as long as the method signatures were the same.
Example:

// C# Document
using System;

class One
{
public void A()
{
Console.WriteLine("One.A() being called");
}
}

class Test : One
{
public static void Main()
{
Test a = new Test();

a.A();
}

public new void A()
{
Console.WriteLine("Test.A() being called");
}
}

Displays:
Test.A() being called

But if you comment out the A() in class test, then it displays.
One.A() being called

My understanding with the virtual/overrides issue is the functionality is there to help deal with compatibility issues between different versions of classes.

Pirate
Jan 4th, 2004, 12:37 PM
This is called Shadowing in VB.NET (hiding in C#) . If you want to obscure a base class method and provide new implementation with the same name but different access level or different return type .
It's different from virtual and overriding in sense of overriden methods must have the same signature and same return types as the base class method .

PT Exorcist
Jan 5th, 2004, 01:55 PM
If I want to hide a method in my derived class that is public in the original class can I do shadowing changing it to private so it doesn't appear in the derived class to the rest of the classes?

edit: Just tried it and it doesnt work as I thought..it would be cool if it worked

Pirate
Jan 5th, 2004, 02:05 PM
I don't think it would be much helpful . I mean this would make sense if you're working on another developer's class and you want to provide the same names for the methods but with different return type or modifiers .

BramVandenbon
May 31st, 2004, 11:45 PM
class One
{
public void A(int one, int two, int three)
{
Console.WriteLine("One.A() being called: {0}", one);
Console.WriteLine("One.A() being called: {0}", two);
Console.WriteLine("One.A() being called: {0}", three);
}
}

class Test : One
{
public static void Main()
{
Test a = new Test();

a.A();
}

public void A(int one, int two, int three, int four):base(one,two,three)
{
Console.WriteLine("Test.A() being called: {0}", four);
}
}


Can't you use other signatures too, like here ? Again, why would we use virtual. (The only explanation I can find would be for Polymorphism when making an instance from a class that is inheriting from an inheritance of an interface. And declare the instance as a reference to the interface type. for example:)

public interface IShape
{
string Name();
}

public class Square : IShape
{
public virtual string Name()
{
return "Square";
}
}

public class Box : Square
{
public override string Name()
{
return "Box";
}
}

class Head
{
static void Main(){
IShape[] Table1 = new IShape[4];
Table1[0]= new Square(10,2);
Table1[1]= new Box(8 , 5, '&');
Table1[2]= new Square(7,4);
Table1[3]= new Box(9,2, '#');
string output = "";
for (int i=0;i < Table1.Length;i++)
{
output += Table1[i].Name() + "\n";
}
Console.WriteLine(output);
}
}

Without the virtual and override here, the output would 4 times use the .Name of the Square class here. Instead like I showed it here, the .Name of the Box class will be found if necessary. (You'll get Dynamic binding, instead of static binding if you use override. In all other situations Virtual seems to be unnecessary and just something people seem to use to avoid complications.)