【问题标题】:Passing properties to a function (only get property error)C#将属性传递给函数(仅获取属性错误)C#
【发布时间】:2013-09-15 12:03:03
【问题描述】:

我有一个类接收所有水晶报表req来打开报表的形式(一个开关和很多案例)。

而且每次这段代码我都需要复制粘贴

if (sender.GetType() == typeof(CheckBox))
{
    foreach (CheckBox elemento in flowLayoutPanel1.Controls.OfType<CheckBox>())
    {
        if (elemento.Checked)
        {
            foreach (string elemento2 in ListaTodos)
            {
                string Name = elemento.Tag.ToString();
                if (Name.Substring(Name.Length - 1, 1) == elemento2.Substring(elemento2.Length - 1, 1))
                {
                    crParceiro.ReportDefinition.ReportObjects[Name].ObjectFormat.EnableSuppress = false;
                    crParceiro.ReportDefinition.ReportObjects[elemento2].ObjectFormat.EnableSuppress = false;
                }
            }
        }
        else
        {
            foreach (string elemento2 in ListaTodos)
            {
                string Name = elemento.Tag.ToString();
                if (Name.Substring(Name.Length - 1, 1) == elemento2.Substring(elemento2.Length - 1, 1))
                {
                    crParceiro.ReportDefinition.ReportObjects[Name].ObjectFormat.EnableSuppress = true;
                    crParceiro.ReportDefinition.ReportObjects[elemento2].ObjectFormat.EnableSuppress = true;
                }
            }
        }
    }
}

问题:我创建了一个函数并试图将这部分代码粘贴到那里...并且我通过了 crParceiro.ReportDefinitioncrParceiro。 ReportDefinition.ReportsObject 但它没有设置属性,我无法设置它并返回 REF 我们的 OUT...

我试图返回值和 Linq 表达式(它说...“对象没有设置属性”)没有成功 **Linq Exp及返回值参考:**Link here

这个问题的一个很好的例子是:

ReportDefinition teste = new ReportDefinition();
teste.ReportObjects = null;

//Error: Property or idexer ...cannot be assigned to -- its read only.

我能做什么?我很迷茫..

【问题讨论】:

    标签: c# linq crystal-reports lambda return


    【解决方案1】:

    在您发布的代码中,您ReportObjects 设置为 null 或任何其他值。您正在通过索引访问 ReportObjects 的项目并更改这些项目的属性,但不是直接在 ReportObjects

    所以这应该有效。

    private void YourMethod(ReportObjects repObjs, List<string> ListaTodos)
    {
        foreach (CheckBox elemento in flowLayoutPanel1.Controls.OfType<CheckBox>())
        {
            bool enableSuppress ;
    
            //enableSuppress changes based on the the "elemento" being checked or not
            enableSuppress = !elemento.Checked ;
    
            foreach (string elemento2 in ListaTodos)
            {
                string Name = elemento.Tag.ToString();
    
                if (Name.Substring(Name.Length - 1, 1) == elemento2.Substring(elemento2.Length - 1, 1))
                {
                    repObjs[Name].ObjectFormat.EnableSuppress = enableSuppress;
                    repObjs[elemento2].ObjectFormat.EnableSuppress = enableSuppress;
                }
            }
        }
    }
    

    然后在你当前的通话中你像这样使用它

    if (sender.GetType() == typeof(CheckBox))
    {
        YourMethod(crParceiro.ReportDefinition.ReportObjects, ListaTodos) ;
    }
    

    【讨论】:

    • @Daniloloko 太好了,请记住,当您传递数组时,它们已经是引用,因此您可以毫无问题地更改它们。有关更多详细信息,请参阅此处yoda.arachsys.com/csharp/parameters.html
    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多