Generic Class with Inheritance
-
I have a generic class which I use to populate a BindingList which I then use as the DataSource of my DataGridView. This class inherits from my LINQ table and adds some extra properties such as State etc... The problem is that I have to creat a new class for every single LINQ table I choose to use this way, I need some way to make my inheritance generic. This code works for that specific class (but it's not really generic since it only works for "myExampleTable"): Please not that <<T>> is only written with double brackets because the correct syntax doesn't want to display in CP.
namespace BL.CustomLINQ { public class StateTracker<<T>> : myExampleTable where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
But as I said, this code is useless for me! This is what I really want to do, but I'm doing it wrong:namespace BL.CustomLINQ { public class StateTracker<<T>> : T where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
Note that I'm inheriting from the same class as the one I passed in as the generic. The error I get (underlining T) is: Cannot derive from 'T' because it is a type parameter. Is there any way to do this or should I try a different (less appealing ) approach? -
I have a generic class which I use to populate a BindingList which I then use as the DataSource of my DataGridView. This class inherits from my LINQ table and adds some extra properties such as State etc... The problem is that I have to creat a new class for every single LINQ table I choose to use this way, I need some way to make my inheritance generic. This code works for that specific class (but it's not really generic since it only works for "myExampleTable"): Please not that <<T>> is only written with double brackets because the correct syntax doesn't want to display in CP.
namespace BL.CustomLINQ { public class StateTracker<<T>> : myExampleTable where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
But as I said, this code is useless for me! This is what I really want to do, but I'm doing it wrong:namespace BL.CustomLINQ { public class StateTracker<<T>> : T where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
Note that I'm inheriting from the same class as the one I passed in as the generic. The error I get (underlining T) is: Cannot derive from 'T' because it is a type parameter. Is there any way to do this or should I try a different (less appealing ) approach?I don't think this could work. Inheritance is handled at compile time while Generic type parameters are resolved at run time. There's no way for the compiler to know what the base class of of StateTracker<T> would be, and therefore what the functional interface to that base class would be. For example, in your default constructor you are explicitly calling the default constructor of the base class - but what if type T does not define a default constructor or the default constructor is hidden (private)? In my opinion you would be better off to create your StateTracker class to contain an element of Type T and interact with it in this manner even if C# did allow you to derive from a Generic type.
"We are men of action; lies do not become us."
-
I have a generic class which I use to populate a BindingList which I then use as the DataSource of my DataGridView. This class inherits from my LINQ table and adds some extra properties such as State etc... The problem is that I have to creat a new class for every single LINQ table I choose to use this way, I need some way to make my inheritance generic. This code works for that specific class (but it's not really generic since it only works for "myExampleTable"): Please not that <<T>> is only written with double brackets because the correct syntax doesn't want to display in CP.
namespace BL.CustomLINQ { public class StateTracker<<T>> : myExampleTable where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
But as I said, this code is useless for me! This is what I really want to do, but I'm doing it wrong:namespace BL.CustomLINQ { public class StateTracker<<T>> : T where T:class { public enum ErrorState { NoError = 0, RowIsIncomplete = 1, RowValidationFailed = 2 } public int state{get;set;} public ErrorState RowErrorState { get; set; } public StateTracker() : base() { } } }
Note that I'm inheriting from the same class as the one I passed in as the generic. The error I get (underlining T) is: Cannot derive from 'T' because it is a type parameter. Is there any way to do this or should I try a different (less appealing ) approach?You can't do that. Can you tell us why you need such a design?
Navaneeth How to use google | Ask smart questions
-
You can't do that. Can you tell us why you need such a design?
Navaneeth How to use google | Ask smart questions
I'm kinda struggeling with the DataGridView, this control has given me some grey hairs lately. It's my first time that I use it in this manner. The reason I'm doing this StateTracker thing is to be able to add extra information to this list that I bind to the DataGridView which I use to label a row as new or edited etc. I'm doing this because, when done editing, I submit only the affected rows to my Web Service (WCF) which then updates the rows appropiately. Is there a better way?
-
I'm kinda struggeling with the DataGridView, this control has given me some grey hairs lately. It's my first time that I use it in this manner. The reason I'm doing this StateTracker thing is to be able to add extra information to this list that I bind to the DataGridView which I use to label a row as new or edited etc. I'm doing this because, when done editing, I submit only the affected rows to my Web Service (WCF) which then updates the rows appropiately. Is there a better way?
I'm not sure about the necessity of using a StateTracker to track the state of a row, but notwithstanding that, why would you need to derive the StateTracker from a generic type? Based on your post, I am guessing that you want to actually derive the StateTracker from the List that is used as the GridView data source. Assuming you could do this, it really wouldn't be an effective inheritance model because A List is a logical grouping of objects, and a StateTracker is an object created for tracking the state of something. There really is no logical inheritance relation between the two. I think it would be more effective to use a model like this: Say you have objects you are viewing in your GridView. We'll call them objects of type Widget. Now you want to populate your GridView with a data source. You're using a List<Widget> collection object as your data source. Great, except this doesn't have all the data you need. You want to track the state of the List<Widget>. So just create an overloaded class, call it WidgetCollection or something like that. This class will inherit from List<Widget> and also contain an instance of the StateTracker class (which is a base type - not derived from anything other than System.Object). This inner instance of StateTracker should be used to track the state of each WidgetCollection. Again I don't know if this would be the best solution because I don't fully know your situation, but something along these lines should meet your needs - You have a collection of objects that either tracks state on the collection level or the object level. If you are tracking on the collection level, then the collection contains a StateTracker to handle this. If you are tracking on the object level, then each Object contains a StateTracker to handle this. Hope that helps or gets you pointed in the right direction.
"We are men of action; lies do not become us."
-
I'm not sure about the necessity of using a StateTracker to track the state of a row, but notwithstanding that, why would you need to derive the StateTracker from a generic type? Based on your post, I am guessing that you want to actually derive the StateTracker from the List that is used as the GridView data source. Assuming you could do this, it really wouldn't be an effective inheritance model because A List is a logical grouping of objects, and a StateTracker is an object created for tracking the state of something. There really is no logical inheritance relation between the two. I think it would be more effective to use a model like this: Say you have objects you are viewing in your GridView. We'll call them objects of type Widget. Now you want to populate your GridView with a data source. You're using a List<Widget> collection object as your data source. Great, except this doesn't have all the data you need. You want to track the state of the List<Widget>. So just create an overloaded class, call it WidgetCollection or something like that. This class will inherit from List<Widget> and also contain an instance of the StateTracker class (which is a base type - not derived from anything other than System.Object). This inner instance of StateTracker should be used to track the state of each WidgetCollection. Again I don't know if this would be the best solution because I don't fully know your situation, but something along these lines should meet your needs - You have a collection of objects that either tracks state on the collection level or the object level. If you are tracking on the collection level, then the collection contains a StateTracker to handle this. If you are tracking on the object level, then each Object contains a StateTracker to handle this. Hope that helps or gets you pointed in the right direction.
"We are men of action; lies do not become us."
Hi cor2879 Thank you for that!
cor2879 wrote:
So just create an overloaded class, call it WidgetCollection or something like that. This class will inherit from List and also contain an instance of the StateTracker class
From this I understand that I cannot get away without creating a special class for every table in my LINQ model. Also, tell me what you think, but I think that it would work better to create a class, say, TrackedWidget which inherits from Widget, then my DataGridView's datasource is List<<TrackedWidget>>. This way I'll have object level tracking. Another solution I've been working on, was to create a Dictionary<<int1, int2>> to record my object's state, where int1 is the value of the primary key column of that table and int2 is the state enumerator. Thank you again for your help!