4 Replies - 573 Views - Last Post: 31 March 2013 - 07:57 PM Rate Topic: -----

#1 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Probleming implementing method inherited from interface

Posted 31 March 2013 - 07:38 PM

Question
Hello, I'm getting a series of errors in my C# program about methods from a certain interface defined in a series of sub-classes. The errors seem to be saying that the inherited method is not defined in the sub-classes. I'm not 100% on interfaces so the problem is probably the class definitions. I know that implementing interface methods seals the class but no classes derive from the sub-classes. See below for more information. Thank you.

Information
I'm getting the following errors in my program:

Quote

Error 1 'Editor2.Folder<Editor2.Script>' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'Editor2.Folder<Editor2.Script>' could be found (are you missing a using directive or an assembly reference?) f:\documents\visual studio 2010\Projects\Editor2\Editor2\Resources.cs 61 21 Editor2

Error 2 'Editor2.Folder<Editor2.GameObject>' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'Editor2.Folder<Editor2.GameObject>' could be found (are you missing a using directive or an assembly reference?) f:\documents\visual studio 2010\Projects\Editor2\Editor2\Resources.cs 62 21 Editor2

Error 3 'Editor2.Folder<Editor2.Room>' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'Editor2.Folder<Editor2.Room>' could be found (are you missing a using directive or an assembly reference?) f:\documents\visual studio 2010\Projects\Editor2\Editor2\Resources.cs 63 19 Editor2


They are caused on the 3 lines of the Load method here:
public class Project
{
    public Folder<script> scripts = new Folder<script>();
    public Folder<GameObject> objects = new Folder<GameObject>();
    public Folder<Room> rooms = new Folder<Room>();

    public void Load(XmlNode node)
    {
        scripts.Load(node.SelectSingleNode("scripts"));   <-- Error 1
        objects.Load(node.SelectSingleNode("objects"));   <-- Error 2
        rooms.Load(node.SelectSingleNode("rooms"));       <-- Error 3
    }
}


Each of the derived classes Script, GameObject and Room have headers like this...
public class Script : Resource, IResource


... and they each implement the Load method of IResource...
public interface IResource
{
    void Load(XmlNode node);
}


The class Resource just has a public string member. No class derives from any of these derived classes.

This post has been edited by Coding in the name of: 31 March 2013 - 07:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Probleming implementing method inherited from interface

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,327
  • Joined: 05-May 12

Re: Probleming implementing method inherited from interface

Posted 31 March 2013 - 07:44 PM

The error is about Folder<script> not having a Load() method that matches, not that Script not having a Load() method. How is your Folder<T> class implemented? Does it have a Load() method that takes an XML node?
Was This Post Helpful? 0
  • +
  • -

#3 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Probleming implementing method inherited from interface

Posted 31 March 2013 - 07:48 PM

Sorry, forgot that bit.

    public class Folder<T> : Resource, IResource where T : IResource
    {
        public Dictionary<string, T> resources = null;

        void IResource.Load(XmlNode node)
        {
            resources.Clear();

            foreach (XmlNode child in node.ChildNodes)
            {
                T resource = default(T);
                resource.Load(child);
                resources.Add(child.Attributes["name"].Value, resource);
            }
        }
    }

This post has been edited by Coding in the name of: 31 March 2013 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,327
  • Joined: 05-May 12

Re: Probleming implementing method inherited from interface

Posted 31 March 2013 - 07:53 PM

This:
void IResource.Load(XmlNode node)



Declares an an explicit interface method implementation. It is considered private. You need to change it to:
public void Load(XmlNode node)


Or change your calling code to:
((IResource) scripts).Load(node.SelectSingleNode("scripts"));


Was This Post Helpful? 1
  • +
  • -

#5 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Probleming implementing method inherited from interface

Posted 31 March 2013 - 07:57 PM

Thanks Skydiver, that fixed it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1