简体   繁体   English

C#如何写入数组的每个部分?

[英]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. 我想为数组的每个部分编写一个不同的“ if”语句。 For example an if statement for the NHS number, date of birth, title, etc... They will all have different 'if' statements. 例如,有关NHS编号,出生日期,职称等的if语句。它们都将具有不同的'if'语句。

How do I write a separate if statement for all of them? 如何为所有这些单独编写if语句?

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: 创建枚举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. 根据您的描述,您试图逐行读取文件中的某些输入,并按某个键进行拆分(看起来您试图将单词“ Error”用作拆分器,这似乎很奇怪),然后解析内容转化为有用的数据。 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. 您可以使用Integer.TryParse(string, out int)来确保无效数据不会导致未处理的异常,但这需要额外的步骤。

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. 您将对“ part”数组中的每个元素重复此操作。

You can use DateTime.TryParse(String, out DateTime) if the date format in the file matches the date format of your system. 如果文件中的日期格式与系统的日期格式匹配,则可以使用DateTime.TryParse(String, out DateTime) 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. 但是,为了更好地处理全球化,如果文件包含预定义的日期格式,则还可以使用DateTime.TryParseExact(String,String,IFormatProvider,DateTimeStyles,DateTime)并通过IFormatProvider使用确切的日期格式。

One last note. 最后一点。 Your "dateOfBirth" should probably be a List<DateTime> instead of a List<int> 您的“ dateOfBirth”可能应该是一个List<DateTime>而不是一个List<int>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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