Tris
Tris

Reputation: 169

Passing string array's through methods in c#

I'm very new to C#

I'm trying to create code that takes input from a user and saves it in an array in the SignUp() method and then if I call the Profile() method it displays the personal information

The error that occurs is when I try and call the Profile() method in the main line it says "There is no argument given that corresponds to the required formal parameter 'privinfo' of Profile(string[])' "

using System;

namespace SDD_Assessment_2
{
    class Program
    {
        static void Main(string[] args)
        {
            //SignUp();
            var privInfo = SignUp();
            Profile(privInfo);         
        }

        static string[] SignUp()
        {
            string[] privinfo = new string[4];
            Console.Write("First Name: ");
            privinfo[0] = Console.ReadLine();
            Console.Write("Last Name: ");
            privinfo[1] = Console.ReadLine();
            Console.Write("Email: ");
            privinfo[2] = Console.ReadLine();
            Console.Write("Password: ");
            privinfo[3] = Console.ReadLine();
            return privinfo;
        }
        static void Profile(string[] privinfo)
        {
            Console.WriteLine("Name: " + privinfo[0] + " " + privinfo[1]);
            Console.WriteLine("Email: " + privinfo[2]);
            Console.WriteLine("Password: " + privinfo[3]);
        }
    }
}

Upvotes: 3

Views: 725

Answers (1)

sagi
sagi

Reputation: 40481

You're not saving the value returned from SignUp(), so when the method ends, the local data is deleted. You need to catch the value, and pass it to Profile method, which expects an array of strings as an input.

static void Main(string[] args)
{
    var privInfo = SignUp();
    Profile(privInfo);
    ...
}

Upvotes: 3

Related Questions