TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Chris
NA
2
4.3k
Newbie confusion to do with Main() when trying to tidy up a program.
Nov 13 2010 10:09 AM
This is a general problem I have always had trying to understand how C# is meant to be used. It might be that there is some silly syntax mistake I always make, so I think I can't do something, or it could be failure to get it at all.
I write a program to do something. It has a few classes with their variables and methods etc and it has one class which contains Main(). Pretty much the only thing in that class is Main().
I develop the program to do what ever it is meant to do and eventually I get it to work. When I've finished Main() might be a few hundred lines long and has a whole bunch of variables and some code controlling instantiations of classes.
And that is all fine except I don't like the look of the long piece of code in Main(). It is too big to follow easily and maybe has a couple of big nested loops etc. Additionally I might decide that now its working I'd like to loop through the whole thing with different options or whatever.
So, I want to take some of the bigger chunks of code in Main() which have specific purposes and turn them into new methods. That way the code will by much easier to understand and adjust.
And this is where it goes wrong for me, I can't seem to work out how, in general, you are meant to partition the Main() method into several new methods.
* You can't define methods at all within Main(), it seems that just isn't allowed - or am I wrong????
* If you put the new methods into an existing class, the class needs access to all the variables and methods previously in Main() i.e. it needs to contain them. It might be the case that sometimes this makes sense but in general it is probably no good because they don't really belong there and also Main() then has to access them through the class instance.
* You could put the data and new methods into another new class. This would be like a program flow control class. But does anyone do that? It just seems clunky. And main still has to access them through the class instance. It just strikes me that I am moving all the stuff I expect to find in Main() into another class, just so that I can make some local methods. In the end Main() won't have any purpose other to instantiate and run the other class. This just can't be what was intended - or is it????
* You can put the data and new methods into the class which contains Main(). But this is really just a special case of the previous option, the data might be in the same class as Main() but it still has to be accessed through a class instantiation.
Have I missed some basic point or what?
Thanks in advance.
=============================================================================
I'm having to reply here because "reply" doesn't seem to work for me...
Thanks so much for your response. I feel a lot better about it now. I have several books in fact, I don't know if they are good or not but I think they are acceptable. What has caused me the confusion is that the books are restricted to smallish programs and so the reader never sees my problem arising, everything can go into Main() and that's just dandy. But when I go on a spree and Main() gets over a certain size, I just begin to think, hang on, this can't be right.
The last program I did was a nightmare to partition. After reading the reply below I think the mistake I make when I know a program might get big, is declaring all the variables inside Main() at the start. If they were declared inside the Program class to begin with then accessibly issues would get sorted out as the program developed, where as moving them out of Main() later is always as disaster.
After posting I did wonder about Main() being the method called at runtime and so perhaps it really was meant to just call up another class (unlike the book examples!). I haven't had to integrate with other C# code yet but I imagine this must be easier if Main has as little in it as possible.
In fact I expect this is the correct way to think about it. Rather than treat the code as a big program which does such and such, treat it as a piece of code that might just sit around in a class in a namespace and perhaps get called once in a while. From that point of view then, all the program flow control stuff is clearly just a regular method inside just another class. And Main() has nothing to do with it. Then I just use main to make an instance of my code being used.
But what actually happens is that when a program is fairly small or is going to get deleted anyway, people are bound to think it isn't worth the trouble of writing the code in yet another method. So all the examples you get as a newbie fail to make this clear.
Reply
Answers (
1
)
Problem when summing datagridview selected cells
C# forms app web interface, Problem