none
Class reference in winform app RRS feed

  • Question

  • Excuse my newbie question, but we all started with this types of questions..at least i hope so :). So, I want to create a winform app that takes some input from the user(name, age, married or not) and after you press the add user button will create a text file. Then the app closes and when you open it again it will read the text file and display the text(name, age, married or not) in a listbox.

    The problem is I don't know if i should create a new class or write the code in the Form1.cs(but i guess this is wrong?). If I need to create a class, how can I make the reference to userListBox? I've worked only in unity and I know nothing about theese things. If I'm not unclear(I know I'm not) please ask for more information.

    Saturday, October 12, 2019 11:50 AM

Answers

  • For such a simple thing, you do not need to create a class; you may as well add it in the form itself. But if you want to educate yourself in separating concerns, moving out of the UI the code that does not do UI-related things, then it is a good practice to move it into a separate class.

    However, the class should not access the userListBox, or any other components in the UI. This would go against the "separation of concerns" criteria that is what led you to move into a separate class the operations that are not UI-related. Instead, the code in the Form should take any data that needs to be processed and pass it as arguments to a method in your class. The class performs its operations (write into a file) and returns back as a result of the method call any information that the calling form needs to display in the UI. In this way, the class does not need to have (and should not have) any "knowledge" about the UI that is calling it.

    Similarly, during the Form_Load event, the form calls a method in the class that reads the data from the file and returns it to the caller. The caller then presents the data in the corresponding UI controls. Again, in this way the class does not need to reference any controls in the UI.


    Saturday, October 12, 2019 12:41 PM
    Moderator
  • The problem is I don't know if i should create a new class or write the code in the Form1.cs(but i guess this is wrong?).

    If you write such code in the form class, then that's called tight coupling. 

    https://www.c-sharpcorner.com/UploadFile/yusufkaratoprak/difference-between-loose-coupling-and-tight-coupling/

    You should try to implement SoC in your solution, which means don't put all the code for data access in the button click-event.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    If I need to create a class, how can I make the reference to userListBox? I've worked only in unity and I know nothing about theese things. 

    The code using the Listbox instances a class/object using a method on the object that is going to return data to the Listbox by reading the data from the file, formulating the data and returning the data  to the code that has instanced the class into an object and making calls to methods/behavior on the object.

    Listbox.Datasource = new Dataclass().GetData();


    • Edited by DA924x Saturday, October 12, 2019 1:08 PM
    • Marked as answer by horatiu_alex Saturday, October 12, 2019 2:25 PM
    Saturday, October 12, 2019 1:06 PM

All replies

  • For such a simple thing, you do not need to create a class; you may as well add it in the form itself. But if you want to educate yourself in separating concerns, moving out of the UI the code that does not do UI-related things, then it is a good practice to move it into a separate class.

    However, the class should not access the userListBox, or any other components in the UI. This would go against the "separation of concerns" criteria that is what led you to move into a separate class the operations that are not UI-related. Instead, the code in the Form should take any data that needs to be processed and pass it as arguments to a method in your class. The class performs its operations (write into a file) and returns back as a result of the method call any information that the calling form needs to display in the UI. In this way, the class does not need to have (and should not have) any "knowledge" about the UI that is calling it.

    Similarly, during the Form_Load event, the form calls a method in the class that reads the data from the file and returns it to the caller. The caller then presents the data in the corresponding UI controls. Again, in this way the class does not need to reference any controls in the UI.


    Saturday, October 12, 2019 12:41 PM
    Moderator
  • So you are saying that I can separate all the things that are not UI related. If i got it right. The create text file code i can put it in a different class, but not the read text file to userListBox. Hmm that makes sense! But do I need to link the createFileText to something, or I can just create a class and write it there?
    Saturday, October 12, 2019 12:53 PM
  • The problem is I don't know if i should create a new class or write the code in the Form1.cs(but i guess this is wrong?).

    If you write such code in the form class, then that's called tight coupling. 

    https://www.c-sharpcorner.com/UploadFile/yusufkaratoprak/difference-between-loose-coupling-and-tight-coupling/

    You should try to implement SoC in your solution, which means don't put all the code for data access in the button click-event.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    If I need to create a class, how can I make the reference to userListBox? I've worked only in unity and I know nothing about theese things. 

    The code using the Listbox instances a class/object using a method on the object that is going to return data to the Listbox by reading the data from the file, formulating the data and returning the data  to the code that has instanced the class into an object and making calls to methods/behavior on the object.

    Listbox.Datasource = new Dataclass().GetData();


    • Edited by DA924x Saturday, October 12, 2019 1:08 PM
    • Marked as answer by horatiu_alex Saturday, October 12, 2019 2:25 PM
    Saturday, October 12, 2019 1:06 PM
  • The create text file code i can put it in a different class, but not the read text file to userListBox.

    No, that's not what I said. The "read text" also goes into the separate class. The important thing is that it does not read into the userListBox. It reads into a variable in memory. Then the variable is returned from the class into the Form, and the Form copies from the variable into the ListBox.

    Saturday, October 12, 2019 2:08 PM
    Moderator