Abstract classes and usercontrol design

  • Thread starter Thread starter steve bull
  • Start date Start date
S

steve bull

I created a usercontrol class, RGBColorSpace, which is derived from an abstract class, ColorSpace, but when I try to
click on the design panel for the control I get an error message "Unable to create instance of abstract class
ColorSpace".

I never try to create a class ColorSpace and don't really want to create a regular class with virtual methods. Does
anyone know why I am having this problem? The code seems to run fine and I can actually debug the control without the
problem arising. As I say it is only when I look at the design interface for the control.

Thanks,

Steve
 
Hi Steve,
I dont have any access to instance abstracts classes, abstract class just
can be used from an inheritance.
That means, you need to inherit the ColorSpace and so you can use it.

Regards,
 
Hi Steve,

yes, you can't use abstract controls in the VS-Designer.
That's because the Designer loads an instance of any Control shown
(including the base-class of the designed class). "That problem is by
design." as would Microsoft say.
 
but I am not trying to create an abstract control only a control that is derived from an abstract class. I don't
understand why that should be a problem.
 
this is the code. though I stripped down the class to basically nothing. The parent class I changed to be non-abstract
though I have just commented out most of the changes. RGB is a public sealed clas that extends IColorSpaceStructure.

I suppose that if that is the way MS does things that is the way things are. Guess it doesn't really make sense to me as
to why they need to do things that way.


It complains about not being able to instantiate ColorSpace even though I never try to create an object of that type.
Well, directly anyway.


Steve




using System;
using System.Drawing;
using System.Windows.Forms;
using Tools.ColorModel;

namespace NewControls
{
public class CCDebug : ColorSpace
{

internal override IColorSpaceStructure Structure
{
get {return new RGB(100, 200, 240);}
set {RGB rgb = (RGB)value;}
}

public CCDebug()
{
}/* End of CCDebug() */


public override void SetDefaultSelection()
{

} /* End of SetDefaultSelection() */

public override Color GetColor()
{
return(Color.Black);

} /* End of GetColor() */


protected override void UpdateValues(IColorSpaceStructure csStructure)
{
RGB rgb = ( RGB ) csStructure;
} /* End of UpdateValues(IColorSpaceStructure) */


} /* End of internal class CCDebug */

} /* End of namespace NewControls */




using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using Tools.ColorModel;

namespace NewControls
{

/// <summary>
/// Abstract base class for the color spaces used in the application.
/// </summary>

public abstract class ColorSpace : UserControl
// public class ColorSpace : UserControl
{

// events
internal event ColorSpaceEventHandler ComponentValueChanged;
internal event ColorSpaceEventHandler SelectedComponentChanged;

// member fields
private ArrayList m_colorSpaceComponents;
private ColorSpaceComponent m_selectedComponent;
private bool m_displayRadioButtons;
private bool m_readOnly;

/// <summary>
/// Gets an ArrayList containing the components contained by this color
/// space.
/// </summary>

protected ArrayList ColorSpaceComponents
{
get
{
return m_colorSpaceComponents;
}

} /* End of ColorSpaceComponents */



/// <summary>
/// Gets the currently selected color space component.
/// </summary>

internal ColorSpaceComponent SelectedComponent
{
get
{
return m_selectedComponent;
}
}

/// <summary>
/// Gets or sets a value that indicates whether or not the radio
/// buttons in the color space components are displayed.
/// </summary>

internal bool DisplayRadioButtons
{
get
{
return m_displayRadioButtons;
}
set
{
m_displayRadioButtons = value;

foreach(ColorSpaceComponent csc in m_colorSpaceComponents)
csc.RadioButtonVisible = m_displayRadioButtons;
}
}


/// <summary>
/// Gets or sets a value that indicates whether or not the contents of
/// the ColorSpaceComponent can be changed.
/// </summary>

internal bool ReadOnly
{
get
{
return m_readOnly;
}
set
{
m_readOnly = value;

foreach (ColorSpaceComponent csc in m_colorSpaceComponents)
csc.ReadOnly = m_readOnly;
}

}

/// <summary>
/// Constructor.
/// </summary>

protected ColorSpace() : base()
{
m_colorSpaceComponents = new ArrayList();
m_displayRadioButtons = true;

} /* End of ColorSpace() */


/// <summary>
/// Handles the ComponentSelected event that the ColorSpaceComponent
/// raises.
/// </summary>
/// <param name="sender">The ColorSpaceComponent that raised the event.</param>
/// <param name="e">An EventArgs containing the event data.</param>

protected void ComponentSelected(ColorSpaceComponent sender, EventArgs e)
{
ChangeCurrentlySelectedComponent(sender);

} /* End of ComponentSelected(ColorSpaceComponent, EventArgs) */


/// <summary>
/// Changes the currently selected color space component.
/// </summary>
/// <param name="component">A ColorSpaceComponent representing the
/// component that is to be set as the selected component.</param>

protected void ChangeCurrentlySelectedComponent(ColorSpaceComponent component)
{
// make sure none of the ColorSpaceComponents are checked.
ResetComponents();

component.Selected = true;
m_selectedComponent = component;

OnSelectedComponentChanged( EventArgs.Empty );

} /* End of ChangeCurrentlySelectedComponent(ColorSpaceComponent) */


/// <summary>
/// Handles the ComponentTextKeyUp event that the ColorSpaceComponent
/// raises.
/// </summary>
/// <param name="sender">The ColorSpaceComponent that raised the event.</param>
/// <param name="e">An EventArgs containing the event data.</param>

protected void ComponentTextKeyUp(ColorSpaceComponent sender, EventArgs e)
{
OnComponentValueChanged( e );
}


/// <summary>
/// Sets the selected property of the color space components to false.
/// </summary>

internal void ResetComponents()
{
foreach(ColorSpaceComponent csc in m_colorSpaceComponents)
csc.Selected = false;
}


/// <summary>
/// Raises the SelectedComponentChanged event.
/// </summary>
/// <param name="e">An EventArgs containing event data.</param>

protected void OnSelectedComponentChanged(EventArgs e)
{
if (SelectedComponentChanged != null)
SelectedComponentChanged( this, e );
}



/// <summary>
/// Raises the ComponentValueChanged event.
/// </summary>
/// <param name="e">An EventArgs containing event data.</param>

protected void OnComponentValueChanged(EventArgs e)
{
if (ComponentValueChanged != null)
ComponentValueChanged( this, e );

} /* End of OnComponentValueChanged(EventArgs) */


/// <summary>
/// Sets the default selected component.
/// </summary>

// public virtual void SetDefaultSelection() {}
public abstract void SetDefaultSelection();

/// <summary>
/// Returns a Color object representing the color that the color
/// space's coordinates represent.
/// </summary>

public abstract Color GetColor();
// public virtual Color GetColor() {return Color.Black;}

/// <summary>
/// Updates the color space coordinate values.
/// </summary>
/// <param name="csStructure">A IColorSpaceStructure object containing
/// the coordinate values that the color space is to be updated with.</param>

// protected virtual void UpdateValues( IColorSpaceStructure csStructure ) {}
protected abstract void UpdateValues( IColorSpaceStructure csStructure );

private void InitializeComponent()
{
//
// ColorSpace
//
this.Name = "ColorSpace";
this.Load += new System.EventHandler(this.ColorSpace_Load);

} /* End of InitializeComponent() */


private void ColorSpace_Load(object sender, System.EventArgs e)
{

}

/// <summary>
/// Gets or sets the IColorSpace based object that contains the
/// coordinate values of the components in the color space.
/// </summary>

internal abstract IColorSpaceStructure Structure { get; set; }
// internal virtual IColorSpaceStructure Structure { get{return new RGB(100, 200, 240);} set{} }

} /* End of public abstract class ColorSpace */

} /* End of namespace NewControls */



using System;

namespace Tools.ColorModel
{

public interface IColorSpaceStructure {}

} /* End of namespace Tools.ColorModel */







using System;

namespace Tools.ColorModel
{

public sealed class RGB : IColorSpaceStructure
{
private int m_red;
private int m_green;
private int m_blue;

internal int Red
{
get
{
return m_red;
}
set
{
m_red = value;
}
}

internal int Green
{
get
{
return m_green;
}
set
{
m_green = value;
}
}

internal int Blue
{
get
{
return m_blue;
}
set
{
m_blue = value;
}
}

internal RGB(int red, int green, int blue)
{
m_red = red;
m_green = green;
m_blue = blue;

} /* End of RGB(int, int, int) */


public override string ToString()
{
return String.Format( "Red: {0}; Green: {1}; Blue: {2} ", Red, Green, Blue );
}


public override bool Equals(object obj)
{
bool equal = false;

if (obj is RGB)
{
RGB rgb = ( RGB ) obj;

if (this.Red == rgb.Red && this.Blue == rgb.Blue && this.Green == rgb.Green)
{
equal = true;
}

}

return equal;

} /* End of Equals(object) */


public override int GetHashCode()
{
return base.GetHashCode ();
}


} /* End of public sealed class RGB */

} /* End of namespace Tools.ColorModel
*/
 
As i said.
the designer uses an instance of baseclass of the control wich is designed.
So in the designer you can construct an abstract class but you can't derive
from one.
That's how the formsdesigner works.
 
Back
Top