Everyone,
I have a question... hope you can help me...

I have 4 methods and they are expecting to return some value to the caller.

MethodA()
MethodB()
MethodC()
MethodD()

MethodA() call MethodB() then MethodB() call MethodC() then MethodC() call MethodD() and MethodD() will return something. if the MethodD() return empty to MethodC(), how can I stop the execution and return it to MethodA() without returning to MethodB() and others.

Please help! If you have any question, please post it as my question is not that clear. ThankYou!

Can you not just return an empty string?

You could always throw an exception. It's not recommended but it's an option.

i mean if MethodD() found that this particular string is empty (some condition), I want the system to return back to MethodA() instead of going back to MethodC() then back to MethodB() then to MethodA()..

Is there such thing in programming for C#?

If i throw an exception in MethodD(), how do i check if there is an exception in MethodC() and MethodB() and MethodA()?

I don't know to be honest. If its an app where the user needs to enter text I'd show a messagebox if the string was empty.

try putting the try catch in MethodA and throwing the exception in MethodD. It should just fall through to MethodA.

commented: Thanks for the reply +3

This is complicated...
MethodA will call MethodB then MethodB will call MethodC and expect a bool return.
MethodC will call MethodD and expect a bool return after checking the string. If the string in MethodD is empty, I want the system to return to MethodA. If the string in MethodD is not empty, it will return a bool to MethodC then to MethodB then back to MethodA.

You can't just return to MethodA. MethodA didn't call MethodD--MethodC did. Even with exceptions, if unhandled, they are passed back to the caller. An exception could potentially be passed from D to C to B to A. Also, control needs to be passed back from MethodD to MethodC so clean-up of MethodC can occur--disposing of objects. Why do you feel that you should avoid returning up the chain--from D to C to B to A?

commented: Thanks =) +3

Is this the kind of thing you're looking for?

        public static void MethA()
        {
            try
            {
                MethB();
                Console.WriteLine("String is NOT empty");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }

        private static bool MethB()
        {
            return MethC();
        }

        private static bool MethC()
        {
            return MethD();
        }

        private static bool MethD()
        {
            string userInput = Console.ReadLine();

            if (!string.IsNullOrEmpty(userInput))
            {
                return true;
            }
            else
            {
                throw new Exception("String is empty");
            }
        }

If you throw the exception in MethodD it either throws an exception, bombs out of the application and displays the exception or the exception is caught by the first try/ catch it comes to and is delt with accordingly.

@cgeier is right, the exception will just be passed back through the call stack until it hits the catch in MethodA.

Also as @cgeier said, if MethodB and MethodC don't do anything other than return the value returned from the method they have called they are redundent (I assumed you would be doing other functions dependent of the returned values).

@cgeier, hmm not that I wanted to avoid to returning from D to C to B to A.. Maybe I should elaborate more. I wanted to get the error when I reach methodA from the error I have got in MethodD.. Sorry I didn't explain properly.

@ChrisHunter, thanks for the code. I have placed the try Catch in every method and managed to retrieve the error message of MethodD in MethodA when it returns.

Lastly, Thanks both for the help!

I'd probably just use a bool feild in the class to indicate an error which is set to true in MethodD. and check it before checking Method return value, and return before executing the rest of the Method code, to avoid unwanted recursion.

@Suzie999 I already had an existing return bool field which will return to indicate other information so for this error, I cannot return a bool to indicate the error message.

I'm not talking about a bool return from method, I mean a shared class field.

do you mean static data member or global data member?

psuedo class to demonstrate what I mean.

class someclass
    {
        private bool error = false;

        public bool MethodA()
        {
            bool Breturn = MethodB();
            if (error)
            {
                return false;
            }
            //rest of code
            return false;

        }

        public bool MethodB()
        {
            bool Creturn = MethodC();
            if (error)
            {
                return false;
            }
            //rest of code
            return false;

        }

        public bool MethodC()
        {
            bool Dreturn = MethodD();
            if (error)
            {
                return false;
            }
            //rest of code
            return false;

        }

        public bool MethodD()
        {
            // whatever code
            if (!whatever_code_is_what_it_should_be)
            {
                error = true;
                return false;
            }
            //rest of code
            return false;

        }


    }

Thanks for the explaination and suggestion, I understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.