Skip to content

Instantly share code, notes, and snippets.

@Sheepings
Last active November 27, 2019 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sheepings/72b592196ba5b1c2ca5c2c17d5366191 to your computer and use it in GitHub Desktop.
Save Sheepings/72b592196ba5b1c2ca5c2c17d5366191 to your computer and use it in GitHub Desktop.
This class creates a class of persons which is read from a text file. You can search the class for specific persons also.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static readonly string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "mytext.txt");
private void Button1_Click(object sender, EventArgs e)
{
using (FileStream reader = File.OpenRead(path))
using (TextFieldParser iParse = new TextFieldParser(reader))
{
iParse.Delimiters = new[] { "," };
do
{
string[] fields = iParse.ReadFields();
RegisteredPerson registeredPerson = new RegisteredPerson(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]);
RegisteredPerson.IEnum.Add(registeredPerson);
}
while (!iParse.EndOfData);
}
}
private void button2_Click(object sender, EventArgs e)
{
RegisteredPerson personFound = RegisteredPerson.FindPerson("Antanas");
/* Do things with the persons info */
if (personFound != null)
MessageBox.Show($"Persons Address is : {personFound.Address}");
else MessageBox.Show("No person found");
}
}
public class RegisteredPerson
{
public static List<RegisteredPerson> IEnum = new List<RegisteredPerson>();
public RegisteredPerson(string firstName, string lastName, string gender, string email, string address, string dateOfBirth)
{
FirstName = firstName;
LastName = lastName;
Gender = gender;
Email = email;
Address = address;
DateOfBirth = dateOfBirth;
}
public static RegisteredPerson FindPerson(string param)
{
using (List<RegisteredPerson>.Enumerator Person = IEnum.GetEnumerator())
{
while (Person.MoveNext())
{
if (Person.Current.FirstName.Equals(param))
{
return Person.Current;
}
}
}
return null;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string DateOfBirth { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment