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.
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:
I just don't know how to connect these two.
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 ...
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? 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.
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.
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.
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;
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With