Jump to content
  • Advertisement
Sign in to follow this  
Side Winder

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

This topic is 3714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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~

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites
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.


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!