Can a console VB.NET prog return a value to caller?

G

Guest

Greetings,
Since Main() is a subroutine in VB.NET console apps and not a function, is
there a way to return a value to a script ro other "caller" for a VB.NET
console .exe?
sub main()
like
if file.exists(myFile)
return 1 'file downloaded
else
return 0 'file not downloaded
exit sub
Any ideas? Am I missing something simple...?

thanks,
johnny
 
H

Herfried K. Wagner [MVP]

johnnyG said:
Since Main() is a subroutine in VB.NET console apps and not a function, is
there a way to return a value to a script ro other "caller" for a VB.NET
console .exe?

\\\
Public Module Program
Public Function Main(ByVal Args() As String) As Integer
For Each Arg As String In Args
MsgBox(Arg)
Next Arg
Application.Run(New MainForm())
If...Then
Return 0
Else
Return 1
...
End If
End Function
End Module
///

Select 'Sub Main' as startup object in the project properties.

Alternatively you can use 'Application.Exit(<exit code>)' or set
'Environment.ExitCode' to the exit code.

Note that the IDE won't show the correct exit code when debugging.
 
G

Guest

Herfried,

I have been asked to amend a number of .Net 1.1 batch processes so that they
return an exit code of 0 when it was successful and an exit code of 1 when it
fails. I have been playing with Environment.ExitCode and Environment.Exit.
As you mention here VS.Net 2003 always displays an exit code of 0 in the
Debug Window. How else can I tell what the exit code is after running the
..exe? I can't figure out how to test that Environment.Exit(1) actually
returns 1 and not the default 0.

Any help would be greatly appreciated

Regards

John
 
P

Phill W.

John said:
How else can I tell what the exit code is after running the .exe?
I can't figure out how to test that Environment.Exit(1) actually
returns 1 and not the default 0.

You could put up a dialog with the intended result just before leaving
the program (when the Debugger's attached, of course), but you'd still
be relying on the Exit code being returned correctly.
The only way to confirm /that/ is to run the program "for real" and test
the value returned.

[test.bat]
start /wait program.exe rubbish arguments
echo %ERRORLEVEL%

HTH,
Phill W.
 
G

Guest

That did the trick, my knowledge of MSDOS is quite limited :)

Thanks

John

Phill W. said:
John said:
How else can I tell what the exit code is after running the .exe?
I can't figure out how to test that Environment.Exit(1) actually
returns 1 and not the default 0.

You could put up a dialog with the intended result just before leaving
the program (when the Debugger's attached, of course), but you'd still
be relying on the Exit code being returned correctly.
The only way to confirm /that/ is to run the program "for real" and test
the value returned.

[test.bat]
start /wait program.exe rubbish arguments
echo %ERRORLEVEL%

HTH,
Phill W.
 
M

Michael D. Ober

Watch out for the %ErrorLevel% variable. You need to test for higher
numbered error codes first as it %errorlevel% returns true if the return
code is equal to or greater than the tested level. Also, %errorlevel% is
only documented to support errorlevels 0 to 255.

Mike Ober.

John Read said:
That did the trick, my knowledge of MSDOS is quite limited :)

Thanks

John

Phill W. said:
John said:
How else can I tell what the exit code is after running the .exe?
I can't figure out how to test that Environment.Exit(1) actually
returns 1 and not the default 0.

You could put up a dialog with the intended result just before leaving
the program (when the Debugger's attached, of course), but you'd still
be relying on the Exit code being returned correctly.
The only way to confirm /that/ is to run the program "for real" and test
the value returned.

[test.bat]
start /wait program.exe rubbish arguments
echo %ERRORLEVEL%

HTH,
Phill W.
 

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