[2010] What is wrong with the code?
Hi, Here I gave the code. My aim : I am trying to make the frmView to generic for populating from Manager Class List.
Code:
namespace WindowsFormsApplication1
{
public abstract class AbsLookUpView
{
public abstract string LookupName{get;}
}
}
--------------------------------------------------------------------------
namespace WindowsFormsApplication1
{
public abstract class AbsManager<T> where T:AbsLookUpView
{
public abstract List<T> LoadAll();
}
}
==========================================================
public partial class Department: AbsLookUpView
{
public override string LookupName
{
get
{
return "Department";
}
}
}
==========================================================
namespace WindowsFormsApplication1
{
public class DeptManager : AbsManager<Department>
{
private DataClasses1DataContext dc = new DataClasses1DataContext();
public override List<Department> LoadAll()
{
return dc.Departments.ToList<Department>();
}
}
}
==========================================================
namespace WindowsFormsApplication1
{
public partial class frmView : Form
{
public AbsManager<AbsLookUpView> ManagerCls;
public void BindGrid()
{
this.lkpDataGridView.DataSource = ManagerCls.LoadAll();
}
}
}
==========================================================
namespace WindowsFormsApplication1
{
public partial class frmTest : frmView
{
public frmTest()
{
InitializeComponent();
}
private void frmTest_Load(object sender, EventArgs e)
{
AbsManager<AbsLookUpView> dmb ;
DeptManager dm = new DeptManager();
dmb = dm; [Error in this line is given below]
this.ManagerCls = dmb;
this.BindGrid();
Form5 f = new Form5(this);
this.ChildForm = f;
}
}
}
==========================================================
Error: Cannot implicitly convert type 'WindowsFormsApplication1.DeptManager' to 'WindowsFormsApplication1.AbsManager<WindowsFormsApplication1.AbsLookUpView>' E:\test\WindowsFormsApplication1\WindowsFormsApplication1\frmTest.cs
here, DeptManager is inherited from AbsManager<WindowsFormsApplication1.AbsLookUpView>. Then why it throws error. Pls explain.
What is wrong with the above code. Why is this error is occuring? Pls explain experts.:eek2:
Re: [2010] What is wrong with the code?
"DeptManager is inherited from AbsManager<WindowsFormsApplication1.AbsLookUpView>"
It is?
You have: public class DeptManager : AbsManager<Department>
If it inherited from AbsLookUpView wouldn't it look like this:
public class DeptManager : AbsManager<AbsLookUpView>
Or did I miss something?
-tg
Edit - never mind... I did miss something... ok... back to square 1.
Re: [2010] What is wrong with the code?
The fact that String inherits Object does not mean that List<String> inherits List<Object>. Likewise, the fact that Department inherits AbsLookUpView does not mean that AbsManager<Department> inherits AbsManager<AbsLookUpView>. Your DeptManager class inherits AbsManager<Department> so it can be cast as type AbsManager<Department>. It doesn't inherit AbsManager<AbsLookUpView> so it can't be cast as that type.
Re: [2010] What is wrong with the code?
Then, how can we change this code to achieve the aim. Any suggestion to achieve this:confused:
Re: [2010] What is wrong with the code?
Quote:
Originally Posted by
senthilkumartd
Then, how can we change this code to achieve the aim. Any suggestion to achieve this:confused:
You have to change your aim.