473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
Mar 8 '06 #1
6 15446
> 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/de...helloworld.asp

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Mar 8 '06 #2
"johnnyG" <jo*****@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/>

Mar 8 '06 #3
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
"Herfried K. Wagner [MVP]" wrote:
"johnnyG" <jo*****@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/>

May 4 '06 #4
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.
May 4 '06 #5
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.

May 4 '06 #6
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" <Jo******@online.nospam> wrote in message
news:4A**********************************@microsof t.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.


May 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Klatt | last post by:
class Foo { public : explicit Foo(int i) : m_int(i) {} private : int m_int; }; Foo f(int i)
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
16
by: DaTurk | last post by:
Hi, I have a c# application that needs to access c++ libraries, so it does this by using a managed layer of c++ CLI. Anyway, in the CLI function call, that calls the unmanaged function it...
2
by: Mick Walker | last post by:
Public Sub CheckProduct(ByVal _ConnString As String, ByVal ProductList As List(Of Import_ImportLines.Lines)) Dim ReturnValue As Integer = 0 ' Our Return Value Dim conn As New SqlConnection Dim...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
12
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from...
1
by: Derek Hart | last post by:
I am using an MSScriptControl in vb.net to do an evaluation of a text string: tempAnswer = MyScriptControl.Eval(MyScriptString) But how can I use this to get a return value from a vbscript...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.