简体   繁体   中英

how do you add get and set for an array in c#

I'm coding a func that inputs an array with different year of birth and prints out the oldest person. I'm trying to add a validation with get and set but my syntax is wrong.

enter image description here

TL;DR

Properties declaration part:

public class Employee
{
    private string _fullName;
    private int _yearIn;

    public string FullName
    {
        get => _fullName;
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                _fullName = value;
            }
        }

    }

    public int YearIn
    {
        get => _yearIn;
        set
        {
            if (value > 0 && value <= 2020)
            {
                _yearIn = YearIn;
            }
        }
    }
}

And a usage:

var employees = new List<Employee>();
for (int i = 0; i < 3; i++)
{
    Console.WriteLine("Enter Name:");
    string name = Console.ReadLine();

    Console.WriteLine("Enter Year:");
    int yearIn = Convert.ToInt32(Console.ReadLine());

    employees.Add(new Employee
    {
        FullName = name,
        YearIn = yearIn
    });
}

Update
You can do the same in a bit different manner though:

public class Employee
{
    private string _fullName;
    private int _yearIn;

    public bool IsNameValid { get; set; }
    public bool IsYearValid { get; set; }

    public string FullName
    {
        get => _fullName;
        set
        {
            _fullName = value;
            IsNameValid = string.IsNullOrEmpty(value);
        }

    }

    public int YearIn
    {
        get => _yearIn;
        set
        {
            _yearIn = value;
            IsYearValid = (value < 0) || (value > 2020);
        }
    }
}

And later:

Console.WriteLine($"Employee name is: {employees[i].IsNameValid}");
Console.WriteLine($"Employee year is: {employees[i].IsYearValid}");

Update 2
And the last alternative version is that you can use Validation attributes :

public class Employee
{
    [Required]
    [Range(0, 2020)]
    public int YearIn { get; set; }

    [Required]
    [StringLength(50)]
    public string FullName { get; set; }
}

later:

var empl = new Employee{ YearIn =  yearIn, FullName = name};

var context = new ValidationContext(empl, serviceProvider: null, items: null);
var results = new List<ValidationResult>();

var isValid = Validator.TryValidateObject(empl, context, results, true);
Console.WriteLine($"Is model valid: {isValid}");

if (isValid)
{
    employees.Add(new Employee
    {
        FullName = name,
        YearIn   = yearIn
    });
}

You can create wrapper classes over array and use indexer method for accessing the array item. There in, you can put all your validation logic.

class IntData
    {

        public IntData(int size)
        {
            data = new int[size];
        }
        // Array of temperature values
        private int[] data;

        public int this[int index]
        {
            get
            {
                return data[index];
            }

            set
            {
                // Do your validation here
                if (value < 5000) 
                {
                    data[index] = value;
                }
            }
        }
    }


      static void Main(string[] args)
        {
            IntData year = new IntData(3);
            year[0] = 2000;
            year[1] = 6000; // This value won't set because of validation
            year[2] = 4000;

            Console.Read();
        }

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