Reputation: 101
Good morning everyone, I've a problem with the members of the array to which I can't use inside a function even when I declared the structure as global. The detail is that like I wanna read some values entered by console, store them in the array of structures an then read them an print them again in console but I can't use the members inside the functions, Visual Studio 2013 community gives me the following error: Error 1 The name 'Students' does not exist in the current context.
Here is my code:
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();
}
}
Upvotes: 0
Views: 119
Reputation: 4004
You need to movestudent[] Students = new student[5];
outside of Main and declare as static
Upvotes: 2
Reputation: 3204
This is because you have declared it inside the scope of another function, which makes it a local
variable.
Declare that variable outside your all functions to make it global
so that you can use it inside the other functions as well.
Hope this clears it.
Upvotes: 8