Is it a good idea to inherit from standard widgets and set my own default values in C#?

When designing app in Visual Studio (C#), if I know that I will have a certain number of DataGridViews that all have same properties (like width, height, color, some other properties like: disable option to directly edit rows, etc.) is it ok to make my own class ("myDataGridView") that inherits DataGridView class and make all adjustments there, and then just instantiate that class later in code? Like this:

//my class: class myDataGridView : DataGridView { this.BorderStyle = <someValue> this.ColumnCount = <someValue> //etc. public void method1() { //some code... } public void method2() { //some code... } } //instantiate it somewhere: myDataGridView dgv1 = new myDataGridView(); myDataGridView dgv2 = new myDataGridView(); myDataGridView dgv3 = new myDataGridView();

Is that ok regarding OO principles? My friend says that putting code like

this.BorderStyle = <someValue>

in myDataGridView class is bad practice because adjusting properties like that will overrun properties of dataGridView which some other developer may adjusted visually in Visual Studio, if you know what I mean. Does that matter? I mean, if I want to treat my DataGridView as an object then it can have its properties and behavior, right? And having code that adjusts properties of DataGridView in my class is ok, readable, and every other developer who wants to change some properties can change it in myDataGridView class. It that kind of practice bad or wrong? If it is, what is the best practice when you know your app will have many DataGridViews which have same properties/behaviour? Thank you.

-------------Problems Reply------------

In my opinion, this is not the right approach. Inheriting controls just for few property modifications does not make sense. You're not introducing any additional behavior in the control, Rather,try writing some initialization helper methods to do this.

If you are worried that your modifications to the properties get overwritten by a visual setting by the developer you may hide them using [Browsable(false)] attribute.

However, from OOP perspective inheritance is not encouraged unless really necessary. There is a golden rule : "Favor composition over inheritance". As this. __curious_geek said it's not a good idea to inherit controls to set properties this way but it's good practice to always have common base class for your business classes and screens. You may make a function in the base class of all screens to properly adjust the grids for you the way you want. If you make it virtual, you may override it in derived classes to customize the behavior per case as needed.

Category:c# Views:0 Time:2011-03-14

Related post

  • Quickest way to inherit a standard control in WPF? 2011-01-23

    I just want to wrap a standard control with some more additional properties (look stay the same, I don't want to do theming in first stage). Can I just inherit from this standard control instead of UserControl or Control ? In fact I read it is obliga

  • Make child windows inherit parent background color? 2010-06-24

    Is there a way for windows created with WS_CHILD to inherit the parent's background color rather than default to Dialog Color? I'm trying to do this for a tab control, mainly the part that extends due to the tabs not filling up the area. I want this

  • pygtk: What class should my custom widgets inherit from? 2010-09-30

    When making a custom widget in pygtk, what class should it inherit from? I want to be able to put the widget inside other widgets, but I don't want other people to put stuff in mine. Usually I make my widgets inherit from gtk.HBox or gtk.VBox, and th

  • unordered_map (A)constructor, (B) delete allocate in, (C)inherit from 2011-12-25

    My declaration was unordered_map<Comparison,int> Chs when Comparison is a class name. Now I have few questions; A. How can I insert, when the constructor called, some elements (like done with vector) ? e.g. : unordered_map<Comparison,int>

  • Define css class in django Forms 2008-12-30

    Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker = forms.BooleanField(required=False) Is there a way for me to define css classes on each field such that I can then use

  • Why might one also use a blank constructor? 2008-12-31

    I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example: public class Genotype { private boolean bits[]; private i

  • Windows .Net controls - Creating property templates 2009-01-09

    Assume that all text box controls in my .Net Windows application created by dragging a TextBox control onto the Form editor should have the following default properties (some text box instances can override these properties) : Text Align: Centre Resi

  • GWT label widget receiving focus when pressing tab key 2009-01-16

    Current solution: user clicks on label and it is switched with a textarea to allow edit user can leave edit with tab / enter and textarea is switched back to a label The problem is the user has to click on the label with a mouse to get into edit mode

  • How to DRY on CRUD parts of my Rails app? 2009-01-25

    I am writing an app which - similarly to many apps out there - is 90% regular CRUD things and 10% "juice", where we need nasty business logic and more flexibility and customization. Regarding this 90%, I was trying to stick to the DRY principle as mu

  • Method chaining + inheritance don't play well together? 2009-02-15

    Consider: // member data omitted for brevity // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc) struct Widget { Widget& move(Point

  • How to avoid name clashes in javascript widgets 2009-03-09

    I have a javascript widget (a piece of embedded JS and HTML code) that's embedded on other sites. What should I do to make sure it's variable names don't clash with the hosting page variables? I'd like this widget to be "inlined" meaning on the same

  • C++ Templates: Coding error or compiler bug? 2009-05-27

    I'm trying to use templates to get std:list of items, where each item has a pointer to the list which contains it, but I keep hitting a compiler message. Here's a very stripped down version of the code. template <class E> class Item { public: E

  • jQuery UI Calendar displays too large, would like the demo size? 2009-06-18

    So I downloaded a custom themed UI for jQuery and added the calendar control to my site (Example: link text). In the example it shows/displays the size I would like but on my webpage it's about twice the size. why??? I do have a ton of other CSS but

  • What is a good pattern for designing widgets in ASP.NET? 2009-07-21

    There is some desire to add widget-like components to the Default page of our corporate intranet, with things like job search, weather, etc. My original thought was to define the widgets as user controls, with a similar div structure, and use callbac

  • ASP.NET Page inheritance 2009-08-04

    Looking for advice on the net but not getting very far, so I thought I would ask for some advice. I've seen it done, so know what I want to do, but looking I can work out how it was done What I want to do, is allow users to modify the layout of an AS

  • css inheritence - quiz12 2009-09-28

    What will be the text size “Universe” with the below declaration <div style=”font-size:12px;”>World is<div style=”font-size:0.5em;”>VERY small in <div style=”font-size:100%;”>Universe</div></div></div> 12px 6px fon

  • Problem with set related property in designer at first time 2009-10-28

    I Develop a Form Designer, also develop some custom control that inherit from standard control like button. Also, i have some property that related, for example user must select department first, and then select person. and after user select person,

  • toString(), equals(), and hashCode() in an interface 2009-11-11

    So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to use.

  • Sitecore xsl localisation 2009-11-30

    When testing with .. /xsl/sample rendering.xslt ..which is a simple rendering provided out of the box by Sitecore, I notice that, in the loop that outputs the item's children, all children are included, regardless of whether those items have a versio

Copyright (C), All Rights Reserved.

processed in 0.179 (s). 11 q(s)