Click here to Skip to main content
15,662,459 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
Hi all,
I have a MainForm and approximately 40 DialogForms in my project. There is also a class (named for ex. MyClass) which contains most of the methods and procedures in my project. My problem is that;

1) In this case, should I describe MyClass.cs Static or Dynamic? I think the diference is that; static class methods remains on memory after first created, and dynamic class methods is coollected by Garbage Collector from time to time. On the other hand, every time I use a method after creating an instance of MyClass, the memory usage is getting increase until GC collected it. In addition to this, static class members keep the last value and sometimes it may be dangerous. Is that true? I am confusing to choice dynamic or static class.

2) If I define MyClass as static, there are also some MainForm variables I have to use. In this case, I think I have to use static members of MainForm and define MainForm as static. But, although I tried to make MainForm static, I have not managed to do like that; Is there a mistake?

MainForm.cs :

namespace MyProject
public static partial class MainForm : Form

Thanks in advance...
Updated 21-Mar-11 21:55pm
Pavel Yermalovich 22-Mar-11 4:20am    
Here is an example ot SmartUI antipattern.
H.Johnson 22-Mar-11 4:28am    
Hi Pavel, I could not see a link. Could you share again please?
Sergey Alexandrovich Kryukov 22-Mar-11 11:12am    
OP commented:

Thanks for all your replies. I just wanted to give a simple example to explain my problem. Therefore, I do not use all my methods and procedures in a single class. I have three project (DataAccess, Business and Presentation Layer) and a lof of classess in it. So, taking a look at the presentation layer: I have just wanted to give one of my Class as an example. On the other hand, by RAM usage, I meant that the instance of the class. In this case, waht do you suggest about that?

a)I make MyClass.cs as static.

b)I created all the methods and procedures in MyClass.cs as static.

c)In this case, if I define MyClass as static, there is a MainForm's instance I have to use in MyClass.cs in order to use MainForm's component. So, is it possible to create an instance of MainForm in static MyClass.cs?

d)What about the properties? Should I create all the properties in MyClass.cs as static? May ther be any problem for the last kept values?

MainForm fm = new MainForm();
/// Returns current student row selected on datagridview.
internal static StudentDS.StudentRow CurrentStudent
return (StudentDS.StudentRow)

Thanks in advance...

Whoa, whoa. Back up a little.

"Should I describe MyClass.cs Static or Dynamic?"

It's very simple: do you declare an instance of MyClass at any time? If you do, it needs to be dynamic. If you do not, then it can be static.

Generally, if you are using MyClass as a container for generic utility routines, then I would declare it as static: which means that all fields and methods within it also become static.

You are also a little confused as to grabage collection: "dynamic class methods is coollected by Garbage Collector from time to time" is not true: methods are never garbage collected, only fields and class instances. And they are only garbage collected when there are no more references to them. Memory use does not increase each time you use a method: stack use may, and memory may if you use the new keyword, but stack space will be reclaimed when the method exits, and if you do not keep references to objects created with new then they will be garbage collected in time.

For your (2), you do not have to declare MainForm as static in order to use use static members. In fact, because you need to instantiate MainForm in order to disply it, you cannot declare it as static.
Share this answer
Sergey Alexandrovich Kryukov 22-Mar-11 11:34am    
Griff, this is all correct, my 5. You understand how much OP is confused about the static, so he is probably not ready to understand all your reasoning. Remember we discussed my way of explaining thing? See my Answer (I'm not so enthusiastic though... :-)

First of all, I think your choises (1 and 2) is like choosing between two diseases (you don't want them both).
I you have to choose, I'd go for non static! However the project architecture you have right now is unwanted.

You have a couple is Windows Forms which is presentation. It's wise to seperate those forms in a single project. Then I think it's wise to move the methods in MyClass to a seperate project and catagorize them in classes if needed. Then you'll be able to instanciate the class you need to work in, and pass variables to it.

You may also want to make yourself familiar with the term 'Multitier Architecture[^]' which is the way how to structure your projects into a solid solution.

Good luck,

Share this answer
Sergey Alexandrovich Kryukov 22-Mar-11 11:30am    
You Answer is good, my 5, but don't you think OP won't be able to understand it because of absolute confusion on what is the static? Please see my Answer as well.
Dalek Dave 22-Mar-11 11:45am    
Good Answer, 5.
0) The good news: You are trying to separate presentation logic from application logic. That's a very good thing to do. The bad news is, that trying to concentrate all those things in one class is terrible object oriented design. Making this class static will turn the terrible object oriented design in something even worse: Terrible and old fashioned structural design.

1) Making a class static has certain consequences. Look here:[^]

If you make MyClass static, you turn it into a simple collection of globally accessible functions. It will work, you will not have to worry about creating or destroying instances of MyClass and all your forms can access the methods. So far, so good.

The disadvantages:
- It's a bad design. A class should never serve more than one purpose, no more, no less. Packing all logic into one class creates many possibilities for mix-ups.

- The entire application logic is accessible from everywhere. That's bad. There is no way to guarantee that some Form does not get access to things it is not supposed to. This can introduce all kinds of bugs.

- With all logic in one static class, you give away all further options for abstraction, encapsulation and (as said above) access restrictions.

So, a static 'MyClass' will work, but cannot and should not have any member variables. You would have to treat it as totally stateless. Trying to maintain some static state for the entire application in one static class will be very messy and error prone. Perhaps you may look at some better ways to separate application logic, presentation logic and data access. In that case you might be interested in the MVC (Model View Controller) or MVP (Model View Presenter) patrterns. Just search for them in the articles and you will find enough about both.

3) Forms cannot be static. They are not designed to work that way. For example, you may want to open the same form twice at the same time. A static form could not be used twice, therefore you must be able to create two separate instances, which you can only do with a non-static class.
Share this answer
Sergey Alexandrovich Kryukov 22-Mar-11 11:35am    
You are right, my 5, but... OP will hardly understand this because of absolute confusion about what the static is. Please see my Answer.
Dalek Dave 22-Mar-11 11:45am    
Good Call.
I meant your application. Antipatterns is that you dont need to repeat. I recommend you to read this book to get introduced with enterprise architecture
Share this answer
H.Johnson 22-Mar-11 4:58am    
Thanks, but I do not have enough time to read this book 500 page :) and I need a suitable solution an advices suitable on my situation.
Sergey Alexandrovich Kryukov 22-Mar-11 11:17am    
Shame on you
Pavel Yermalovich 22-Mar-11 5:08am    
You've got the answer form OriginalGriff. But if you are going to continue be a developer you don't need to say you have no time and 500 pages is so much for you
No, your understanding of static and non-static classes (not "dynamic") is absolutely wrong, and hence all the problems.

It is not the difference in garbage collection or memory use. The difference is that a non-static class (a type) can by used to create one or more instancesobjects. It can have instance (non-static) methods and properties. Main difference of instant method and property is access to "this", that is, the instance of the class. It is passes as an implicit (invisible) parameter "this". As a result, instance method and properties can implicitly access (through "this") other instances and properties of the same instance. (Explicitly, what the instance is known as a variable or parameter, all code can access method and properties, depending on accessibility.)

Static classes can not be instantiated and can not have instance (non-static) methods and properties. They still can work with data, but only static data.

Generally, non-static classes can be uses as static, but static classes can not be uses as non-static (instantiated), so being a static class in a constraint.

Please, if you want to say "I knew all that", think again. If you knew the works, it does not mean you understand it.

Share this answer

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