I understand that C# console applications need an entry point of Main(), and that Main() can return and integer or nothing. My question is, what do people typically put in this method? I understand that the method is needed otherwise the compiler will not know where to start, but it kind of seems like a waste. For instance, I am working on part of an EDI and the first task is to read the contents of a directory into an array, but this does not seem appropriate for the Main() method. I realize this is probably just my inexperience asking, but any input would be greatly appreciated. Thanks.
C# Entry Point
Collapse
X
-
You can use it to instanciate any classes you need, setup and directory structures etc.
Al though generally it would be more like
Code:int main(string[] args) { //if required: //do something based upon the provided arguments initfunction(); somefunction(); //maybe a loop // return 0; }
-
Originally posted by PlaterYou can use it to instanciate any classes you need, setup and directory structures etc.
Al though generally it would be more like
Code:int main(string[] args) { //if required: //do something based upon the provided arguments initfunction(); somefunction(); //maybe a loop // return 0; }
Comment
-
Originally posted by PlaterYou can use it to instanciate any classes you need, setup and directory structures etc.
Al though generally it would be more like
Code:int main(string[] args) { //if required: //do something based upon the provided arguments initfunction(); somefunction(); //maybe a loop // return 0; }
The entry point has got to be static
</nitpicking>
@OP If you don't include the entry point then how are you going to specify where your program starts?Comment
-
Originally posted by r035198x<nitpicking>
The entry point has got to be static
</nitpicking>
@OP If you don't include the entry point then how are you going to specify where your program starts?Comment
-
Originally posted by mcfly1204If you read what I wrote originally, you will see that I am aware of the compiler needing the entry point to know where to begin. If you keep reading you will see that my question is, logically, what else can you do with this entry point? i.e. this obviously begins the application, but how can I use it to move on through my code?
You shouldn't be doing much in the Main method anyway. The best programs simply use it to instantiate an object of some class which drives the whole application.Comment
-
Originally posted by r035198x<nitpicking>
The entry point has got to be static
</nitpicking>
And don't get so caught up on the return integer. As far as I know it was really on used back in the days of DOS to set the ERRORLEVEL enivronment variable for use in batch file scripts and possibly just to notify the shell that the program "failed" at whatever it wanted to do.
I don't even think most .NET projects have you return an int. Mine auto-declare main() as voidComment
-
After some reading I see that returning an integer is helpful to indicate success or failure, which I am not concerned with. I also know that I am not trying to pass any value from the command line, so the parameters will be blank. So I understand how I will structure the method, but what could I place in the method that would begin the application. I don't know, specific to my case, what I would instantiate.Comment
-
Originally posted by mcfly1204After some reading I see that returning an integer is helpful to indicate success or failure, which I am not concerned with. I also know that I am not trying to pass any value from the command line, so the parameters will be blank. So I understand how I will structure the method, but I could place in the method that would begin the application.Comment
-
I think I understand a little better now. Here is what I went with:
namespace P3A4
{
class MainDriver
{
static void Main()
{
XMLSelect select = new XMLSelect();
select.SelectFi les();
select.InsertSL X();
}
class XMLSelect
{
public void SelectFiles()
{
......
So basically, the class that the Main method is contained in does not have to be descript as far as the application is concerned. However, you then can instantiate another class as an object, such as XMLSelect, and begin to call methods that will begin the application. Correct thinking?Comment
Comment