简体   繁体   中英

C# How do I write to each part of my array?

I want to write a different 'if' statement for each part of my array. For example an if statement for the NHS number, date of birth, title, etc... They will all have different 'if' statements.

How do I write a separate if statement for all of them?

This is part of a system that will check the validity of the fields.

I have tried:

string[] NHSarray = new string[0];

I think this syntax is along the right lines of what I need to do, I just can't figure it out.

List<int> NHSnumber = new List<int>();
List<int> DateOfBirth = new List<int>();
List<int> Title = new List<int>();
List<int> GivenName = new List<int>();
List<int> Surname = new List<int>();
List<int> Gender = new List<int>();

using (var reader = new StreamReader(@"C:\Users\wy6282\Desktop\VS\nhsBatchChecker\Patients.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        //split the line
        string[] parts = line.Split(new[] { "Error" }, StringSplitOptions.None);

        //get valid integers
        NHSnumber.Add(GetInt(parts[0].Split(' ', '\'')));
        DateOfBirth.Add(GetInt(parts[1].Split(' ', '\'')));
        Title.Add(GetInt(parts[2].Split(' ', '\'')));
        GivenName.Add(GetInt(parts[3].Split(' ', '\'')));
        Surname.Add(GetInt(parts[4].Split(' ', '\'')));
        Gender.Add(GetInt(parts[5].Split(' ', '\'')));
    }
}

Assuming that your patient information always contains all the data listed as well as in the same order, I would map the split out data into an object, probably called Patient:

public class Patient
{
    public int NHSNumber {get; set;}
    public dateTime DateOfBirth {get; set;}
    public string Title {get; set;}
    public string GivenName {get; set;}
    public GenderOptions Gender {get; set;}
}

Create the enum GenderOptions:

public enum GenderOptions
{
    M,
    F,
    U
} 

Once you've got an object then validate it. You can do this in many ways but a simple method like:

public bool IsValidPatient(Patient patient)
{
    // setup guard clauses
    if(patient.NHSNumber < 10) return false;
    if(patient.DateOfBirth.Year < 1900) return false

    // more checks
}

Of course, if you want to display an error based on what went wrong then you can return some sort of status object rather than a bool, which contains a message field or something indicating the error. If these checks aren't specific to patients only then you might want to split them out into individual ones and group what you need into 1 method for validating a patient.

From your description, you are trying to read some input from a file line by line, split on some key (it looks like you're trying to use the word "Error" as your splitter, which seems odd), and then parse the content into useful data. Correct?

If the file you are reading from has a known format that doesn't change, basically you just need to access the correct elements of your "parts" array and assign the values. You are on the right track. I would suggest that if you know what the data type for each part of your data source is, that you use the appropriate conversion instead of trying to parse it manually.

You can use Integer.TryParse(string, out int) to ensure that invalid data doesn't cause an unhandled exception, but that requires an extra step.

int nhsNum;
if(!Integer.TryParse(part[0], out nhsNum) {
    // handle invalid number values here.
}
// at this point nhsNum will have the correctly parsed integer value.
NHSNumber.add(nhsNum)

You would repeat this for each element in the "part" array.

You can use DateTime.TryParse(String, out DateTime) if the date format in the file matches the date format of your system. However, to better handle globalization, if the file contains a pre-defined date format you can also use DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) and use the exact date format through the IFormatProvider.

One last note. Your "dateOfBirth" should probably be a List<DateTime> instead of a List<int>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM