Inheritance Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Rynearson

    Inheritance Question

    The code below does not compile and I cannot figure out why. The compiler
    issues the error: "Cannot convert type 'CustomCollecti on' to
    System.Collecti ons.Generic.Lis t<IGraphableRec ord>".

    1.
    I have a IGraphableRecor d interface. The class CustomGraphReco rd implements
    the interface (so, CustomGraphReco rd "is a" IGraphableRecor d).

    2.
    I have a CustomCollectio n class which is a generic list of CustomGraphReco rd
    objects.

    3.
    OK, now I have an interface, IGraphableTable , which specifies a method
    GetGraphData () that returns a Generic list of IGraphableRecor d objects. I
    also have a class which attempts to implement the interface. It contains a
    private variable (_collectionOfR ecords) of type, CustomCollectio n. I am
    trying to satisfy the interface requirement of implementing the
    GetGraphData() method by returning the variable _collectionOfRe cords.
    However, this is where the above mentioned compile error occurs.

    I have tried a couple variations, but have been uncessfull at getting a
    compile. Can anyone provide ideas on what is wrong and how to fix it?

    Gary



    using System;

    using System.Collecti ons.Generic;

    using System.Text;

    public interface IGraphableRecor d

    {

    int X { get;}

    int Y { get;}

    }

    public class CustomGraphReco rd : IGraphableRecor d

    {

    int _x, _y;

    public int X { get { return _x; } }

    public int Y { get { return _y; } }

    public string Name { get { return "Custom"; } }

    }

    public class CustomCollectio n : List<CustomGrap hRecord>

    {

    }

    public interface IGraphableTable

    {

    List<IGraphable RecordGetGraphD ata();

    }

    public class CustomGraphUnit : IGraphableTable

    {

    CustomCollectio n _collectionOfRe cords = new CustomCollectio n();

    public List<IGraphable RecordGetGraphD ata()

    {

    return (List<IGraphabl eRecord>) _collectionOfRe cords;

    }

    }


  • Gary Rynearson

    #2
    Re: Inheritance Question

    OK ... that link does explain why the code does not compile, and I am now
    able to get the code to compile. However, my NUnit test generates a runtime
    exception:

    TestCovariantGe nerics.TestCrea tingObjects.A01 _CanCreateCusto mGraphUnit :

    System.InvalidC astException :

    Unable to cast object of type

    'CovariantGener ics.EnumerableG eneric`2[CovariantGeneri cs.CustomGraphR ecord,

    CovariantGeneri cs.IGraphableRe cord]'

    to type

    'System.Collect ions.Generic.IL ist`1[CovariantGeneri cs.IGraphableRe cord]'.



    I think the code gets pretty ugly, in terms of casting requirements, in the
    GetGraphData() method of the CustomGraphUnit class.

    Can anyone provide suggestions on how to get this test to work?

    Here is the new code:
    using System;

    using System.Collecti ons.Generic;

    using System.Text;

    using System.Collecti ons;

    namespace CovariantGeneri cs

    {

    public interface IGraphableRecor d

    {

    int X { get;}

    int Y { get;}

    }

    public class CustomGraphReco rd : IGraphableRecor d

    {

    int _x, _y;

    string _name;

    public int X { get { return _x; } }

    public int Y { get { return _y; } }

    public string Name { get { return _name; } }

    public CustomGraphReco rd(int x, int y, string name)

    {

    _x = x;

    _y = y;

    _name = name;

    }

    }

    public class CustomCollectio n : List<CustomGrap hRecord>

    {

    }

    public interface IGraphableTable

    {

    IList<IGraphabl eRecordGetGraph Data();

    }

    public class EnumerableGener ic<TClass, TInterface: IEnumerable<TIn terface>

    where TClass : TInterface

    {

    private IList<TClasslis t;

    public EnumerableGener ic(IList<TClass list)

    {

    this.list = list;

    }

    public IEnumerator<TIn terfaceGetEnume rator()

    {

    foreach (TClass item in list)

    yield return item;

    }

    IEnumerator IEnumerable.Get Enumerator()

    {

    return this.GetEnumera tor();

    }

    }

    public class CustomGraphUnit : IGraphableTable

    {

    CustomCollectio n _collectionOfRe cords;

    public CustomGraphUnit (CustomCollecti on coll)

    {

    _collectionOfRe cords = coll;

    }

    public IList<IGraphabl eRecordGetGraph Data()

    {

    return (IList<IGraphab leRecord>)

    new EnumerableGener ic<CustomGraphR ecord, IGraphableRecor d>

    (_collectionOfR ecords); // _collectionOfRe cords;

    }

    }

    }

    namespace TestCovariantGe nerics

    {

    using CovariantGeneri cs;

    using NUnit.Framework ;

    [TestFixture]

    public class TestCreatingObj ects

    {

    [Test]

    public void A01_CanCreateCu stomGraphUnit()

    {

    // Create CustomCollectio n

    CustomCollectio n coll = createCustomCol l();

    CustomGraphUnit cgu = new CustomGraphUnit (coll);

    IList<IGraphabl eRecordgRecs = cgu.GetGraphDat a();

    }

    private CustomCollectio n createCustomCol l()

    {

    CustomCollectio n coll = new CustomCollectio n();

    for (int i = 0; i < 10; i++)

    {

    CustomGraphReco rd rec = new CustomGraphReco rd(

    i + 1,

    (i + 3) * 2,

    "item " + i.ToString());

    coll.Add(rec);

    }

    return coll;

    }

    }

    }


    Comment

    • Gary Rynearson

      #3
      Re: Inheritance Question

      OK, here is a working solution, with NUnit tests. My model uses point and
      line objects.

      using System;

      using System.Collecti ons.Generic;

      using System.Text;

      using System.Collecti ons;

      namespace CovariantGeneri cs

      {

      public interface IGraphablePoint

      {

      int X { get;}

      int Y { get;}

      }

      public class PointWithLable : IGraphablePoint

      {

      int _x, _y;

      string _name;

      public int X

      {

      get { return _x; }

      set { _x = value; }

      }

      public int Y

      {

      get { return _y; }

      set { _y = value; }

      }

      public string Name

      {

      get { return _name; }

      set { _name = value; }

      }

      public PointWithLable( int x, int y, string name)

      {

      _x = x;

      _y = y;

      _name = name;

      }

      public override string ToString()

      {

      return string.Format(" X = {0}; Y = {1}; Name = {2}",

      X, Y, Name);

      }

      }

      public class PointWithNoLabl e : IGraphablePoint

      {

      int _x, _y;

      #region IGraphablePoint Members

      public int X

      {

      get { return _x; }

      set { _x = value; }

      }

      public int Y

      {

      get { return _y; }

      set { _y = value; }

      }

      #endregion

      public PointWithNoLabl e(int x, int y)

      {

      _x = x;

      _y = y;

      }

      public override string ToString()

      {

      return string.Format(" X = {0}; Y = {1}",

      X, Y);

      }

      }

      public class LabledPointList : List<PointWithL able>

      {

      }

      public class UnLabledPointLi st : List<PointWithN oLable>

      {

      }

      public interface IGraphableLine

      {

      IEnumerable<IGr aphablePointGet ThePoints();

      }

      public class LabledLine : IGraphableLine

      {

      LabledPointList _points;

      public LabledLine(Labl edPointList points)

      {

      _points = points;

      }

      #region IGraphableLine Members

      public IEnumerable<IGr aphablePointGet ThePoints()

      {

      return new EnumerableGener ic<PointWithLab le, IGraphablePoint >(_points);

      }

      #endregion

      }

      public class UnLabledLine : IGraphableLine

      {

      UnLabledPointLi st _points;

      public UnLabledLine(Un LabledPointList points)

      {

      _points = points;

      }

      #region IGraphableLine Members

      public IEnumerable<IGr aphablePointGet ThePoints()

      {

      return new EnumerableGener ic<PointWithNoL able, IGraphablePoint >(_points);

      }

      #endregion

      }

      public class EnumerableGener ic<TClass, TInterface: IEnumerable<TIn terface>

      where TClass : TInterface

      {

      private List<TClasslist ;

      public EnumerableGener ic(List<TClassl ist)

      {

      this.list = list;

      }

      public IEnumerator<TIn terfaceGetEnume rator()

      {

      foreach (TClass item in list)

      yield return item;

      }

      IEnumerator IEnumerable.Get Enumerator()

      {

      return this.GetEnumera tor();

      }

      }

      }

      namespace TestCovariantGe nerics2

      {

      using CovariantGeneri cs;

      using NUnit.Framework ;

      [TestFixture]

      public class TestCreatingObj ects

      {

      [Test]

      public void A01_CanCreateLi neWithNamePoint s()

      {

      // Create CustomCollectio n

      LabledPointList pList = createLabledPoi ntList();

      LabledLine line = new LabledLine(pLis t);

      foreach (PointWithLable pt in line.GetThePoin ts())

      {

      //Console.WriteLi ne("Point is type: " +

      // pt.GetType().To String());

      Console.WriteLi ne(pt.ToString( ));

      }

      }

      [Test]

      public void A01_CanCreateLi neWithNoNamePoi nts()

      {

      UnLabledPointLi st pList = createUnLabledP ointList();

      UnLabledLine line = new UnLabledLine(pL ist);

      foreach (PointWithNoLab le pt in line.GetThePoin ts())

      {

      Console.WriteLi ne(pt.ToString( ));

      }

      }

      private UnLabledPointLi st createUnLabledP ointList()

      {

      UnLabledPointLi st upList = new UnLabledPointLi st();

      for (int i = 0; i < 10; i++)

      {

      upList.Add(new PointWithNoLabl e(

      (i + 1),

      (i + 2) * 3));

      }

      return upList;

      }



      private LabledPointList createLabledPoi ntList()

      {

      LabledPointList p = new LabledPointList ();

      for (int i = 0; i < 10; i++)

      {

      p.Add(new PointWithLable(

      (i + 1),

      (i + 2) * 3,

      "Name " + i.ToString()));

      }

      return p;

      }

      }

      }


      Comment

      Working...