G
Gary Rynearson
The code below does not compile and I cannot figure out why. The compiler
issues the error: "Cannot convert type 'CustomCollection' to
System.Collections.Generic.List<IGraphableRecord>".
1.
I have a IGraphableRecord interface. The class CustomGraphRecord implements
the interface (so, CustomGraphRecord "is a" IGraphableRecord).
2.
I have a CustomCollection class which is a generic list of CustomGraphRecord
objects.
3.
OK, now I have an interface, IGraphableTable, which specifies a method
GetGraphData () that returns a Generic list of IGraphableRecord objects. I
also have a class which attempts to implement the interface. It contains a
private variable (_collectionOfRecords) of type, CustomCollection. I am
trying to satisfy the interface requirement of implementing the
GetGraphData() method by returning the variable _collectionOfRecords.
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.Collections.Generic;
using System.Text;
public interface IGraphableRecord
{
int X { get;}
int Y { get;}
}
public class CustomGraphRecord : IGraphableRecord
{
int _x, _y;
public int X { get { return _x; } }
public int Y { get { return _y; } }
public string Name { get { return "Custom"; } }
}
public class CustomCollection : List<CustomGraphRecord>
{
}
public interface IGraphableTable
{
List<IGraphableRecord> GetGraphData();
}
public class CustomGraphUnit : IGraphableTable
{
CustomCollection _collectionOfRecords = new CustomCollection();
public List<IGraphableRecord> GetGraphData()
{
return (List<IGraphableRecord>) _collectionOfRecords;
}
}
issues the error: "Cannot convert type 'CustomCollection' to
System.Collections.Generic.List<IGraphableRecord>".
1.
I have a IGraphableRecord interface. The class CustomGraphRecord implements
the interface (so, CustomGraphRecord "is a" IGraphableRecord).
2.
I have a CustomCollection class which is a generic list of CustomGraphRecord
objects.
3.
OK, now I have an interface, IGraphableTable, which specifies a method
GetGraphData () that returns a Generic list of IGraphableRecord objects. I
also have a class which attempts to implement the interface. It contains a
private variable (_collectionOfRecords) of type, CustomCollection. I am
trying to satisfy the interface requirement of implementing the
GetGraphData() method by returning the variable _collectionOfRecords.
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.Collections.Generic;
using System.Text;
public interface IGraphableRecord
{
int X { get;}
int Y { get;}
}
public class CustomGraphRecord : IGraphableRecord
{
int _x, _y;
public int X { get { return _x; } }
public int Y { get { return _y; } }
public string Name { get { return "Custom"; } }
}
public class CustomCollection : List<CustomGraphRecord>
{
}
public interface IGraphableTable
{
List<IGraphableRecord> GetGraphData();
}
public class CustomGraphUnit : IGraphableTable
{
CustomCollection _collectionOfRecords = new CustomCollection();
public List<IGraphableRecord> GetGraphData()
{
return (List<IGraphableRecord>) _collectionOfRecords;
}
}