Jump to content

Separating Logic & Presentation

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
Howdy,

I'm wanting to learn C#, so I figured I'd write an application that would do something useful for me. So, I came up with a program I want to write, and I'm in the process of writing out how the logic for it should go. When I do start doing this I want my logic to be separate from my GUI. However, I don't know how to do that. I'm using VS 2010 Pro, and I'm not super familiar with it (or C# for that matter).

For starters, would you recommend just using winforms for this, or maybe use wpf?

For now, I've started a win forms project. I created two folders in that project.. logic & presentation.

Now, this part I wasn't sure about. In the logic folder I added a class file. Then moved the form.cs file that was created when I made the win forms project to the Presentation folder. I left the program.cs in the root. So far so good?

My question is once I start programming.. how do I link one to the other? Since both files share the same name space is it as simple as just calling methods from the logic as I need to more or less?
Posted Image

#2
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
Let's say you're making a calculator that adds two numbers. To properly separate logic from ui, you would of course create your form with two text boxes and a button. Create another class that has a method that handles the addition. e.g.

public Class Calculator 
{
    public double AddNumbers(double one, double two)
    {
        return 1 + 2;
    }

}

On your form, in the event delegate for your button, you'd do something like:

Calculator calc = new Calculator();
MessageBox.Show(calc.addNumbers(Double.Parse(textBox1.Text), Double.Parse(textBox2.Text);

Your form calls the methods of aanother class to do work, instead of doing the work itself.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users