Console Application - Exit Code

M

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
 
H

Herfried K. Wagner [MVP]

Mythran said:
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

Mythran

Herfried K. Wagner said:
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.

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
 
H

Herfried K. Wagner [MVP]

Mythran said:
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

Mythran

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top