Home All Groups Group Topic Archive Search About

Console Application - Exit Code

Author
12 May 2006 5:54 PM
Mythran
What is the correct way to set the exit code from within a console
application?  Currently we do:

Public Function Main() As Integer
    ...
    Environment.ExitCode = ...
    Return Environment.ExitCode
End Function

Should we use Environment.ExitCode And/Or return the exit code?

Thanks,
Mythran

Author
12 May 2006 10:08 PM
Herfried K. Wagner [MVP]
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> schrieb:
> What is the correct way to set the exit code from within a console
> application?  Currently we do:
>
> Public Function Main() As Integer
>    ...
>    Environment.ExitCode = ...
>    Return Environment.ExitCode
> End Function
>
> Should we use Environment.ExitCode And/Or return the exit code?

You do not need to (and IMO should not) set 'Environment.ExitCode' if you
return the exit code in the 'Main' function and vice versa.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 May 2006 10:36 PM
Mythran
Show quote Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OPh0NChdGHA.5016@TK2MSFTNGP04.phx.gbl...
> "Mythran" <kip_potter@hotmail.comREMOVETRAIL> schrieb:
>> What is the correct way to set the exit code from within a console
>> application?  Currently we do:
>>
>> Public Function Main() As Integer
>>    ...
>>    Environment.ExitCode = ...
>>    Return Environment.ExitCode
>> End Function
>>
>> Should we use Environment.ExitCode And/Or return the exit code?
>
> You do not need to (and IMO should not) set 'Environment.ExitCode' if you
> return the exit code in the 'Main' function and vice versa.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>

After performing some tests, I found that setting Environment.ExitCode does
nothing when Main is defined as a function (and hence, returns a value).  If
it is defined as a sub, setting ExitCode sets the exit code.  So yes, you
are correct :)

Thanks,
Mythran
Author
12 May 2006 10:46 PM
Herfried K. Wagner [MVP]
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> schrieb:
> After performing some tests, I found that setting Environment.ExitCode
> does nothing when Main is defined as a function (and hence, returns a
> value).  If it is defined as a sub, setting ExitCode sets the exit code.
> So yes, you are correct :)

Note that VS.NET 2003 (and maybe VS 2005) didn't show the correct return
value when running the project from within the IDE.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 May 2006 5:22 PM
Mythran
Show quote Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23M5MKdhdGHA.1260@TK2MSFTNGP05.phx.gbl...
> "Mythran" <kip_potter@hotmail.comREMOVETRAIL> schrieb:
>> After performing some tests, I found that setting Environment.ExitCode
>> does nothing when Main is defined as a function (and hence, returns a
>> value).  If it is defined as a sub, setting ExitCode sets the exit code.
>> So yes, you are correct :)
>
> Note that VS.NET 2003 (and maybe VS 2005) didn't show the correct return
> value when running the project from within the IDE.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>

I didn't test running it in the IDE.  I had a javascript that ran it and
display the results of the Exec or Run (whichever one it was) function :)

Mythran