Can you please help me? I'm trying to add values to a list I've made and print them to screen. Unfortunately something seems to be wrong on my code. Forgive me if I'm totally wrong but I need some help over here. Thank you very much in advance for your help.
My code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace genericTypes
{
class Student
{
private string id;
private string FName;
private string LName;
private string ParentID;
private string Grade;
private int Group;
public Student(string id, string FName, string LName, string ParentID, string Grade, int Group)
{
// TODO: Complete member initialization
this.id = ID;
this.FName = fName;
this.LName = lName;
this.ParentID = parentID;
this.Grade = grade;
this.Group = group;
}
public string ID { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string parentID { get; set; }
public string grade { get; set; }
public int group { get; set; }
}
/* class Parent
{
public string ID { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string job { get; set; }
} */
class Program
{
static void Main(string[] args)
{
bool finish = true;
string id = "0";
string FName = "";
string LName = "";
string ParentID = "";
string Grade = "";
int Group = 0, stdNumber = 0;
//char answer = 'Y';
List
do
{
Console.WriteLine("Insert student's personal info");
Console.Write("Student ID: ");
id = Console.ReadLine();
Console.Write("First name: ");
FName = Console.ReadLine();
Console.Write("Last name: ");
LName = Console.ReadLine();
Console.Write("Parent ID: ");
ParentID = Console.ReadLine();
Console.Write("Grade: ");
Grade = Console.ReadLine();
Console.Write("Group: ");
Group = int.Parse(Console.ReadLine());
myStudentList.Add(new Student(id, FName, LName, ParentID, Grade, Group));
Console.Write("Do you want to continue input? (Y/N): ");
ConsoleKeyInfo answer = Console.ReadKey();
Console.WriteLine();
finish = (answer.Key == ConsoleKey.Y) ? false : true;
} while (finish == false);
foreach (Student std in myStudentList)
{
stdNumber++;
Console.WriteLine("Info for student number {0}", stdNumber.ToString());
Console.WriteLine("Student ID: {0}", std.ID);
Console.WriteLine("First name: {0}", std.fName);
Console.WriteLine("Last name: {0}", std.lName);
Console.WriteLine("Parent ID: {0}", std.parentID);
Console.WriteLine("Grade: {0}", std.grade);
Console.WriteLine("Group: {0}", std.group.ToString());
Console.WriteLine("*******************************");
}
}
}
}

VulpesPosted Mar 22, 2015, 12:57 PM
1. The arguments to the Student constructor are not actually being used to set anything. What you're doing instead is to set the private fields to the values of the public automatic properties which will still have their default values of null (0 in the case of group).
2. When you attempt to write the values of the automatic properties for a particular Student object to the console they will still be null or zero.
3. The C# compiler automatically generates backing fields for automatic properties - you don't need to supply your own and, if you do, they will be completely independent of the compiler generated ones.
So, if you change your Student class to this, then all should be well:
class Student
Iraklis MarkelisPosted Mar 22, 2015, 1:09 PM