Home All Groups Group Topic Archive Search About

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

Author
8 Mar 2006 1:29 PM
johnnyG
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

Author
8 Mar 2006 1:42 PM
Peter Macej
> Since Main() is a subroutine in VB.NET console apps and not a function, is

Main may be also function:
Function Main(ByVal CmdArgs() As String) As Integer

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconhelloworld.asp

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
8 Mar 2006 3:40 PM
Herfried K. Wagner [MVP]
"johnnyG" <john***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
4 May 2006 12:28 PM
John Read
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


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "johnnyG" <john***@discussions.microsoft.com> schrieb:
> > 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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
4 May 2006 1:37 PM
Phill W.
John Read wrote:

> 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.
Author
4 May 2006 2:12 PM
John Read
That did the trick, my knowledge of MSDOS is quite limited :)

Thanks

John

Show quoteHide quote
"Phill W." wrote:

> John Read wrote:
>
> > 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.
>
Author
5 May 2006 3:44 AM
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.

Show quoteHide quote
"John Read" <JohnBaer@online.nospam> wrote in message
news:4AD1B5E7-2ABC-4A4A-9A42-84C5683BA22C@microsoft.com...
> That did the trick, my knowledge of MSDOS is quite limited :)
>
> Thanks
>
> John
>
> "Phill W." wrote:
>
> > John Read wrote:
> >
> > > 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.
> >
>