简体   繁体   English

如何在 C# 的 LIST<> 中动态获取用户输入的多个字段,而不像我在下面的代码中那样进行硬编码?

[英]How to take user input of multiples fields in LIST<> in C# dynamically not hard coded like i did the code below?

using System;
using System.Collections.Generic;

namespace Branches
{
    public class Branch
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }  

        public Branch() { }

        public Branch(int id, string code, string name)
        {
            Id = id;
            Code = code;
            Name = name;
        }
    }

    class Program
    {
        static void Main()
        {
            List<Branch> br = new List<Branch>(){
                new Branch(){ Id=401, Code="SBI120800", Name="Chembur West" },
                new Branch(){ Id=402, Code="SBI120700", Name="Chembur East" },
                new Branch(){ Id=403, Code="SBI120900", Name="Govandi West" },
                new Branch(){ Id=404, Code="SBI120500", Name="Govandi East" },
                new Branch(){ Id=405, Code="SBI120400", Name="Andheri West" },
                new Branch(){ Id=406, Code="SBI120300", Name="Andheri East" },
            };

            foreach (var branches  in br)
            {
                Console.WriteLine(branches.Name);
            }
        }
    }
}

You could do something like this:你可以这样做:

List<Branch> br = new List<Branch>();
while(true) 
{
 Console.WriteLine("Please provide Id")
 var id =  Console.ReadLine();
 ... // Ask other questions 
 br.Add( new Branch { Id =id, ...});
 Console.WriteLine("Would you like to add another branch? Y/N");
 if(Console.ReadKey().ToLower() == 'n') break;
}

A snippet that trims spaces, uses commas as delimiters between individual fields and newliness between different Branch objects, checks for duplicate IDs, codes, and names, and checks the code format according to the format I saw in your above code ( SBIxxxxxx ):修剪空格的片段,使用逗号作为各个字段之间的分隔符和不同分支对象之间的换行符,检查重复的 ID、代码和名称,并根据我在上面的代码中看到的格式检查代码格式( SBIxxxxxx ):

// This requires LINQ.
List<Branch> br = new List<Branch>(); // You can replace 'new List<Branch>()' with 'new()' if you're using C# 9.0 or newer.
while (true)
{
    Console.WriteLine("Enter the ID, code, and name of the branch, seperated by commas, or enter an empty string to finish:");
    string line = Console.ReadLine();
    if (line == String.Empty)
        break;
    string[] entry = line.Split(',');
    entry[0] = entry[0].Trim();
    entry[1] = entry[1].Trim();
    entry[2] = entry[2].Trim();
    entry[1] = entry[1].ToUpper();
    if (entry.Length != 3)
        Console.WriteLine("Incorrect number of arguments.");
    else if (!Int32.TryParse(entry[0], out int id))
        Console.WriteLine("Invalid ID entered.");
    else if (entry[2] == String.Empty)
        Console.WriteLine("Name must not be blank.");
    else if (!(entry[1].StartsWith("SBI") && Int32.TryParse(entry[1].Remove(0, 3), out int code) && code >= 100000 && code <= 999999))
        Console.WriteLine("Invalid code format.");
    else if ((from e in br
                where ((id == e.Id)
                    || (entry[1] == e.Code)
                    || (entry[2] == e.Name))
                select e).Count() > 0)
        Console.WriteLine("Duplicate ID, code, or name entered.");
    else
        br.Add(new Branch(id, entry[1], entry[2])); // Again, you can replace 'new Branch()' with 'new()' if you're using C# 9.0 or newer.
}
foreach (Branch b in br)
    Console.WriteLine(b.Name);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

If you find bugs with this code, please report them.如果您发现此代码存在错误,请报告它们。

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

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