Answered by:
Problem to event

Question
-
Hi,
Is there a way to have one Out parameter, or to return one string, from Main, to Console App below
static string Main(string[] args) { string File1,OrigPath; File1 = "C:/ed/List0.txt"; //OrigPath="C:/ed/data/"; OrigPath = args[0]; try { Path.GetDirectoryName(OrigPath); } catch { return "Path does not exist!"; }
How to adjust the above, due to this error?
Error 2 'ScanFolder.Program.Main(string[])': not all code paths return a value C:\App\ScanFolder3_2\ScanFolder\Program.cs 17 23 ScanFolder
Many Thanks & Best Regards, Hua Min
- Edited by Jackson_1990 Thursday, October 27, 2016 8:58 AM
Answers
-
First of all: A console application can only have a return type of void or int. See Main() Return Values (C# Programming Guide)
When you have as return type int, then each code path must return a value or you use the pattern of single point of return:
namespace ConsoleCS { using System; using System.IO; public class Program { static int Main(string[] args) { const int NO_ERROR = 0; const int UNKNOWN_EXCEPTION = 255; try { string OrigPath; OrigPath = args[0]; Path.GetDirectoryName(OrigPath); return NO_ERROR; } catch { return UNKNOWN_EXCEPTION; } } } }
I strongly recommend using the SPOR pattern:
namespace ConsoleCS { using System; using System.IO; public class Program { static int Main(string[] args) { const int NO_ERROR = 0; const int UNKNOWN_EXCEPTION = 255; int result = NO_ERROR; try { string OrigPath; OrigPath = args[0]; Path.GetDirectoryName(OrigPath); } catch { result = UNKNOWN_EXCEPTION; } return result; } } }
- Proposed as answer by Sabah ShariqMVP Thursday, October 27, 2016 1:15 PM
- Marked as answer by Jackson_1990 Friday, October 28, 2016 7:10 AM
All replies
-
First of all: A console application can only have a return type of void or int. See Main() Return Values (C# Programming Guide)
When you have as return type int, then each code path must return a value or you use the pattern of single point of return:
namespace ConsoleCS { using System; using System.IO; public class Program { static int Main(string[] args) { const int NO_ERROR = 0; const int UNKNOWN_EXCEPTION = 255; try { string OrigPath; OrigPath = args[0]; Path.GetDirectoryName(OrigPath); return NO_ERROR; } catch { return UNKNOWN_EXCEPTION; } } } }
I strongly recommend using the SPOR pattern:
namespace ConsoleCS { using System; using System.IO; public class Program { static int Main(string[] args) { const int NO_ERROR = 0; const int UNKNOWN_EXCEPTION = 255; int result = NO_ERROR; try { string OrigPath; OrigPath = args[0]; Path.GetDirectoryName(OrigPath); } catch { result = UNKNOWN_EXCEPTION; } return result; } } }
- Proposed as answer by Sabah ShariqMVP Thursday, October 27, 2016 1:15 PM
- Marked as answer by Jackson_1990 Friday, October 28, 2016 7:10 AM
-
-
Stefan already answered you.
No, you cannot return a string from the Main method or have an 'out' parameter. The Main method is called automatically by the system when a Console application is executed and must follow the predefined patterns (returning void or int, and accepting no parameters or a string array of arguments).
After all, where do you expect the returned/out string to go?
If you want the console to output error strings you can write them to a log or output them to the standard error output as demonstrated here.
-
Many thanks.
One last thing, when running such Console App, that is returning int, how to 'get known' there is error being returned or not?
Many Thanks & Best Regards, Hua Min
- Edited by Jackson_1990 Thursday, October 27, 2016 9:51 AM
-
If you choose to return an int, then you're setting the %ERRORLEVEL%.
Whether these values indicate an error or not is simply by convention. You define for example that any error code different than 0 literally means an error. Or you can define 0 no error, path found, 1 no error, path not found, 2 no error, path found but not enough permissions..
btw, that's why I've used constants in my samples.
-
-
How are you running/calling the application?
Assuming you are running an app from another C# application, and assuming you are doing this using the standard Process class, then you can check the Process.ExitCode property
See the example here (obviously this ExitCode is only going to be valid once the other process has terminated).
-