Link to home
Start Free TrialLog in
Avatar of rowmark
rowmark

asked on

For loop in C#--Very very Urgent

Hello experts,

To give a quick back ground, I have a user control called filters and I am using it around 5 times in my ASPX page. The ids of the controls are filters1,filters2,filters3 etc.

I have few public properties in the user controls. Now I have a function which will return a list of filters as shown below.

Now I am having a for loop which will through an give the values.

Is there a way that, if the Reportfilters.Count=2 it does

                   filters1.ColumnName = Reportfilters[0].Filter_Column;
                    filters1.Condition = Reportfilters[0].Condition;
                    filters1.FilterValue1 = Reportfilters[0].FilterValue1;
                    filters1.FilterValue2 = Reportfilters[0].FilterValue2;

                    filters2.ColumnName = Reportfilters[1].Filter_Column;
                    filters2.Condition = Reportfilters[1].Condition;
                    filters2.FilterValue1 = Reportfilters[1].FilterValue1;
                    filters2.FilterValue2 = Reportfilters[1].FilterValue2;

Below is my actual code.

Please help


**********************************************************************************************************
  List<ReportFilter> Reportfilters = Controllers.ReportBuilderController.GetAllFilters(Model);

                for (int i = 0; i < Reportfilters.Count; i++)
                {
                    filters1.ColumnName = Reportfilters[i].Filter_Column;
                    filters1.Condition = Reportfilters[i].Condition;
                    filters1.FilterValue1 = Reportfilters[i].FilterValue1;
                    filters1.FilterValue2 = Reportfilters[i].FilterValue2;
                }
ASKER CERTIFIED SOLUTION
Avatar of swingspen
swingspen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dstanley9
dstanley9

try using FindControl():

filters filter = null;
for (int i = 0; i < Reportfilters.Count; i++)
{
  filter = this.FindControl("filters" + i.ToString()) as filter;
  if(filter != null)
  {
    filter.ColumnName = Reportfilters[i].Filter_Column;
    filter.Condition = Reportfilters[i].Condition;
    filter.FilterValue1 = Reportfilters[i].FilterValue1;
    filter.FilterValue2 = Reportfilters[i].FilterValue2;
  }
}
As in if the loop value == 2?  or just the total count of filters added?
for the 1st option
inside the loop put this:
if(i==2)
{
     //do this
}
else
{
    //do that
}

Let me know!
Avatar of rowmark

ASKER

swingspen,

I am trying your second option which is feasible for me. But when I try to build the solution I am getting an error saying Not all code paths return a value in this function:

Please help

private Filters GetFiltersControl(int index)
        {
            switch (index)
            {
                case 1:
                    return filters1;

                case 2:
                    return filters2;
            }
        }
Give this a try

private Filters GetFiltersControl(int index)
{
    switch (index)
    {
        case 1:
           return filters1;

        case 2:
            return filters2;

        default:
            return null;
    }
}

Note:  You may want to look at dstanley9's solution as it is essentially doing the same thing as the switch statement above but isn't using a hard coded set of rules.  Thus if you add a new filter object in the future as long as you keep with the same naming convention, it'll get picked up.