大家早上好,即使我将结构声明为全局,我也无法在函数内部使用数组成员.细节是,我想读取控制台输入的一些值,将它们存储在结构数组中然后读取它们再次在控制台中打印它们但我不能使用函数内部的成员,Visual Studio 2013社区给了我跟随错误:错误1当前上下文中不存在名称"学生".
这是我的代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
}