We help IT Professionals succeed at work.

Why is my return firing more than once (and changing my var value..) (Easy points...)

OddiC
OddiC asked
on
245 Views
Last Modified: 2013-11-05
Ok,

I know the answer is simple- I am obviously not referencing/assigning the variables in my function correctly. The following function is used to ensure a user selects one of the available console options- it works recursively to alert user to enter another value if he/she doesn't select an available option. This function works for me when the user has only made one failed entry.. however if they fail entries on the 1st two attempts, all subsequent attempts are not capturing the entered value (but rather a preceeding value they entered). Much appreciation to anyone who can help me with this. The code:

   public static string ValidateKey(string select)
        {
            if (select != "A" && select != "E" && select != "P" && select != "D")
            {
                Console.WriteLine("-----------------Wrong!----------------");
                select = Convert.ToString(Console.ReadKey().Key);
                ValidateKey(select);
            }

            return select;
        }
Comment
Watch Question

Commented:
If I follow it correctly, I think you actually want:

return ValidateKey(select);
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Works perfect. Thanks Justin W.. much appreciated. Do you know why my return variable (and the return fire) was behaving in the way I described with my original code?

Commented:
You're welcome.

>> Do you know why my return variable (and the return fire) was behaving in the way I described with my original code?
Yes, because calling the method recursively doesn't modify the variable. The parameter was passed by value not by reference. This would also have worked, but it's slower and more prone to errors (as you discovered):

   public static string ValidateKey(string select)
        {
            if (select != "A" && select != "E" && select != "P" && select != "D")
            {
                Console.WriteLine("-----------------Wrong!----------------");
                select = Convert.ToString(Console.ReadKey().Key);
                select = ValidateKey(select);
            }

            return select;
        }

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.