Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对同一对象使用两个get和set属性_C#_.net_Winforms_Properties_Mvp - Fatal编程技术网

C# 对同一对象使用两个get和set属性

C# 对同一对象使用两个get和set属性,c#,.net,winforms,properties,mvp,C#,.net,Winforms,Properties,Mvp,我在UI中有一个DataGridView,我的演示文稿类需要设置和从网格获取数据。这样我就可以在这种情况下使用公共属性,如下所示 public DataGridView DeductionDetailsInGrid { get { return dgEmployeeDeductions; } } public List <Deduction > DeductionDetails { set { dgEmployeeDeductions.DataSource = v

我在UI中有一个
DataGridView
,我的
演示文稿
类需要
设置
网格
获取数据。这样我就可以在这种情况下使用
公共属性
,如下所示

public DataGridView DeductionDetailsInGrid
{
    get { return dgEmployeeDeductions; }
}


public List <Deduction > DeductionDetails
{
    set { dgEmployeeDeductions.DataSource = value; }
}

正如您所说,将
DataGridView
返回给演示者会破坏封装,并将视图和演示者耦合起来。演示者不应该知道视图用于可视化模型的控件。演示者只需将数据传递给视图

跟随你的设定者的例子。在getter中返回一个
列表
。您可以在getter中映射模型列表,然后返回到presenter

public List<Deduction> DeductionDetails
{
    get
    {
        List<Deduction> deductionsList = new List<Deduction>();

        foreach(var deductionFromGrid in dgEmployeeDeductions.Items)
        {
            Deduction deduction = new Deduction();
            // map properties here

            deductionsList.Add(deduction)
        }

        return deductionsList;
    }
}
公共列表详细信息
{
得到
{
列表扣减列表=新列表();
foreach(dgEmployeeDeductions.Items中的var扣除从网格中扣除)
{
扣减=新扣减();
//在此映射属性
扣减列表。添加(扣减)
}
返回扣减列表;
}
}
公共列表扣减详细信息
{
获取{return(List)dgeEmployeedEducations.DataSource;}
设置{DGemployeedEducations.DataSource=value;}
}

嗯,setter是没有意义的,因为这种类型的用户可以通过getter访问DGV。他们可以使用getter获取数据并设置数据源,而无需通过您的属性。这就是我想要更改的内容。但是我的演示者应该只能通过属性获取数据,我不知道。
public List declutationdetails{get{(List)dgeemployeedeductions.DataSource;}set{dgeemployeedeductions.DataSource=value;}}
@Sriram Sakthivel,代码中的错误:只有赋值、调用、递增、递减和新对象表达式可以用作语句。对不起,get应该与
get类似{return(List)dEmployeedEducations.DataSource;}
我不明白这正是您需要的。如果我遗漏了一些明显的内容,请纠正我。
public List<Deduction> DeductionDetails
{
    get
    {
        List<Deduction> deductionsList = new List<Deduction>();

        foreach(var deductionFromGrid in dgEmployeeDeductions.Items)
        {
            Deduction deduction = new Deduction();
            // map properties here

            deductionsList.Add(deduction)
        }

        return deductionsList;
    }
}
public List <Deduction > DeductionDetails
{
    get { return (List<Deduction>)dgEmployeeDeductions.DataSource; }
    set { dgEmployeeDeductions.DataSource = value; }
}