向List添加约束<> [英] Adding a constraint to a List<>

查看:114
本文介绍了向List添加约束<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道如何为List添加限制<> ;.

我想要做的是有限的List< employee>可以在班级团队中。



 class Team 
{
public virtual String Name {get;组; }
[Display(Name =Project Title)]
public String ProjectTitle {get;组; }
public List< Employee>员工{get;组; }
public DateTime DateCreated {get;组; }
}







所以每个团队最多可以拥有说8个人。如何为List<>设置限制/约束?可以将多少员工添加到一个团队?也许有多种方法可以做到,建议最有效的方法。

这些实体将是数据库中的表,仅举几例。

解决方案

我只是让我的'团队类继承自列表与LT;员工>并为列表的添加方法定义我自己的处理程序:

  public   class 团队:列表< employee> 
{
public int MaxEmployees {获得; private set ; }

public virtual String 名称{获取; private set ; }

[显示(名称= 项目标题)]
public 字符串 ProjectTitle { get ; private set ; }

public DateTime DateCreated { get ; private set ; }

public 团队(字符串名称, string title, int maxEmployees)
{
Name = name;
ProjectTitle = title;
DateCreated = DateTime.Now;
MaxEmployees = maxEmployees;
}

// 注意使用'new,not'覆盖.. 。
public new void 添加(员工员工)
{
if this 。 Count == MaxEmployees)
{
throw new ArgumentOutOfRangeException( 达到团队的最大员工人数);
}

base .Add(employee);
}
} < / employee >


查看此链接

使用数据注释提供解决方案



http://stackoverflow.com/questions/5146732/viewmodel-验证列表 [ ^ ]

Hi people, I want to find out on how I can add a limit to the List<>.
What I want to do is have a limited List<employee> that can be inside a Class Team.

class Team
{
    public virtual String Name { get; set; }
    [Display(Name = "Project Title")]
    public String ProjectTitle { get; set; }
    public List<Employee> Employees { get; set; }
    public DateTime DateCreated { get; set; }
}




So each Team can have a maximum of lets say 8 people. How can I set a limit/constraint to a List<> on how many Employees can be added to one team? Maybe there are multiple ways of doing it, suggest the most effective way.
These entities will be tables in the data base, just to mention.

解决方案

I'd simply have my 'Team class inherit from List<Employee> and define my own handler for the 'Add method for the List:

public class Team : List<employee>
{
    public int MaxEmployees { get; private set; }

    public virtual String Name { get; private set; }

    [Display(Name = "Project Title")]
    public String ProjectTitle { get; private set; }

    public DateTime DateCreated { get; private set; }

    public Team(string name, string title, int maxEmployees)
    {
        Name = name;
        ProjectTitle = title;
        DateCreated = DateTime.Now;
        MaxEmployees = maxEmployees;    
    }

    // note use of 'new, not 'override ...
    public new void Add(Employee employee)
    {
        if (this.Count == MaxEmployees)
        {
            throw new ArgumentOutOfRangeException("", "Maximum Number of Employees on Team reached");
        }

        base.Add(employee);
    }
}</employee>


Check this link
Provides solution using data annotations

http://stackoverflow.com/questions/5146732/viewmodel-validation-for-a-list[^]


这篇关于向List添加约束&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆