Re: Wrong overload resolution ?



Hi James,
Thanks for the interesting response. I agree that the question is - Is this a C# compiler bug or a specification design issue. Will look forward for someone to answer. My comment are below.
 
"James Curran" <jamescurran@xxxxxxxx> wrote in message news:uU3DCO3PFHA.3668@xxxxxxxxxxxxxxxxxxxx...
>     Well, I'm gonna guess that it's working according to the C# spec, but
> I'm leaning toward it being a rather bad design choice on the spec writers.
>
>     To follow what going on, add the lines:
> Class1 o1 = o2;
> o1.Method1(s);
>
> to the end of your Main() function.  Run it, and you'll note that while
> o2.Method1() give the wrong response, o1.Method1() is correct.

Yes, I know, This is bacause if the method is declared vurtual in the base class, the latest override will be invoked, even if the object is cast to the base type. Currently I resolve this issue by calling ((Class1)o2).Method1(s);

> Now,  replace the "override" with "new" (or just delete it).  Now,
> o2.Method1() is correct, while o1.Method1() is wrong.
 
I think we can't say wrong here. The "new" keyword prevents Class2.Method1(string s) from being an override of Class1.Method1(string s) and the rule i mentioned above do not apply. So if you have a variable of type Class1 pointing to an instance of Class2, the method of Class1 will be invoked. I think, this is normal behaviour.

>
> So, what I THOUGHT was happening was that Method1(object) was hiding the
> Class1.Method1() (including hiding it's override).

> BUT, now change Method1(object o) to Method1(int o).  Now, both o2.Method1()
> & o1.Method1() are correct.  So, it's only hiding it if the parameters are
> similar (C++ would hide it based on just the name)
 
I think this is because string cannot cast to int and the compiler takes the right way.
 

>
>
> "Vladimir Granitsky" <
vl_granitsky@xxxxxxxxxxx> wrote in message
>
news:##2N1G1PFHA.2788@xxxxxxxxxxxxxxxxxxxx...
> Hi guys,
>
> Please, look at the code below and try to step into it. The compiled code
> calls the loosely typed method public void Method1(object o) !?!?
>
> Am I right that C# compiler does wrong overload resolution ?
>
> I've used parameters of type object and string here, just to illustrate the
> problem. Really I have a bit more deep inheritance graph, and the things get
> more interesting if the strongly typed overload is like override public void
> Method1(BaseType x). When I call it with parameter of type SubType (that
> inherits BaseType) the right method is called.
>
> Thanks for any useful points.
>
> Regadrs,
> Vladimir Granitsky
> using System;using System.Diagnostics;namespace OverloadResolution{
> public class Class1    {        virtual public void Method1(string s)
> {            Trace.WriteLine("Class1.Method1");        }    }    public
> class Class2 : Class1    {        override public void Method1(string s)
> {            Trace.WriteLine("Class2.Method1a");        }        public void
> Method1(object o)        {
>     Trace.WriteLine("Class2.Method1b");        }    }    class Client
> {        [STAThread]        static void Main(string[] args)
> {            string s = "blah";            Class2 o2 = new Class2();
> o2.Method1(s);        }    }}
>
>


Relevant Pages

  • Re: override by name in c#?
    ... Try this songie, ... public void Show ... public class Class2: Class1 ... > But even though I didn't think this would work to override the base class> because it's not the same signature, it does, and apart from the warning, this> is what I want to happen. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Requesting tips, comments for an EDT thread-safe game architecture
    ... Use a Window or JWindow and do active rendering. ... synchronize some parts of your code but the more you can avoid that the ... public void myRenderingLoop() { ...
    (comp.lang.java.gui)
  • Re: Problem with overloaded functions
    ... Then use keyword override in the derived class Point3D, ... public void ReadXml2 ... > public class Point2D: IXmlSerializable ... > class1.cs: warning CS0108: The keyword new is required on> 'Point3D.WriteXml' because it hides inherited member ...
    (microsoft.public.dotnet.languages.csharp)
  • Requesting tips, comments for an EDT thread-safe game architecture
    ... me wonder whether my general game architecture was thread safe or not. ... public static void mainthrows InterruptedException, ... public void windowActivated{ ...
    (comp.lang.java.gui)
  • Re: JTree not updating after deleting a node in DnD
    ... public void fireTreeStructureChanged(Object source, TreePath path) ... public MyTransferable(final DefaultMutableTreeNode data) { ...
    (comp.lang.java.gui)