C# Windows Forms App: Separate GUI from Business Logic
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;
}
}
}
Make your 'Process' class public (and as @DavidStratton says, change the name):
public class MyProcess
I would say you should parse your string
value from TextBox.Text
to an int
:
private void button1_Click(object sender, EventArgs e)
{
MyProcess myProcess = new MyProcess();
string result = textBox1.Text;
int number;
if(int.TryParse(textBox1.Text, out number))
{
result = myProcess.AddTen(number).ToString();
}
textBox1.Text = result;
}
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.