Search code examples
c#classoopclass-method

Object Oriented Programming - Basic informations on Methods with WindowsForm


I studied in programming and graduated in 1998... yeah i'm old LOL Long story made short, I never worked in that field, but decided to get back to it to make myself a membership management program. There is one thing I can't recall about object oriented programming and I'd need help if someone could please clarify a few things for me.

I have a program I'm working on that has a main Windows Form calling on different other forms to perform different actions on a database containing information about a sports center that I own.

After a while working on the project, I realized that a few methods I'm using in my different child forms are exactly the same. For example, if the user enters a first and last name, i often have to find what's the memberID. I just copied the code to fasten the process.... BUT !

I DO remember my teachers kept saying:

"If you need something to be accessed by many, then why should it belong to someone?"

and I know it's not a good way to program.

My question is: How do I work that correctly? Should I put my method in the main parent form and call it from the others? Is that the right way to do it? Do I just need to use a class containing these methods and then I can call them from anywhere in the app? I'm confused.

Thank you for your time and help.


Solution

  • To solve these kind of issues we have some options:

    1. Helper Class: Create Helper class. Make it public, this class will be available for all forms. Now you can use methods of this class in all forms.

    2. Static class: Create a static class with static properties. by using this you can access use this property value any where throughout application. Main advantage of this you don't need to hit database again and again to get same record.

    Hope this will help you.