Question

Using C# .NET 4.0, Visual Studio 2010.

Well at the moment I'm looking into de-coupling of Classes and using interfaces. I've implemented a Solution from another post to test if I could get it working but unfortunately I have never used an interface ever.

So here's the basics of what I have:

Form1:

partial class Form1 : InterfacePareto
{
    public string myTest
        {
            get { return herpTxt.Text; }
            set { herpTxt.Text = value; }
        }  
} 

Interface:

interface InterfacePareto
{
    string myTest { get; set; }
}

MyWorkingOutClass:

Class MyWorkingOutClass
{
    private readonly InterfacePareto pare;

    public MyWorkingOutClass(InterfacePareto pare)
    {
        this.pare = pare;
    }

    private void Testtime()
    {
        string firstName = pare.myTest;
        pare.myTest = firstName + " extra";
    }
}

The purpose:

The plan at the moment is to get the text from the forms textbox. Then pass it to the working class. The working class then does whatever calculations etc needed, then passes the result back to the forms textbox.

My question is, is my code along the right tracks. If yes, then what am I missing/doing wrong? Or if anyone thinks this is not the right way of achieving what I need, do they have any suggestions?

Many thanks!

Was it helpful?

Solution

I've just tested code and this works fine for me:

public partial class MainForm :Form, InterfacePareto //My main form inheriting Form class and interface
{
    public MainForm()
    {
        InitializeComponent();
    }

    public string myTest
    {
        get { return herpTxt.Text; }
        set { herpTxt.Text = value; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //On button click create MyWorkingOutClass instance and pass MainForms instance
        MyWorkingOutClass mc = new MyWorkingOutClass(this); 
        //After this line text box content will change
        mc.Testtime();
    }
}

//Changed modifier to public
public interface InterfacePareto
{
    string myTest { get; set; }
}

//Changed modifier to public
public class MyWorkingOutClass
{
    private readonly InterfacePareto pare;

    public MyWorkingOutClass(InterfacePareto pare)
    {
        this.pare = pare;
    }

    //Changed modifier to public
    public void Testtime()
    {
        string firstName = pare.myTest;
        pare.myTest = firstName + " extra";
    }
}

OTHER TIPS

This should work fine.

There is one issue you will get when the MyWorkingOutClass does its work on a different thread than the UI thread.

To solve that you might want to change the implementation on the form to switch to the UI thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top