L
Lars
Hi,
I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?
I am fairly new to generics, but I am aware of that fact
that if you have a class B, that inherits from A, then
List<B> does NOT inherit from List<A>. So I understand
why the example below does not compile, but I fail to
see how to solve the problem stated above... For example,
I need to be able to return a BindingList<LeaseContractLine>
from the LeaseContract class below.
// Sample:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace GenericInheritanceTest
{
public partial class Form1 : Form
{
// A simple form with only a DataGridView on it:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LeaseContract leaseContract = new LeaseContract();
// Display the contract lines in a DataGridView:
uxLeaseContractGrid.DataSource = leaseContract.GetLines();
}
}
public class Contract
{
protected BindingList<ContractLine> _lines = new
BindingList<ContractLine>();
public Contract() { }
//
// methods providing functionality for all types of contracts...
//
}
public class LeaseContract : Contract
{
public LeaseContract() { }
public BindingList<LeaseContractLine> GetLines()
{
// Will not compile (since the cast is not valid):
return (BindingList<LeaseContractLine>)_lines;
}
//
// specific LeaseContract methods, etc.
//
}
public class ContractLine
{
public ContractLine() { }
}
public class LeaseContractLine : ContractLine
{
public LeaseContractLine() { }
}
}
Any help would be greatly appreicated!
Thanks,
Lars
I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?
I am fairly new to generics, but I am aware of that fact
that if you have a class B, that inherits from A, then
List<B> does NOT inherit from List<A>. So I understand
why the example below does not compile, but I fail to
see how to solve the problem stated above... For example,
I need to be able to return a BindingList<LeaseContractLine>
from the LeaseContract class below.
// Sample:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace GenericInheritanceTest
{
public partial class Form1 : Form
{
// A simple form with only a DataGridView on it:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LeaseContract leaseContract = new LeaseContract();
// Display the contract lines in a DataGridView:
uxLeaseContractGrid.DataSource = leaseContract.GetLines();
}
}
public class Contract
{
protected BindingList<ContractLine> _lines = new
BindingList<ContractLine>();
public Contract() { }
//
// methods providing functionality for all types of contracts...
//
}
public class LeaseContract : Contract
{
public LeaseContract() { }
public BindingList<LeaseContractLine> GetLines()
{
// Will not compile (since the cast is not valid):
return (BindingList<LeaseContractLine>)_lines;
}
//
// specific LeaseContract methods, etc.
//
}
public class ContractLine
{
public ContractLine() { }
}
public class LeaseContractLine : ContractLine
{
public LeaseContractLine() { }
}
}
Any help would be greatly appreicated!
Thanks,
Lars