Solved

For loop in C#--Very very Urgent

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now