I'm struggling with a situation that I come up again time and time again but I am not sure whether the way that I am doing things is wrong or whether I could be doing things in a different way.
An Example:
I have a Windows Form that has a DataGridView with some private methods to perform validation of the datagrid and interpreting right mouse clicks on the datagridview etc. This windows form is essentially an "abstract" class and is never instantiated directly.
I then inherit from this base class and customise it in various ways (template pattern) e.g. define the columns of the datagridview and particular formatting methods which are specific to those columns etc.
When I consume these classes the base class public methods form my interface and I can instantiate the particular type of datagridview that I want and manipulate it through the common interface. Lovely.
The Problem:
The main problem is that you cannot actually declare a Windows Form class as abstract without causing the Visual Studio designer to throw a wobbly as it cannot instantiate these abstract classes.
Some Solutions:
At the moment I am "implementing" those methods in the base class that I want to be overridden with:
throw new NotSupportedException();
so at least if I forget to override one of these methods that form my interface. This seems rather smelly to me though and I don't really like it.
Another solution I toyed with was to do away with inheritance altogether and define an interface (e.g. IMyDataGrid) and implement that in each datagridview class (sort of strategy pattern). The problem here though is that you lose the benefits of code re-use that inheritance gives you meaning that you have to create many different forms, drop a datagridview on them - effectively copy and pasting the same code into each one. Bad.
Is there a better way of trying to achieve this?
abstract keyword is used to create a abstract class and method. Abstract class in java can't be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.
The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.
A Windows Forms application is an event-driven application supported by Microsoft's . NET Framework. Unlike a batch program, it spends most of its time simply waiting for the user to do something, such as fill in a text box or click a button. The code for the application can be written in a .
There are many ways to do this depending on your requirements.
DataGridView
that implements an interface to do the custom logicvirtual
methods instead of using an abstract
classYou'll have to pick an option that best suits your needs. Without knowing the domain and specifics in which your question is being asked, I don't think we can give you a 100% certain answer.
Check out this method to know how to create the two attributes needed.
You need the following attribute and type descriptor class (code taken from UrbanPotato)
// Source : taken from http://www.urbanpotato.net/default.aspx/document/2001 Seem to be down
// Allow the designer to load abstract forms
namespace YourNamespace
{
// Place this attribute on any abstract class where you want to declare
// a concrete version of that class at design time.
[AttributeUsage(AttributeTargets.Class)]
public class ConcreteClassAttribute : Attribute
{
Type _concreteType;
public ConcreteClassAttribute(Type concreteType)
{
_concreteType = concreteType;
}
public Type ConcreteType { get { return _concreteType; } }
}
// Here is our type description provider. This is the same provider
// as ConcreteClassProvider except that it uses the ConcreteClassAttribute
// to find the concrete class.
public class GeneralConcreteClassProvider : TypeDescriptionProvider
{
Type _abstractType;
Type _concreteType;
public GeneralConcreteClassProvider() : base(TypeDescriptor.GetProvider(typeof(Form))) { }
// This method locates the abstract and concrete
// types we should be returning.
private void EnsureTypes(Type objectType)
{
if (_abstractType == null)
{
Type searchType = objectType;
while (_abstractType == null && searchType != null && searchType != typeof(Object))
{
foreach (ConcreteClassAttribute cca in searchType.GetCustomAttributes(typeof(ConcreteClassAttribute), false))
{
_abstractType = searchType;
_concreteType = cca.ConcreteType;
break;
}
searchType = searchType.BaseType;
}
if (_abstractType == null)
{
// If this happens, it means that someone added
// this provider to a class but did not add
// a ConcreteTypeAttribute
throw new InvalidOperationException(string.Format("No ConcreteClassAttribute was found on {0} or any of its subtypes.", objectType));
}
}
}
// Tell anyone who reflects on us that the concrete form is the
// form to reflect against, not the abstract form. This way, the
// designer does not see an abstract class.
public override Type GetReflectionType(Type objectType, object instance)
{
EnsureTypes(objectType);
if (objectType == _abstractType)
{
return _concreteType;
}
return base.GetReflectionType(objectType, instance);
}
// If the designer tries to create an instance of AbstractForm, we override
// it here to create a concerete form instead.
public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
{
EnsureTypes(objectType);
if (objectType == _abstractType)
{
objectType = _concreteType;
}
return base.CreateInstance(provider, objectType, argTypes, args);
}
}
}
Assign them to your abstract form like this:
[TypeDescriptionProvider(typeof(GeneralConcreteClassProvider))]
[ConcreteClass(typeof(MyAbstractConcreteForm))]
public abstract partial class MyAbstractForm : Form
{
}
Create a new class that will inherit from your abstract form. This class will be instanciated by Visual Studio
public class MyAbstractConcreteForm: MyAbstractForm
{
public MyAbstractConcreteForm() : base() { }
}
This should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With