none
Problem to event RRS feed

  • 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


    Thursday, October 27, 2016 8:57 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
    Thursday, October 27, 2016 9:07 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
    Thursday, October 27, 2016 9:07 AM
  • Thanks a lot.
    Can we return one string from Main event? Or to have one Out parameter, which is string?

    Many Thanks & Best Regards, Hua Min

    Thursday, October 27, 2016 9:19 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.

    Thursday, October 27, 2016 9:31 AM
  • 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


    Thursday, October 27, 2016 9:44 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.

    Thursday, October 27, 2016 9:57 AM
  • Hi Stefan,

    I mean to detect if any error code has been returned, when running/calling such Application (.exe file), on Windows.


    Many Thanks & Best Regards, Hua Min

    Thursday, October 27, 2016 3:30 PM
  • 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).

    Thursday, October 27, 2016 3:48 PM
  • Read the link I've posted. This is the way you detect it.

    Maybe you should explain what you're trying to do.. what's your context?

    Thursday, October 27, 2016 4:51 PM