Solved

For loop in C#--Very very Urgent

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

16 Experts available now in Live!

Get 1:1 Help Now