简体   繁体   中英

How do i fix this error when calling my Method?

Am trying to call a method to the main method, how do i write it so that it does not display errors?

its mainly meant to prompt a user to enter a name and the age, i have tried to to put the method under the main method but it ain't working.

namespace Methodss
{
    class Program
    {
        static void Main(string[] args)
        {
            SayHi(String Name, int Age)
        }
        static void SayHi(String Name, int Age)
        {
            Name = Console.ReadLine();
            Console.WriteLine("Enter Your Name");
            Console.ReadKey();
            Age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Your Age");
            Console.ReadKey();
            Console.ReadLine();
            Console.WriteLine("Hello User"+Name+"you are"+ Age+" Years old");
        }
    } 
}

I expect the upon compiling the program to ask for name and age then output "Hello "Name" your "Age" years of age". where Name and Age are the Values from what the user inputs

Made a few Changes, from the suggestions, it worked namespace Methodss { class Program { static void Main(string[] args)

    {
        SayHi();
    }
    static void SayHi()
    {
        Console.WriteLine("Enter Your Name");
        var name=Console.ReadLine();
        Console.WriteLine("Enter Your Age");
        int age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Hello \t" + name + " you are " + age + " Years old");
        Console.ReadLine();
    }
}

}

This how i have redone it, its now working as expected

namespace Methodss
{
    class Program
    {
        static void Main(string[] args)
        {
            SayHi();
        }

        static void SayHi()
        {
            Console.WriteLine("Enter Your Name");
            var name=Console.ReadLine();
            Console.WriteLine("Enter Your Age");
            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Hello "+"\t" + name + " "+"you are"+" " + age + " "+" Years old");
            Console.ReadLine();
        }
    }
}

The main error is that you are confusing the list of actual parameters with the list of formal parameters. Formal parameters contain the information about the type of the parameter, while the actual parameter is a variable or a expression.

For example, the function MultiplyBy2() has a formal parameter of type double , but can be invoked with an expression or a variable.

  class Main {
    static double MultiplyBy2(double x)
    {
        return 2 * x;
    }

    static void Main(String[] args)
    {
        int x = 5;

        Console.WriteLine( MultiplyBy2( 6 ) );     // 12
        Console.WriteLine( MultiplyBy2( x ) );     // 10
    }
  }

Also, it seems you are interested on returning the name and the age, not actually passing them. My guess is that you would obtain the name and the age from SayHi() and actually say hello from Main() .

It is not trivial to return more than one value, so we can use out to indicate that these variables will be modified with the value set in the SayHi() function.

This also means that SayHi() is not the appropriate name for that function. Let's go with AskPersonalData() .

    class Program
    {
        static void Main(string[] args)
        {
            string name;
            int age;

            AskPersonalData( out name, out age );
            Console.WriteLine( "Hello User" + name + "you are" + age + " Years old" );
        }

        static void AskPersonalData(out string name, out int age)
        {
            Console.Write( "Enter Your Name: " );
            name = Console.ReadLine();

            Console.Write( "Enter Your Age: " );
            age = Convert.ToInt32( Console.ReadLine() );
        }
    } 

You can dig deeper about parameter passing in the MSDN .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM