473,378 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to extend controls with base class?

Franna Tech
Hi

I have a BaseClass

Expand|Select|Wrap|Line Numbers
  1. public class BaseClass
  2. {
  3.     [XmlIgnore]
  4.     public PropertyGrid PropertyGridControl
  5.     [XmlAttribute("Name")]
  6.     public string Name
  7.     { get; set; }
  8. }
Then I have multiple classes that I want to derive from controls and inherit / implement the BaseClass.

Expand|Select|Wrap|Line Numbers
  1. public class MyLabel : Label
  2. {   
  3. }
  4.  
  5. public class MyPanel : Panel
  6. {
  7. }
There is no multiple inheritance in c#, so I have to derive form the control itself, but how do I then extend my class to implement the BaseClass.

I need to be able to manage common properties of different controls for xml serialization purposes.

What would be the best way to do this?

I have thought of creating a BaseClass like
Expand|Select|Wrap|Line Numbers
  1. public class BaseClass
  2. {
  3.     [XmlIgnore]
  4.     public PropertyGrid PropertyGridControl
  5.     [XmlAttribute("Name")]
  6.     public string Name
  7.     { get; set; }
  8.  
  9.     [XmlIgnore]
  10.     protected Control ControlObj;
  11. }
  12.  
and then inherit from that and create a new control like
Expand|Select|Wrap|Line Numbers
  1. public class MyLabel : BaseClass
  2. {   
  3.     public MyLabel() : base()
  4.     {
  5.         ControlObj = new Label();
  6.     }
  7. }
  8.  
  9. public class MyPanel : BaseClass
  10. {   
  11.     public MyPanel() : base()
  12.     {
  13.         ControlObj = new Panel();
  14.     }
  15. }
But this way is creating lots of work downstream.

Is there a better way to implement this scenario?

Thanks
Franna
Feb 27 '11 #1
2 3252
Have you looked at interfaces? maybe a possibility there?
Feb 27 '11 #2
Hi Bryan

Thanks, yes, I have created an interface and implemented this in my subclasses with an Extension class as follows:

Expand|Select|Wrap|Line Numbers
  1. public interface IDesignBaseControl
  2. {
  3.  
  4.         void Initialize();
  5.  
  6.         string Name { get; set; }
  7. }
  8.  
  9. public static class DesignBaseControlExtension
  10. {
  11.         public static void InitializeBase(this IDesignBaseControl bse)
  12.         {
  13.             // TODO: Base Initialization
  14.             bse.BackColor = Color.Beige;
  15.         }
  16.  
  17.         public static void ReadXml(this IDesignBaseControl bse, System.Xml.XmlReader reader)
  18.         {
  19.             throw new NotImplementedException();
  20.         }
  21.  
  22.         public static void WriteXml(this IDesignBaseControl bse, System.Xml.XmlWriter writer)
  23.         {
  24.             writer.WriteAttributeString("Name", bse.Name);
  25.         }
  26.  
  27.         public static System.Xml.Schema.XmlSchema GetSchema(this IDesignBaseControl bse)
  28.         {
  29.             return null;
  30.         }
  31. }
  32.  
  33. public class DesignLabel : Label, IDesignBaseControl, IXmlSerializable
  34. {
  35.         public DesignLabel()
  36.             : base()
  37.         {
  38.             Initialize();
  39.         }
  40.  
  41.         public void Initialize()
  42.         {
  43.             this.InitializeBase();
  44.         }
  45.  
  46.         public void ReadXml(System.Xml.XmlReader reader)
  47.         {
  48.             throw new NotImplementedException();
  49.         }
  50.  
  51.         public void WriteXml(System.Xml.XmlWriter writer)
  52.         {
  53.             DesignBaseControlExtension.WriteXml(this, writer);
  54.         }
  55. }
  56.  
  57. public class DesignPanel : Panel, IDesignBaseControl, IXmlSerializable
  58. {
  59.         public DesignPanel()
  60.             : base()
  61.         {
  62.             Initialize();
  63.         }
  64.  
  65.         public void Initialize()
  66.         {
  67.             this.InitializeBase();
  68.         }
  69.  
  70.         public void ReadXml(System.Xml.XmlReader reader)
  71.         {
  72.             throw new NotImplementedException();
  73.         }
  74.  
  75.         public void WriteXml(System.Xml.XmlWriter writer)
  76.         {
  77.             DesignBaseControlExtension.WriteXml(this, writer);
  78.         }
  79. }
I'm not sure if this is industry standard, but it works.

Franna
Feb 28 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
4
by: henrikpierrou | last post by:
Hi, I am trying to extend an overridden base class method (printer) by printing some extra fields and then calling the base class method. Extract from the python tutorial: 'An overriding...
5
by: Allen | last post by:
Hi all, I have a derived class that inherits from two base classes. One of the base class constructors takes as input a pointer that is initialized in the other base class. How can I make sure...
3
by: James | last post by:
Hi, I have two classes, a base clase ItemType and a class which inherits from it called Item. During the runtime of my program, I create an instance of ItemType, then later, I would like to...
5
by: Chris Capon | last post by:
Is there any way to cast a base class object to a derived class datatype when the derived class adds no new fields nor alters the object allocation in any way? The scenario would be where you...
0
by: rjl444 | last post by:
web developer express asp.net 2.0: I need to have 3 user controls. these controls include a textbox and several asp controls (checkbox, button, etc). I would like to have a base class that...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.