Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t使用结构数组C的成员#_C#_.net - Fatal编程技术网

C# Can';t使用结构数组C的成员#

C# Can';t使用结构数组C的成员#,c#,.net,C#,.net,大家早上好,我对数组的成员有一个问题,即使我将结构声明为全局结构,也不能在函数中使用它。详细情况是,就像我想读取控制台输入的一些值,将它们存储在结构数组中,然后在控制台中再次读取并打印它们,但我无法使用函数中的成员,Visual Studio 2013社区给了我以下错误:错误1当前上下文中不存在名称“Students” 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.T

大家早上好,我对数组的成员有一个问题,即使我将结构声明为全局结构,也不能在函数中使用它。详细情况是,就像我想读取控制台输入的一些值,将它们存储在结构数组中,然后在控制台中再次读取并打印它们,但我无法使用函数中的成员,Visual Studio 2013社区给了我以下错误:错误1当前上下文中不存在名称“Students”

这是我的密码:

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

namespace Practica_modulo_4
{
    class Program
    {
        public struct student                                                               //Structure to hold the data of a student.
        {
            private string firstName, lastName, degree;                                     //Declaration of members of the structure.
            private DateTime birthDay;

            public string FirstName                                                         //Property of access to firstName.
            {
                get
                {
                    return firstName;
                }
                set
                {
                    firstName = value;
                }
            }

            public string LastName                                                          //Property of access to lastName.
            {
                get
                {
                    return lastName;
                }
                set
                {
                    lastName = value;
                }
            }
            public DateTime BirthDay                                                        //Property of access to birthday.
            {
                get
                {
                    return birthDay;
                }
                set
                {
                    birthDay = value;
                }
            }
            public string Degree                                                            //Property of access to degree.
            {
                get
                {
                    return degree;
                }
                set
                {
                    degree = value;
                }
            }
        }

        public static void readstudenData();

        static void Main(string[] args)
        {
            student[] Students = new student[5];
            readstudentData();                                                              //Reading of one student's information.
            printstudentData(Students[0].FirstName, Students[0].LastName, Students[0].BirthDay, Students[0].Degree);//Printing of
            //the information read at readstudentData().
        }


        public static void readstudentData()
        {

            Console.WriteLine("Please type the first name, last name, birthday (YYYY,DD,MM) and degree to obtain for the student\npressing Enter each time:");
            Students[0].FirstName = Console.ReadLine();                                     //Reading of the elements for the first student,
            Students[0].LastName = Console.ReadLine();                                      //note that I've used this form because of the
            Students[0].BirthDay = Convert.ToDateTime(Console.ReadLine());                  //instructions for the assignment, there are better
            Students[0].Degree = Console.ReadLine();                                        //ways to do the assignment for all de elements of the
                                                                                            //array but the text of the assignment explicitly says
                                                                                            //to not to publish that information because is part
                                                                                            //of the challenge, so please don't penalize me for
                                                                                            //this.
        }

        static void printstudentData(string firstName, string lastName, DateTime birthday, string degree)
        {
            Console.WriteLine("El estudiante {0} {1} nació el {2} y el grado que obtendrá es: {3}",firstName,lastName,Convert.ToString(birthday),degree);
            Console.ReadLine();
        }

}这是因为您在另一个函数的作用域内声明了它,这使它成为一个
局部变量

在所有函数之外声明该变量,使其成为全局变量,以便在其他函数中也可以使用它


希望这能澄清问题。

您需要移动
student[]Students=新学生[5]在Main之外,并声明为
静态

Students是Main内部的局部变量。因此,这超出了readstudentData的范围。这不是edx课程中的问题吗?谢谢你的回答,这非常有帮助。请将其标记为答案,以供其他人参考,并提供更多信息。谢谢你的回答,这非常有帮助。别忘了将解决问题的回答标记为答案。