Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a MainForm, a DialogForm and a MyClass containing most of the methods I use. I have the situation below;

I) on MainForm : I create an instance of MyClass in order to access methods or procedures on MyClass. There is no problem for this.

II) on MyClass : For some methods like Databinding that requires label binding, I think it is better to move this kind of methods to MainForm as they need some components (label, etc) on the MainForm. If I use this kind of methods on MyClass, I have to create MainForm's instance on MyClass that is not seem to be logical. Because in this case there is a ring relations between MainForm and MyClass. So, instead of this, I moved this kind of methods to MainForm. Is that true?

III) On the other hand, I need to access some of the MainForm methods or procedures from DialogForm. But I want to this without creating an instance of MainForm. I have no idea which way to go. So, in this scenario, what should I do? Could you give some examples for the 3 situations above?

BR
Posted

I think if you researched patterns like MVC or MVVM you would get a solution.
Anything that required access or manipulation of a UI element should, of course, be in the form or dialog. Anything else can be moved to an external class so it can be used in any situation. Use databinding to connect the UI and class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-11 14:04pm    
Nice suggestion, my 5.
I only added some formal considerations which are valid even without analyzing the semantics of intended use, please see.
--SA
"Without creating an instance" simply means you need a static method with internal or public access. However, it's hardly an elegant way in case of main form class. If you don't need an instance of this class, why not having the method is some other class, let's sat, a utility class?

—SA
 
Share this answer
 
"Without creating an instance" simply means you need a static method with internal or public access. However, it's hardly an elegant way in case of main form class. If you don't need an instance of this class, why not having the method is some other class, let's say, a utility class?

—SA
 
Share this answer
 
Comments
H.Johnson 30-May-11 2:07am    
Thanks SAKryukov, but I have never used this kind of class. If it is possible, could you give en example please? If not, never mind; I will try some methods.
BR
Sergey Alexandrovich Kryukov 30-May-11 13:53pm    
I can give you an example, but this would be too general. Instead, look at my explanation and then decide if you still need any sample (I would gladly write for you).

There is not special "kind of class". This is just a term from code design.
If the method is called without instantiation, it's static. If it's static, it cannot use "this", if only uses the data of input/output method arguments. If so, it is abstract enough and not directly related to concrete application, can be used in other places. If, so, Form or other application class is a bad place, the method will only contaminate it, such method belongs to a separate class, usually also static. Such classes are called utility classes. Imagine: classes "StringUtility", "AttributeUtility", "AssemblyUtility", etc.
Is that clear? I would put a sample, but it's so abstract that words are better. Ask a question, anyway...
--SA
H.Johnson 9-Jun-11 6:54am    
Thanks for reply. Actually I want to be clarified on tese issues.

Assume that; I have a MainForm and a DialogForm and I have to use a custom method both of these Main and Dialog forms. So, what is the most logical way? I chose that; cteating a method on a Class and access this method in the Class from Main and Dialog forms. Other case, I have to create this method 2 times. Now; if I create my method in the Class, there is also some ways.
a) Creating the Class's instance on DialogForm and accessing the method from DialogForm.
b) Creating my method as static and accessing this method from everywhere (DialogForms, etc.) directly like that; Class.MyMethod()? which one is better?
c) On the other hand, there is a pretty way like using property. But I am not sure if it is suitable for my situation. Neverthless, assume that if I use property on the MainForm in order to access the MainForm's variables in my Method in Class. In this case it does not make any sense by the way I reach this my method from the DialogForm. Am I wrong?
Kindest regards...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900