Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows Forms App: Separate GUI from Business Logic

I would like some advice on how to separate the UI and business logic in a simple C# Windows Forms Application.

Let's take this example:

The UI consists of a simple textbox and a button. The user enters a number between 0 and 9 and clicks the button. The program should add 10 to the number and update the text box with that value.

enter image description here

The business logic part should have no idea of the UI. How can this be accomplished?

Here's the empty Process class (Business Logic):

namespace addTen
{
    class Process
    {
        public int AddTen(int num)
        {
            return num + 10;
        }
    }
}

The requirement is:

  1. When the user clicks the button, somehow, the Process::AddTen gets invoked.
  2. The Textbox must be updated with the return value of Process::AddTen.

I just don't know how to connect these two.

like image 256
Happy Go Lucky Avatar asked Jul 18 '12 17:07

Happy Go Lucky


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

First, you need to change your class name. "Process" is name of a class in the Class Library and will likely cause confusion for anyone reading your code.

Let's assume, for the rest of this answer that you changed the class name to MyProcessor (still a bad name, but not a well-known, often-used class.)

Also, you're missing the code to check to ensure that the user input is, indeed, a number between 0 and 9. That's appropriate in the Form's code rather than the class code.

  • Assuming the TextBox is named textBox1 (The VS generated default for the first TextBox added to the form)
  • Further assuming the button's name is button1

In Visual Studio, double-click on the button to create the button click event handler, which will look like this:

protected void button1_Click(object sender, EventArgs e)
{

}

Within the event handler, add code so it looks like this:

 protected void button1_Click(object sender, EventArgs e)
 {
   int safelyConvertedValue = -1;
   if(!System.Int32.TryParse(textBox1.Text, out safelyConvertedValue))
   {
     // The input is not a valid Integer value at all.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   // If you made it this far, the TryParse function should have set the value of the 
   // the variable named safelyConvertedValue to the value entered in the TextBox.
   // However, it may still be out of the allowable range of 0-9)
   if(safelyConvertedValue < 0 || safelyConvertedValue > 9)
   {
     // The input is not within the specified range.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   MyProcessor p = new MyProcessor();
   textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
 }

The class, with the access modifier set properly, should look like this:

namespace addTen       
{       
    public class MyProcessor
    {       
        public int AddTen(int num)       
        {       
            return num + 10;       
        }       
    }       
}    
like image 108
David Avatar answered Sep 19 '22 17:09

David


You can create another class called "Process.cs" for example. Methods that involve processing or data calculation you move there. In your case for example:

public class Process
{
 public int AddTen(int num)
 {
   return num + 10;
 }
}

Your UI click event will have a call to your "Process layer":

 var myProcess = new Process();
  //and then calculation
  var initNumber = Convert.ToInt32(textBox.Text);
  var calculatedValue = myProcess.AddTen(initNumber);

  textBox.Text = calculatedValue.ToString();

This way your business logic, such as calculating is kept separately. If your UI changes you can still simply call myProcess.AddTen() method whether it's a web, Windows or a Mobile form.

like image 20
InspiredBy Avatar answered Sep 22 '22 17:09

InspiredBy