Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For loop in C#--Very very Urgent

Posted on 2007-03-27
5
Medium Priority
?
283 Views
Last Modified: 2010-04-16
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;
                }
0
Comment
Question by:rowmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
swingspen earned 2000 total points
ID: 18802811
Here are a couple solutions that might work for you.

1) Place all the filter controls in a List

    //Initialize the list probably on page load
    List<Filters> filterControls;

    Then your code would look something like this

    List<ReportFilter> Reportfilters = Controllers.ReportBuilderController.GetAllFilters(Model);
       
    for (int i = 0; i < Reportfilters.Count; i++)
    {
      filterControls[i].ColumnName = Reportfilters[i].Filter_Column;
      filterControls[i].Condition = Reportfilters[i].Condition;
      filterControls[i].FilterValue1 = Reportfilters[i].FilterValue1;
      filterControls[i].FilterValue2 = Reportfilters[i].FilterValue2;
    }

2) Use a separate method to return the filter you want

    List<ReportFilter> Reportfilters = Controllers.ReportBuilderController.GetAllFilters(Model);        
    for (int i = 0; i < Reportfilters.Count; i++)
    {
        Filters control = GetFiltersControl(i);
      control .ColumnName = Reportfilters[i].Filter_Column;
      control .Condition = Reportfilters[i].Condition;
      control .FilterValue1 = Reportfilters[i].FilterValue1;
      control .FilterValue2 = Reportfilters[i].FilterValue2;
    }

    ***** Separate Method  ******
    private Filters GetFiltersControl(int index)
    {
        switch(index)
        {
            case 1:
                return filter1;

            case 2:
                return filter2;
        }
    }
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 18802837
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;
  }
}
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18802845
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!
0
 

Author Comment

by:rowmark
ID: 18803318
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;
            }
        }
0
 
LVL 1

Expert Comment

by:swingspen
ID: 18803415
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.
      
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question