无法从C#中的方法中检索信息?

时间:2013-02-05 00:44:10

标签: c#

class Program
{
    public static void GetSchoolInfo()
    {
        string SchoolName,
               EnrollmentStatus,
               ColorOne,
               ColorTwo;

        Console.WriteLine("Please enter your school name: ");
        SchoolName = Console.ReadLine();
        Console.WriteLine("Please enter your Enrollment Status: ");
        EnrollmentStatus = Console.ReadLine();
        Console.WriteLine("Pleas enter one of your school's colors: ");
        ColorOne = Console.ReadLine();
        Console.WriteLine("Please enter the other school color: ");
        ColorTwo = Console.ReadLine();
    }
    static void Main(string[] args)
    {
        GetSchoolInfo();

        Console.ReadLine();

    }

我的目的是创建一个方法来获取这些字符串然后很好地输出它们。 我似乎无法在方法之外调用SchoolName字符串或任何其他字符串。我目前正在学校为我的Bach in C.S.所以请原谅我,因为这是一个新手问题。

6 个答案:

答案 0 :(得分:2)

而不是返回void返回一个可以在main中使用的类的新实例:

        public class SchoolInfo
        {
            public string SchoolName { get; set; }
            public string EnrollmentStatus { get; set; }
            public string ColorOne { get; set; }
            public string ColorTwo { get; set; }
        }

        public static SchoolInfo GetSchoolInfo()
        {
            string SchoolName,
                   EnrollmentStatus,
                   ColorOne,
                   ColorTwo;

            Console.WriteLine("Please enter your school name: ");
            SchoolName = Console.ReadLine();
            Console.WriteLine("Please enter your Enrollment Status: ");
            EnrollmentStatus = Console.ReadLine();
            Console.WriteLine("Pleas enter one of your school's colors: ");
            ColorOne = Console.ReadLine();
            Console.WriteLine("Please enter the other school color: ");
            ColorTwo = Console.ReadLine();

            return new SchoolInfo()
                {
                    SchoolName = SchoolName,
                    EnrollmentStatus = EnrollmentStatus,
                    ColorOne = ColorOne,
                    ColorTwo = ColorTwo
                };
        }


        private static void Main(string[] args)
        {
            SchoolInfo info = GetSchoolInfo();
            Console.WriteLine("You Entered School Name: "+info.SchoolName);
            Console.ReadLine();

        }

答案 1 :(得分:1)

这是因为它们的范围。范围由定义变量的位置确定,它们在方法内部的示例中定义。这就是他们的范围所在。方法快速超出范围,因此,它们的局部变量也是如此。如果要在类之外访问这些变量,则应将它们定义为类变量,如下所示:

class Program
{
    string SchoolName,
           EnrollmentStatus,
           ColorOne,
           ColorTwo;
public static void GetSchoolInfo()
{

    Console.WriteLine("Please enter your school name: ");
    SchoolName = Console.ReadLine();
    Console.WriteLine("Please enter your Enrollment Status: ");
    EnrollmentStatus = Console.ReadLine();
    Console.WriteLine("Pleas enter one of your school's colors: ");
    ColorOne = Console.ReadLine();
    Console.WriteLine("Please enter the other school color: ");
    ColorTwo = Console.ReadLine();
}
static void Main(string[] args)
{
    GetSchoolInfo();

    Console.ReadLine();

}

答案 2 :(得分:1)

正如@Adam Maras所提到的那样,您正在尝试访问不在您范围内的变量。在函数中声明变量时,它会在该函数中生存和死亡。这意味着您将无法再从该功能外部访问它。

现在的解决方案;创建一个类来保存变量并用于跨主应用程序。

以下是演示。

这些是编程的基础,知道基础知识将带你走很长的路。祝你好运

public class SchoolInfo
{
    /* two properties are defined as public to carry the data */
    public string NameOfTheSchool { get; set; }
    public string AddressOfTheAchool { get; set; }
}


class Program
{
    public static void GetSchoolInfo(SchoolInfo info)
    {
        info.NameOfTheSchool = "AA";
        info.AddressOfTheAchool = "BB";
    }

    static void Main()
    {
        // create the instance of the class
        SchoolInfo mySchoolInfo = new SchoolInfo();

        // pass it into GetSchoolInfo to collect the data
        GetSchoolInfo(mySchoolInfo);

        // print the value of the name of the school
        Console.WriteLine(mySchoolInfo.NameOfTheSchool);

        Console.ReadKey();
    }
}

答案 3 :(得分:0)

您无法在GetSchoolInfo方法之外访问这些变量的原因是它们是局部变量,因此在该方法之外的任何位置都不在scope之内。关于这个问题,我read up

答案 4 :(得分:0)

该方法被声明为void函数。由于变量SchoolNameEnrollmentStatusColorOneColorTwo是局部变量且未返回,因此您无法在GetSchoolInfo()函数之外访问它们。您可以将它们声明为全局变量,也可以创建Collection并从函数中返回它们。我更喜欢选项2,但这取决于你。

答案 5 :(得分:0)

您的GetSchoolnfo()方法中定义的变量是该方法的本地变量,这意味着它们只能在该方法中访问。您可以将它们定义为类级别变量而不是方法,以便其他方法也可以访问它们。

class Program
{
    private String SchoolName;
    private String EnrollmentStatus;
    private String ColorOne;
    private String ColorTwo;

    public static void GetSchoolInfo()
    {

        Console.WriteLine("Please enter your school name: ");
        SchoolName = Console.ReadLine();
        Console.WriteLine("Please enter your Enrollment Status: ");
        EnrollmentStatus = Console.ReadLine();
        Console.WriteLine("Pleas enter one of your school's colors: ");
        ColorOne = Console.ReadLine();
        Console.WriteLine("Please enter the other school color: ");
        ColorTwo = Console.ReadLine();
    }
    static void Main(string[] args)
    {
        GetSchoolInfo();

        Console.ReadLine();

    }