1

So Im learning basic C# and wonders how I can return x and y from AGE(); and NUMBER(); and use those variables in Name();

Right now x and y in the Name(); parameters are understandably wrong since there are no local variables in the constructor. But I returned x and y from the function. So how can I use them in the Name function?

namespace CSharp_001
{
    class Program
    {
        public Program()
        {
            Name(x, y);
            AGE();
            NUMBER();
        }
        public int AGE()
        {
            int x;
            Console.WriteLine(" And enter age: ");
            x = Console.Read();
            return x;
        }
        public int NUMBER()
        {
            int y;
            Console.WriteLine(" And favorite number: ");
            y = Console.Read();
            return y;
        }
        public void Name(int x, int y)
        {
            Console.WriteLine("Enter your name: ");
            string test = Console.ReadLine();
            Console.WriteLine("Hi " + test);
        }
        static void Main(string[] args)
        {
            new Program();
            Console.ReadLine();
        }
    }
}
3
  • Where are the x and y in Program() declared? I would also recommend tweaking your formatting of the code a tiny bit. The way it is now isn't all too consistent with C#. Commented Aug 12, 2015 at 18:03
  • You don't use x or y in the Name method, so why bother passing them in? Commented Aug 12, 2015 at 18:06
  • 2
    Console.Read is the wrong function! Commented Aug 12, 2015 at 18:08

4 Answers 4

2

In c#, the Type of a value is really important. When you read from the console, you get a string. However, your Age() and NUMBER() functions return ints. A function that is declared to return an int cannot return a string. You need to convert those values to int before you can return them:

public int AGE() {
    Console.WriteLine(" And enter age: ");
    return int.Parse(Console.Read());
}

After you fix both functions, you can call Name() like this:

public Program()
{
    int x = AGE();
    int y = NUMBER();
    Name(x, y);
}

or like this:

public Program()
{
    Name(AGE(), NUMBER());
}

In either case, if you want to pass those values to the Name() function, the calls to AGE() and NUMBER() must be resolved before the the call to Name(). The console text indicates you want the prompts in Name() to come first. In that case, you might do this:

public void Name()
{
    Console.WriteLine("Enter your name: ");
    string test = Console.ReadLine();
    Console.WriteLine("Hi " + test);
    int age = AGE();
    int number = NUMBER();
}

public Program()
{
    Name();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Console.Read returns an int - the ASCII code of the character pressed.
Oops.. . I was thinking ReadLine()
0

For educational purposes:

public Program()
{
    var age = AGE();
    var num = NUMBER(); 
    Name(age, num);
}

Comments

0

When a function returns a value, you need to store it or use it immediately. If you look at your example, in the AGE method, you have

    x = Console.Read();

Console.Read is returning the value you're storing in x. But this x is local to the AGE method, nothing else called x will have that value (its scope is local to the AGE method). The AGE method returns this value, but that doesn't mean you can use the variable x anywhere else.

When you call AGE in the Program constructor, you need to save its return value. If you have that value saved, then you can pass it in as an argument to Name.

    int age = AGE();
    Name(age, y);

(assuming y is set by your call to NUMBER). When Name is called, you'll be able to use x, and it will have the value you passed in (which above, is called age).

Comments

0

A few comments:

  • Console.Read only read one character and returns the UTF-16 code of that character - you should use Console.ReadLine instead and parse the results if necessary.
  • Functions should be named to describe what they do, so GetAge and GetName would be more appropriate function names
  • Constructors should only be used to initialize the object to a usable state. Your code should either be in the Main function or in a separate function of Program that Main calls.
  • variables should have meaningful names. x and y do not give any indication of what they represent.

So something like this would be more appropriate:

class Program
{
    public void Run()
    {
        int age = GetAge();
        int number = GetNumber();
        GetName(age, number);
    }
    public int GetAge(){
        string age;
        Console.WriteLine(" And enter age: ");
        age = Console.ReadLine();
        return int.Parse(x); // or TryParse and loop until the parse succeeds
    }
    public int GetNumber(){
        string number;
        Console.WriteLine(" And favorite number: ");
        number = Console.ReadLine();
        return int.Parse(number); // or TryParse and loop until the parse succeeds

    }
    public void Name(int x, int y)
    {
        Console.WriteLine("Enter your name: ");
        string test = Console.ReadLine();
        Console.WriteLine("Hi " + test);
    }
    static void Main(string[] args) {
        new Program().Run();
        Console.ReadLine();
    }
}

1 Comment

Console.Read returns the next UTF-16 code unit of the characters in the input stream after they are converted from the input stream encoding to UTF-16. (Or, -1 if there are no more code units.) For example, if you read twice with "𤑭" as the input then you get 55377, 56429.

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.