简体   繁体   English

调用方法时如何使用局部变量?

[英]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. 我正在尝试使用getStudentInfo方法获取用户输入,然后将其存储在变量中,并将其输入到printStudentInfo方法中以对其进行格式设置并将其写入控制台。

This code shouldn't compile and run at all. 这段代码根本不应该编译和运行。 You don't have a firstName, lastName, or birthday variable in scope. 您在范围内没有firstName,lastName或Birthday变量。 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. 如果要保留变量,则在方法之外声明它们,并以相同的方式分配它们,但不带'string'修饰符。 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 静态属性程序class.Read下创建距离这里大约属性创建静态属性来保存的价值和使用它,你在main()method.Note呼吁任何方法C#属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用我的方法时如何解决此错误? - How do i fix this error when calling my Method? 如何在ConstructorBuilder中定义局部变量? - How do I define local variables in a ConstructorBuilder? 使用Moq,你如何模拟使用局部变量的方法 - Using Moq, How do you Mock a method that uses local variables 使用BeginInvoke方法时如何确保局部变量的正确状态 - How to ensure the correct state of local variables when using BeginInvoke method 在C#中调用DrawRectangle方法时如何使用XOR模式 - How can I use XOR mode when calling DrawRectangle method in C# System.ArgumentOutOfRangeException 调用方法时。 如何找出错误发生的位置? - System.ArgumentOutOfRangeException when calling Method. How do I find out where the error occurs? 在非托管DLL中调用方法时,如何保护C#应用程序免于崩溃? - How do I protect my C# app from crashing when calling a method in an unmanaged DLL? 调用tableAdapter的Fill()方法时,如何防止selectedValue更改? - How do I prevent selectedValue altering when calling tableAdapter's Fill() method? 如何在调用方法时验证是否引发了操作(使用Moq进行单元测试) - How do I verify that an action is raised when calling a method (unit testing with Moq) 如何在另一个方法中的一个方法中使用局部变量? - How can I use a local variable in a method from another method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM