C# - whether Abstract class implementation is correct











up vote
0
down vote

favorite












I'm currently reading on how to implement design patterns. I'm trying to implement the design patterns but need help on whether my understanding is correct.



I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



For each component I have created a class. Each have their own responsibility.



After reading about abstraction, The following is my understanding




An abstract class is a partially defined class that cannot be
instantiated.



The purpose of an abstract class is to define some
common behavior that can be inherited by multiple subclasses.



An abstract class should be used when there is IS-A relationship.



Abstract classes are also useful in the case of modifications to the
project. If you plan on updating the base class in your project, it is
better to make the class abstract. Because you can define a
functionality in an abstract base class and automatically all the
inheriting classes will have the same functionality without disturbing
the hierarchy.




Now coming to my code, After reading about abstraction I tried to implement abstract class I thought below is the scenario to implement abstract class.




Here I need to create multiple class where all the class will have
the same behaviour. So I thought of creating a Component abstract
class.



All the subclass which is also a form of component will have a is-a relationship with the main class.



On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement
the add_dynamic_components methods.




   // abstract class
public abstract class add_components
{
public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
}


The classes that will inherit the above abstract classes are below. This classes are example I have nearly 8 classes which inherit the abstract class.



        //textbox class which will get no of textbox to be created
public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
txtBox.TabIndex = currentTabIndex * 7 + 2;
}
}

//Richtextbox class which will get no of richtextbox to be created and its position
public class addDynamicSpellboxComponents : add_components
{

public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
{
RichTextBox sp = new RichTextBox();
f.panel1.Controls.Add(sp);
sp.Location = new Point(spPointX, spPointY);
sp.Size = new System.Drawing.Size(230, 43);
f.panel1.Controls.Add(sp);
sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
sp.TabIndex = currentTabIndex * 7 + 3;

}
}

//combobox class which will get no of combobox to be created and its position
public class addDynamicComboboxxComponents : add_components
{

public override void add_dynamic_components(int getNoOfComboboxValue, int cmbPointX, int cmbPointY, Form1 f, int currentTabIndex )
{
ComboBox cmb = new ComboBox();
f.panel1.Controls.Add(cmb);
cmb.Location = new Point(cmbPointX, cmbPointY);
cmb.Size = new System.Drawing.Size(95, 23);
cmb.Name = "Add_combobox" + getNoOfComboboxValue;
addItems(cmb);
cmb.DropDownStyle = ComboBoxStyle.DropDownList;

cmb.SelectedIndexChanged += new EventHandler(f.cmbBoxValueChange);
cmb.TabIndex = currentTabIndex * 7 + 4;
}


So now my questions are



1) Whether my thought process on abstraction is correct?



2)Even though I have created abstract class on implementing I'm just calling the subclass instances to call the method since abstract class cannot be instantiated. Here am I missing something. The reason for this question is , whether we only use abstract class for better reading purpose or there is any special classes where it will be useful. Most of the examples in the internet says the abstract helps in re-usability.



3)This is how i implemented the class in form, Here I don't think I'm leveraging the abstract concept. On implementing how to leverage abstract concept.



public partial class Form1 : Form
{
addDynamicComboboxxComponents addcomboboxcomponent = new addDynamicComboboxxComponents();
addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
public Form1()
{
try
{
InitializeComponent();
}
catch (Exception ex)
{
//Write ex.Message to a file
using (StreamWriter outfile = new StreamWriter(@".error.txt"))
{
outfile.Write(ex.Message.ToString());
}
}
initalizeUserdefinedComponents();

}

Public void initalizeUserdefinedComponents()
{
addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);
addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
addcomboboxcomponent.add_dynamic_components(i, cmbPointX, cmbPointY, this, i);
}
}


4) I have a feeling that I'm repeating the same thing by only changing the component i.e Textbox , combobox etc. Is there any way to refactor it.



Please feel free to add some suggestion so that i can improve in design patterns.









share


























    up vote
    0
    down vote

    favorite












    I'm currently reading on how to implement design patterns. I'm trying to implement the design patterns but need help on whether my understanding is correct.



    I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



    For each component I have created a class. Each have their own responsibility.



    After reading about abstraction, The following is my understanding




    An abstract class is a partially defined class that cannot be
    instantiated.



    The purpose of an abstract class is to define some
    common behavior that can be inherited by multiple subclasses.



    An abstract class should be used when there is IS-A relationship.



    Abstract classes are also useful in the case of modifications to the
    project. If you plan on updating the base class in your project, it is
    better to make the class abstract. Because you can define a
    functionality in an abstract base class and automatically all the
    inheriting classes will have the same functionality without disturbing
    the hierarchy.




    Now coming to my code, After reading about abstraction I tried to implement abstract class I thought below is the scenario to implement abstract class.




    Here I need to create multiple class where all the class will have
    the same behaviour. So I thought of creating a Component abstract
    class.



    All the subclass which is also a form of component will have a is-a relationship with the main class.



    On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement
    the add_dynamic_components methods.




       // abstract class
    public abstract class add_components
    {
    public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
    }


    The classes that will inherit the above abstract classes are below. This classes are example I have nearly 8 classes which inherit the abstract class.



            //textbox class which will get no of textbox to be created
    public class addDynamicCptboxComponents : add_components
    {
    public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
    {
    TextBox txtBox = new TextBox();
    f.panel1.Controls.Add(txtBox);
    txtBox.Location = new Point(pointX, pointY);
    txtBox.Size = new System.Drawing.Size(75, 23);
    f.panel1.Controls.Add(txtBox);
    txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
    txtBox.TabIndex = currentTabIndex * 7 + 2;
    }
    }

    //Richtextbox class which will get no of richtextbox to be created and its position
    public class addDynamicSpellboxComponents : add_components
    {

    public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
    {
    RichTextBox sp = new RichTextBox();
    f.panel1.Controls.Add(sp);
    sp.Location = new Point(spPointX, spPointY);
    sp.Size = new System.Drawing.Size(230, 43);
    f.panel1.Controls.Add(sp);
    sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
    sp.TabIndex = currentTabIndex * 7 + 3;

    }
    }

    //combobox class which will get no of combobox to be created and its position
    public class addDynamicComboboxxComponents : add_components
    {

    public override void add_dynamic_components(int getNoOfComboboxValue, int cmbPointX, int cmbPointY, Form1 f, int currentTabIndex )
    {
    ComboBox cmb = new ComboBox();
    f.panel1.Controls.Add(cmb);
    cmb.Location = new Point(cmbPointX, cmbPointY);
    cmb.Size = new System.Drawing.Size(95, 23);
    cmb.Name = "Add_combobox" + getNoOfComboboxValue;
    addItems(cmb);
    cmb.DropDownStyle = ComboBoxStyle.DropDownList;

    cmb.SelectedIndexChanged += new EventHandler(f.cmbBoxValueChange);
    cmb.TabIndex = currentTabIndex * 7 + 4;
    }


    So now my questions are



    1) Whether my thought process on abstraction is correct?



    2)Even though I have created abstract class on implementing I'm just calling the subclass instances to call the method since abstract class cannot be instantiated. Here am I missing something. The reason for this question is , whether we only use abstract class for better reading purpose or there is any special classes where it will be useful. Most of the examples in the internet says the abstract helps in re-usability.



    3)This is how i implemented the class in form, Here I don't think I'm leveraging the abstract concept. On implementing how to leverage abstract concept.



    public partial class Form1 : Form
    {
    addDynamicComboboxxComponents addcomboboxcomponent = new addDynamicComboboxxComponents();
    addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
    addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
    public Form1()
    {
    try
    {
    InitializeComponent();
    }
    catch (Exception ex)
    {
    //Write ex.Message to a file
    using (StreamWriter outfile = new StreamWriter(@".error.txt"))
    {
    outfile.Write(ex.Message.ToString());
    }
    }
    initalizeUserdefinedComponents();

    }

    Public void initalizeUserdefinedComponents()
    {
    addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);
    addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
    addcomboboxcomponent.add_dynamic_components(i, cmbPointX, cmbPointY, this, i);
    }
    }


    4) I have a feeling that I'm repeating the same thing by only changing the component i.e Textbox , combobox etc. Is there any way to refactor it.



    Please feel free to add some suggestion so that i can improve in design patterns.









    share
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm currently reading on how to implement design patterns. I'm trying to implement the design patterns but need help on whether my understanding is correct.



      I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



      For each component I have created a class. Each have their own responsibility.



      After reading about abstraction, The following is my understanding




      An abstract class is a partially defined class that cannot be
      instantiated.



      The purpose of an abstract class is to define some
      common behavior that can be inherited by multiple subclasses.



      An abstract class should be used when there is IS-A relationship.



      Abstract classes are also useful in the case of modifications to the
      project. If you plan on updating the base class in your project, it is
      better to make the class abstract. Because you can define a
      functionality in an abstract base class and automatically all the
      inheriting classes will have the same functionality without disturbing
      the hierarchy.




      Now coming to my code, After reading about abstraction I tried to implement abstract class I thought below is the scenario to implement abstract class.




      Here I need to create multiple class where all the class will have
      the same behaviour. So I thought of creating a Component abstract
      class.



      All the subclass which is also a form of component will have a is-a relationship with the main class.



      On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement
      the add_dynamic_components methods.




         // abstract class
      public abstract class add_components
      {
      public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
      }


      The classes that will inherit the above abstract classes are below. This classes are example I have nearly 8 classes which inherit the abstract class.



              //textbox class which will get no of textbox to be created
      public class addDynamicCptboxComponents : add_components
      {
      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 2;
      }
      }

      //Richtextbox class which will get no of richtextbox to be created and its position
      public class addDynamicSpellboxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
      {
      RichTextBox sp = new RichTextBox();
      f.panel1.Controls.Add(sp);
      sp.Location = new Point(spPointX, spPointY);
      sp.Size = new System.Drawing.Size(230, 43);
      f.panel1.Controls.Add(sp);
      sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
      sp.TabIndex = currentTabIndex * 7 + 3;

      }
      }

      //combobox class which will get no of combobox to be created and its position
      public class addDynamicComboboxxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfComboboxValue, int cmbPointX, int cmbPointY, Form1 f, int currentTabIndex )
      {
      ComboBox cmb = new ComboBox();
      f.panel1.Controls.Add(cmb);
      cmb.Location = new Point(cmbPointX, cmbPointY);
      cmb.Size = new System.Drawing.Size(95, 23);
      cmb.Name = "Add_combobox" + getNoOfComboboxValue;
      addItems(cmb);
      cmb.DropDownStyle = ComboBoxStyle.DropDownList;

      cmb.SelectedIndexChanged += new EventHandler(f.cmbBoxValueChange);
      cmb.TabIndex = currentTabIndex * 7 + 4;
      }


      So now my questions are



      1) Whether my thought process on abstraction is correct?



      2)Even though I have created abstract class on implementing I'm just calling the subclass instances to call the method since abstract class cannot be instantiated. Here am I missing something. The reason for this question is , whether we only use abstract class for better reading purpose or there is any special classes where it will be useful. Most of the examples in the internet says the abstract helps in re-usability.



      3)This is how i implemented the class in form, Here I don't think I'm leveraging the abstract concept. On implementing how to leverage abstract concept.



      public partial class Form1 : Form
      {
      addDynamicComboboxxComponents addcomboboxcomponent = new addDynamicComboboxxComponents();
      addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
      addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
      public Form1()
      {
      try
      {
      InitializeComponent();
      }
      catch (Exception ex)
      {
      //Write ex.Message to a file
      using (StreamWriter outfile = new StreamWriter(@".error.txt"))
      {
      outfile.Write(ex.Message.ToString());
      }
      }
      initalizeUserdefinedComponents();

      }

      Public void initalizeUserdefinedComponents()
      {
      addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);
      addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
      addcomboboxcomponent.add_dynamic_components(i, cmbPointX, cmbPointY, this, i);
      }
      }


      4) I have a feeling that I'm repeating the same thing by only changing the component i.e Textbox , combobox etc. Is there any way to refactor it.



      Please feel free to add some suggestion so that i can improve in design patterns.









      share













      I'm currently reading on how to implement design patterns. I'm trying to implement the design patterns but need help on whether my understanding is correct.



      I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



      For each component I have created a class. Each have their own responsibility.



      After reading about abstraction, The following is my understanding




      An abstract class is a partially defined class that cannot be
      instantiated.



      The purpose of an abstract class is to define some
      common behavior that can be inherited by multiple subclasses.



      An abstract class should be used when there is IS-A relationship.



      Abstract classes are also useful in the case of modifications to the
      project. If you plan on updating the base class in your project, it is
      better to make the class abstract. Because you can define a
      functionality in an abstract base class and automatically all the
      inheriting classes will have the same functionality without disturbing
      the hierarchy.




      Now coming to my code, After reading about abstraction I tried to implement abstract class I thought below is the scenario to implement abstract class.




      Here I need to create multiple class where all the class will have
      the same behaviour. So I thought of creating a Component abstract
      class.



      All the subclass which is also a form of component will have a is-a relationship with the main class.



      On whenever I create a new dynamic component I can just inherit the add_components class so that the new class will implement
      the add_dynamic_components methods.




         // abstract class
      public abstract class add_components
      {
      public abstract void add_dynamic_components(int noOfComponets, int locationX, int locationY, Form1 l, int currentTabIndex);
      }


      The classes that will inherit the above abstract classes are below. This classes are example I have nearly 8 classes which inherit the abstract class.



              //textbox class which will get no of textbox to be created
      public class addDynamicCptboxComponents : add_components
      {
      public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f, int currentTabIndex)
      {
      TextBox txtBox = new TextBox();
      f.panel1.Controls.Add(txtBox);
      txtBox.Location = new Point(pointX, pointY);
      txtBox.Size = new System.Drawing.Size(75, 23);
      f.panel1.Controls.Add(txtBox);
      txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
      txtBox.TabIndex = currentTabIndex * 7 + 2;
      }
      }

      //Richtextbox class which will get no of richtextbox to be created and its position
      public class addDynamicSpellboxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfSpellTxtBox, int spPointX, int spPointY, Form1 f, int currentTabIndex)
      {
      RichTextBox sp = new RichTextBox();
      f.panel1.Controls.Add(sp);
      sp.Location = new Point(spPointX, spPointY);
      sp.Size = new System.Drawing.Size(230, 43);
      f.panel1.Controls.Add(sp);
      sp.Name = "RichTextbox" + getNoOfSpellTxtBox;
      sp.TabIndex = currentTabIndex * 7 + 3;

      }
      }

      //combobox class which will get no of combobox to be created and its position
      public class addDynamicComboboxxComponents : add_components
      {

      public override void add_dynamic_components(int getNoOfComboboxValue, int cmbPointX, int cmbPointY, Form1 f, int currentTabIndex )
      {
      ComboBox cmb = new ComboBox();
      f.panel1.Controls.Add(cmb);
      cmb.Location = new Point(cmbPointX, cmbPointY);
      cmb.Size = new System.Drawing.Size(95, 23);
      cmb.Name = "Add_combobox" + getNoOfComboboxValue;
      addItems(cmb);
      cmb.DropDownStyle = ComboBoxStyle.DropDownList;

      cmb.SelectedIndexChanged += new EventHandler(f.cmbBoxValueChange);
      cmb.TabIndex = currentTabIndex * 7 + 4;
      }


      So now my questions are



      1) Whether my thought process on abstraction is correct?



      2)Even though I have created abstract class on implementing I'm just calling the subclass instances to call the method since abstract class cannot be instantiated. Here am I missing something. The reason for this question is , whether we only use abstract class for better reading purpose or there is any special classes where it will be useful. Most of the examples in the internet says the abstract helps in re-usability.



      3)This is how i implemented the class in form, Here I don't think I'm leveraging the abstract concept. On implementing how to leverage abstract concept.



      public partial class Form1 : Form
      {
      addDynamicComboboxxComponents addcomboboxcomponent = new addDynamicComboboxxComponents();
      addDynamicCptboxComponents addcomponent = new addDynamicCptboxComponents();
      addDynamicSpellboxComponents addSpellboxComponent = new addDynamicSpellboxComponents();
      public Form1()
      {
      try
      {
      InitializeComponent();
      }
      catch (Exception ex)
      {
      //Write ex.Message to a file
      using (StreamWriter outfile = new StreamWriter(@".error.txt"))
      {
      outfile.Write(ex.Message.ToString());
      }
      }
      initalizeUserdefinedComponents();

      }

      Public void initalizeUserdefinedComponents()
      {
      addSpellboxComponent.add_dynamic_components(i, spPointX, spPointY, this, i);
      addcomponent.add_dynamic_components(i, pointX, pointY, this, i);
      addcomboboxcomponent.add_dynamic_components(i, cmbPointX, cmbPointY, this, i);
      }
      }


      4) I have a feeling that I'm repeating the same thing by only changing the component i.e Textbox , combobox etc. Is there any way to refactor it.



      Please feel free to add some suggestion so that i can improve in design patterns.







      c# object-oriented





      share












      share










      share



      share










      asked 9 mins ago









      Vini

      183




      183



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209044%2fc-whether-abstract-class-implementation-is-correct%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209044%2fc-whether-abstract-class-implementation-is-correct%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin