Problem with overloaded functions

  • Thread starter Thread starter Daniel =?iso-8859-1?Q?Lidstr=F6m?=
  • Start date Start date
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hello,

I get warnings when trying to compile this code:

public class Point3D : Point2D
{
public void WriteXml(XmlWriter w)
{
// TODO
}

public XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public void WriteXml(XmlWriter w)
{
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}

class1.cs(23,15): warning CS0108: The keyword new is required on
'Point3D.WriteXml(System.Xml.XmlWriter)' because it hides inherited member
'Point2D.WriteXml(System.Xml.XmlWriter)'
class1.cs(65,15): (Related location)
class1.cs(32,20): warning CS0108: The keyword new is required on
'Point3D.GetSchema()' because it hides inherited member
'Point2D.GetSchema()'
class1.cs(73,20): (Related location)
class1.cs(38,15): warning CS0108: The keyword new is required on
'Point3D.ReadXml(System.Xml.XmlReader)' because it hides inherited member
'Point2D.ReadXml(System.Xml.XmlReader)'
class1.cs(79,15): (Related location)

Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?
Thanks!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
You should declare the overridable methods in the Point2D
as virtual to make them overridable(or abstract if they must be overridden,
and have no default inplementation).

Then use keyword override in the derived class Point3D,
like this:
public class Point3D : Point2D
{
public override void WriteXml(XmlWriter w)
{
// TODO
}

public override XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public virtual void WriteXml(XmlWriter w)
{
}

public virtual XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}
 
The 3D class is derived from the 2D class. The XmlWriter method is not
marked as virtual so overloads must have a different parameter signature or
the method will replace that of the base class. If the intention is to
replace the base method use the new keyword on the derived class, if the
intent is to override the method use the virtual keyword on the base class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?

You haven't overloaded any methods - you've hidden them. Overloading
occurs when there are multiple methods with the same name but different
signatures.

Were you actually intending to override the methods? If so, you need to
declare that they're virtual in the base class, and that you're
overriding them in the child class.
 
Back
Top