Sign in to follow this  
Side Winder

[.net] File organisation in a multi-form project

Recommended Posts

Side Winder    174
What's the usual "etiquette" for this? For example, is it best to have all Form related code in the Form file and any classes in a separate file? Right now I've put my classes into separate files, but had problems accessing the Form controls to change any data. I solved it by using a static variable for the form(s). Is this good coding practice?

Share this post


Link to post
Share on other sites
TickTwo    122
Every class should be in its own file. And static variables like that are a -no go- imho! It simply leads to ugly architecture.

What do your classes do with the form? Do they really need access to the Form?
You could pass your Form as a paramter to the constructor of your classes, but that's also not the best way to do it.

You should always try to seperate your GUI logic and your business logic.

I recommend you to take a look at the Model View Controller (MVC) and/or the View-ViewModel-Model (VVMM) patterns.

Tbh I don't really know how to make a complex WinForms program either; but these patterns surely are recommended to be used there too. I just recently got into creating programs using Windows Presentation Foundation, and the VVMM-patter has been very useful there so far.

Maybe someone with some real world experience could help more~
tick

Share this post


Link to post
Share on other sites
TheTroll    883
You normally have two types of data when talking about classes. Instance data and class data. Instance data is when you "new" a class and the data is contained within that new instance of the class. Class data is data that is shared among all the instances of a class. As a general rule, static data members should only be used for class data.

For what you are doing, you need to pass a reference of the data class to the forms that need access to to.

theTroll

Share this post


Link to post
Share on other sites
CyberJay82    229
You could also use delegates to help exchange data from your classes to your form UI code.

This is used like an event so when ever data changes in your class and you need to signal the form to update the UI, your class just calls the delegate that your form hooks into.

Example:

This would be part of your class code. This sets up an event that anyone can hook into if they have access to the class.

public delegate void DataStatus(object Sender, DataEventArgs e);
public event DataStatus OnDataChange;




In your form code you could hook into the event like so.

DelagateExample de = new DelagateExample();
de.OnDataChange += MyFunctionToCall;




Now in your class, once you needed to exchange data to your form you could call the event like this.

if(OnDataChange != null)
OnDataChange(this, new DataEventArgs(myVariableToExchange));




Then in your form code the MyFunctionToCall would be triggered.

public void MyFunctionToCall(object sender, DataEventArgs e)
{
// do whatever you need to with the data, like set a textbox's text
textbox1.Text = e.MyData.ToString();
}




Note: There is more involved if you have multi-threaded app or need to hook more than one function to an event.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this