Dorothy Santos

Dorothy Santos

  • NA
  • 4
  • 649

Struct Array to and make list out of it

Dec 2 2017 1:02 AM
Hi, I really need help with this one, I've been trying to create a list from struct array that contains the data that i wanted to add in the list. I've been trying but all I am getting was null and/or 0 values when i print my list.
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace List
{
public enum gender { Female, Male }
class Program
{
public struct Patient
{
public string _Name;
public int _Age;
public gender _Sex;
}
static void Main(string[] args)
{
string g;
string choice;
bool quit = false;
Console.WriteLine("\t\tPatient Records");
Patient[] patient = new Patient[10];
List<Patient> info =new List<Patient>();
int i = 0;
while (!quit)
{
Console.WriteLine("\na. Add");
Console.WriteLine("d. Display");
Console.WriteLine("q. Quit ");
Console.Write("\nYour selection: ");
choice = Console.ReadLine();
switch (choice)
{
case "a":
{
bool notNum;
Console.Write("\nPlease enter your name: ");
patient[i]._Name = Console.ReadLine();
do
{
Console.Write("Please enter your age: ");
notNum = int.TryParse(Console.ReadLine(), out patient[i]._Age);
if (!notNum)
{
Console.WriteLine("Invalid entry.");
}
}
while (!notNum);
do
{
Console.Write("Please enter your gender [F/M]: ");
g = Console.ReadLine();
if ((g == "F") || (g == "f"))
{
patient[i]._Sex = gender.Female;
}
if ((g == "M") || (g == "m"))
{
patient[i]._Sex = gender.Male;
}
}
while ((g != "M" && g != "m") && (g != "F" && g != "f"));
i++;
Console.Clear();
break;
}
case "d":
{
Console.Clear();
Console.WriteLine("\nList of patient records...");
for (int j = 0; j < i; j++)
{
Console.WriteLine("\nPatient name: {0}", patient[j]._Name);
Console.WriteLine("Patient age: {0}", patient[j]._Age);
Console.WriteLine("Patient sex: {0}", patient[j]._Sex);
}
break;
}
case "q":
{
quit = true;
break;
}
}
}
}
}
}
above is my code and i wanted to add every data stored in that struct array to my list. How do I do that? I am a newbie to programming. Please help. 

Answers (3)