1

I have a problem when use recursive method call.
I know the problem here setInfo(ref name, ref age, ref address, ref country);, but I don't know how to fix it.

class person{
        private string name;
        private short age;
        private string address;
        private string country;

        public person(){
            Console.Write("Hi i'm constructor \n\n");
        }

        public void setInfo(ref string name, ref short age, ref string address, ref string country){
            if (name != "" || address != "" || country != "" || age != 0) {
                this.name = name;
                this.age = age;
                this.address = address;
                this.country = country;
            } else {
                setInfo(ref name, ref age, ref address, ref country); // Here is what I doubt.
            }
        }

        public void getInfo(){
            Console.Clear();
            Console.WriteLine("---- The information ----\nName: {0}\nAge: {1}\nAddress: {2}\nCountry: {3}", this.name, this.age, this.address, this.country);
        }

    }


// When usage


static void Main(string[] args){
            string name, address, country;
            short age; 

            person one = new person();

            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.Write("Age: ");
            Int16.TryParse(Console.ReadLine(), out age);

            Console.Write("Address: ");
            address = Console.ReadLine();

            Console.Write("Country: ");
            country = Console.ReadLine();

            one.setInfo(ref name, ref age, ref address, ref country);
            one.getInfo();
        }
4
  • 1
    If the code enters the else it will never leave the recursive call since you don't change any values there. Commented Sep 17, 2013 at 11:52
  • 1
    What do you expect to happen when all parameters are empty? It's clear what's the problem but what is not clear is what you're trying to do :) Commented Sep 17, 2013 at 11:52
  • You shouldn't use the ref keyword for passing parameters. All objects in C# (including String) are reference types and are passed around by reference. Using the ref keyword in C# is rather unusual and indicates design issues in most cases. And since you know you have a recursive call, why not remove it? What did you intend to do with this call anyway? Commented Sep 17, 2013 at 11:58
  • @Groo: Thanks, but I'm still beginner in C#, and I still do not know all the rules of the language. Commented Sep 17, 2013 at 12:04

2 Answers 2

1

in setInfo you check if the values are "" and if they are you call the method again without changing nothing.

in this case you call the setInfo over and over until the stack is full and then the exception is thrown

if you gonna call setInfo you need to change the values... or else you just stuck in an infinite calls to setInfo

for example, maybe give default values:

    public void setInfo(ref string name, ref short age, ref string address, ref string country){
            this.name    = name.Equals("")    == false? name : "abe";
            this.address = address.Equals("") == false ? address : "hevean";
            this.country = country.Equals("") == false ? country : "USA";
            this.age     = age > 0 ? age : 18;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I understood now, when call the method again will take the parameters without values, and I must set default value. Are my words correctly ?
@LionKing i think so. as long as you understood that calling the method again with the same parameters as the ones you got will get you nowhere, then you got it.
0

Based on your code you don't want to do recursion there.

You get the stack overflow because you make a recursive call without changing any data, which is what caused the recursive call in the first place. You need to allow the person to re-enter the missing data. Does that make sense?

Try wrapping your input with a do/while loop. This works:

    static void Main(string[] args)
    {
        string name, address, country;
        short age;

        person one = new person();

        do
        {
            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.Write("Age: ");
            Int16.TryParse(Console.ReadLine(), out age);

            Console.Write("Address: ");
            address = Console.ReadLine();

            Console.Write("Country: ");
            country = Console.ReadLine();
        } while (name == "" || address == "" || country == "" || age < 1);

        one.setInfo(ref name, ref age, ref address, ref country);
        one.getInfo();

        Console.ReadKey();
    }

A couple points:

1) You might want to get rid of the ref's since you are not changing any data in your setInfo. The only reason you would use ref is to return the changed values to the caller.

2) You should probably create a constructor that takes all four values and construct your person after you collect your data.

Hope that helps. Cheers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.