简体   繁体   中英

How can I better organize my code in C#?

I'm writing my first ever GUI and my first program in C#. My GUI has dozens of menu options, most of which have very simplistic routines like

private void menuItem54_Click(object sender, EventArgs e)
{
  button1.Text = menuItem54.Text;
  functionFoo(menuItem54.Text);
}

So my question is, can I somehow place all of these functions in another file and include them in my main program? That why my main .cs file isn't cluttered? Thank you!

Yes you can.

just do basic OOP and create class file based on object.

You can learn more about OOP and SOLID Principle here.

So for your example, maybe you can create an interface for the menu.

and create different menu object which uses the same interface.

public interface IMenu{
string functionFoo();
}

public class Menu54 : IMenu {
public string Text { get; set;}
public string functionFoo(){
return Text;
}

You may think about MVVM pattern for your application. Read more: http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM