简体   繁体   中英

How do I use local variables when calling a method?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsExceptions2
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInformation();
            PrintStudentDetails(firstName, lastName,birthDay);
            Console.ReadKey();
        }

        static void GetStudentInformation()
        {
            Console.WriteLine("Enter the student's first name: ");
            string firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name");
            string lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday");
            string birthDay = Console.ReadLine();
        }

        static void PrintStudentDetails(string first, string last, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
        }
    }
}

How do I input these values in my method call? When I run the program, the line comes up blank in the variable spots. I am trying to get the user input with the getStudentInfo method, and then store it in the variables, and input it into the printStudentInfo method to format it and write it to the console.

This code shouldn't compile and run at all. You don't have a firstName, lastName, or birthday variable in scope. What editor are you using to write this in?

If you want to keep the variables then declare them outside of the methods and assign them the same way but without the 'string' modifier. Like this...

class Program
{
    static string firstName;
    static string lastName;
    static string birthday;

    static void Main(string[] args)
    {
        GetStudentInformation();
        PrintStudentDetails(firstName, lastName, birthday);
        Console.ReadKey();
    }

    static void GetStudentInformation()
    {
        Console.WriteLine("Enter the student's first name: ");
        firstName = Console.ReadLine();
        Console.WriteLine("Enter the student's last name");
        lastName = Console.ReadLine();
        Console.WriteLine("Enter the student's birthday");
        birthday = Console.ReadLine();
    }

    static void PrintStudentDetails(string first, string last, string birthday)
    {
        Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var userInputs = GetStudentInformation();
        PrintStudentDetails(userInputs);
        Console.ReadKey();
    }

    static Tuple<string, string, string> GetStudentInformation()
    {
        Console.WriteLine("Enter the student's first name: ");
        string firstName = Console.ReadLine();
        Console.WriteLine("Enter the student's last name");
        string lastName = Console.ReadLine();
        Console.WriteLine("Enter the student's birthday");
        string birthDay = Console.ReadLine();
        return new Tuple<string, string, string>(firstName, lastName, birthDay);
    }

    static void PrintStudentDetails(Tuple<string, string, string> userInputs)
    {
        Console.WriteLine("{0} {1} was born on: {2}", userInputs.Item1, userInputs.Item2, userInputs.Item3);
    }
}

Make this changes you should be able to get what you want.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsExceptions2
{
  class Program
{
    public static string firstName { get; set; }
    public static string lastName { get; set; }
    public static string birthDay { get; set; }


    static void Main(string[] args)
    {

        GetStudentInformation();
        PrintStudentDetails(firstName, lastName, birthDay);
        Console.ReadKey();
    }

    private static void PrintStudentDetails(string firstName, object lastName, object birthDay)
    {
        Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthDay);
    }

    private static void GetStudentInformation()
    {
        Console.WriteLine("Enter the student's first name: ");
        firstName = Console.ReadLine();
        Console.WriteLine("Enter the student's last name");
        lastName = Console.ReadLine();
        Console.WriteLine("Enter the student's birthday");
        birthDay = Console.ReadLine();

    }



}
}

Create static properties to hold the value and use it any method you are calling in the Main() method.Note that static properties are created under program class.Read about properties from here C# Properties

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