0

I have a class thats receive all crystal report req to open the form of the report(a switch and a lot of cases).

and i need to copy and paste every time this code

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;
                }
            }
        }
    }
}

Problem: i created a function and triend to paste this part of code there... and i passed the crParceiro.ReportDefinition, and also crParceiro.ReportDefinition.ReportsObject but it doesnt have the set property and i cant set it out and return on REF our OUT...

I tried to Return the value and Linq Expressions(it says..."object doesnt have set property") no success **Reference of Linq Exp and Return Value: **Link here

A good example of the problem are:

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

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

what can i do? im very lost..

Community
  • 1
  • 1
Danilo Breda
  • 1,092
  • 1
  • 15
  • 30

1 Answers1

1

In the code that you posted you are NOT setting the ReportObjects to null or any other value. You are accesing items of the ReportObjects by index and changing properties of these items but not direcctly on the ReportObjects

So this should work.

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;
            }
        }
    }
}

Then in your current call you use it like this

if (sender.GetType() == typeof(CheckBox))
{
    YourMethod(crParceiro.ReportDefinition.ReportObjects, ListaTodos) ;
}
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • 1
    @Daniloloko great, keep in mind that when you are passing arrays they are already by reference and therefor you can change them without any problem. For more details see here http://www.yoda.arachsys.com/csharp/parameters.html – Mauricio Gracia Gutierrez Sep 11 '13 at 13:45