Tag Archives: property

C#: Abstract Class

If you are like me and you don’t like to repeat code in your project, I am going to talk about reusing code by creating an abstract class using c#.

What is an abstract class?

Abstract class is a class that must be declared with the keyword abstract and it can not be instantiated and must be inherited by another class. The abstract class can have abstract methods, abstract properties and virtual methods.

public abstract class MyAbstractClass {
   public abstract string MyProperty { get; set; }
   public abstract void MyAbstractMethod();
   public void MyMethod() {
     Console.WriteLine("Hello World!");
   }
   public virtual void MyVirtualMethod() {
     Console.WriteLine("This method can be overridden.");
   }
}

Why do we use abstract class?

We can use abstract class to implement a common method which will contain reusable code, which then the class can be inherited. For example you I can create a simple abstract class with a method that implements code to print a message  to a console and protected variable to hold a string message:

public abstract class DisplayMessage {
   protected string message;
   public void Show() {
     Console.WriteLine(message);
   } 
}

Now I will create a two classes that inherits my abstract class and define a two different text message inside their constructors:

public class MyClass1 : DisplayMessage {
   public MyClass1() {
     message = "This is my class 1";
   }
}

public class MyClass2 : DisplayMessage {
   public MyClass2() {
     message = "This is my class 2";
   }

Now in the main method I am going to instantiate the two classes and call the Show() method to display each of the message on the console.

void Main() {
   MyClass1 myClass1 = new MyClass1();
   myClass1.Show();

   MyClass2 myClass2 = new MyClass2();
   myClass2.Show();
}

What is a Virtual Method?

A virtual method can contain implementation and must be declared with the keyword virtual. You can override a virtual method from the inherited class to implement code that is completely different from the one in the abstract class. To demonstrate this I will use the DisplayMessage abstract class and modify the Show() to a virtual method:

public abstract class DisplayMessage {
   protected string message;
   public virtual void Show() {
     Console.WriteLine(message);
   } 
}

Now I will create two classes that inherits the DisplayMessage class, but one of the class will override the Show() method to display the text message four times:

public class MyClass1 : DisplayMessage {
   public MyClass1() {
     message = "This is my class 1";
   }
}

public class MyClass2 : DisplayMessage {
   public MyClass2() {
     message = "This is my class 2";
   }
   public override void Show() {
      for(int idx = 0; idx < 5; idx++)
      {
         Console.WriteLine(message);
      }
   }
}

Summary

Abstract class are very useful for implementing reusable code that uses a common method that will be inherited by other classes. Abstract methods and properties has no implementations but can be overridden by the inherited class. Virtual methods can have implementation and can be overridden by the inherited class. 

Remember not duplicate code in your project, reuse code by implementing appropriate design patterns.

 

 

How to access a common property from multiple form controls.

Recently I had someone posting a question about how to access the Text properties of a different control. I thought of using reflection, because you don’t need to know the type of control you are handling and you don’t need to type cast the object.

If you are handling one specific control, there is no point using reflection, I would suggest just type cast it.

Below is a code snippet from an example I have written to demonstrate how to share one event method with multiple controls on a form. In the method it will get the string value from the Text property of the selected control.

Source Code: ReflectionExample1.zip

<span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.ComponentModel;
<span style="color: blue">using </span>System.Data;
<span style="color: blue">using </span>System.Drawing;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Text;
<span style="color: blue">using </span>System.Windows.Forms;
<span style="color: blue">using </span>System.Reflection;

<span style="color: blue">namespace </span>ReflectionExample1
{
    <span style="color: blue">public partial class </span><span style="color: #2b91af">Form1 </span>: <span style="color: #2b91af">Form
    </span>{
        <span style="color: blue">public </span>Form1()
        {
            InitializeComponent();
        }

        <span style="color: blue">private void </span>DisplayText(<span style="color: blue">object </span>sender, <span style="color: #2b91af">MouseEventArgs </span>e)
        {
            <span style="color: #2b91af">Type </span>type = sender.GetType();
            <span style="color: #2b91af">PropertyInfo </span>pInfo = type.GetProperty(<span style="color: #a31515">"Text"</span>);
            <span style="color: #2b91af">String </span>value = pInfo.GetValue(sender, <span style="color: blue">null</span>).ToString();
            <span style="color: #2b91af">MessageBox</span>.Show(value);
        }
    }
}