Reputation: 33813
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();
}
Upvotes: 1
Views: 5900
Reputation: 2058
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.
Upvotes: 0
Reputation: 11577
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;
}
Upvotes: 1