Solved

For loop in C#--Very very Urgent

Posted on 2007-03-27
5
266 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 500 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

Technology Partners: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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