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_Co lumn;
filters1.Condition = Reportfilters[0].Condition ;
filters1.FilterValue1 = Reportfilters[0].FilterVal ue1;
filters1.FilterValue2 = Reportfilters[0].FilterVal ue2;
filters2.ColumnName = Reportfilters[1].Filter_Co lumn;
filters2.Condition = Reportfilters[1].Condition ;
filters2.FilterValue1 = Reportfilters[1].FilterVal ue1;
filters2.FilterValue2 = Reportfilters[1].FilterVal ue2;
Below is my actual code.
Please help
************************** ********** ********** ********** ********** ********** ********** ********** **********
List<ReportFilter> Reportfilters = Controllers.ReportBuilderC ontroller. GetAllFilt ers(Model) ;
for (int i = 0; i < Reportfilters.Count; i++)
{
filters1.ColumnName = Reportfilters[i].Filter_Co lumn;
filters1.Condition = Reportfilters[i].Condition ;
filters1.FilterValue1 = Reportfilters[i].FilterVal ue1;
filters1.FilterValue2 = Reportfilters[i].FilterVal ue2;
}
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
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_Co
filters1.Condition = Reportfilters[0].Condition
filters1.FilterValue1 = Reportfilters[0].FilterVal
filters1.FilterValue2 = Reportfilters[0].FilterVal
filters2.ColumnName = Reportfilters[1].Filter_Co
filters2.Condition = Reportfilters[1].Condition
filters2.FilterValue1 = Reportfilters[1].FilterVal
filters2.FilterValue2 = Reportfilters[1].FilterVal
Below is my actual code.
Please help
**************************
List<ReportFilter> Reportfilters = Controllers.ReportBuilderC
for (int i = 0; i < Reportfilters.Count; i++)
{
filters1.ColumnName = Reportfilters[i].Filter_Co
filters1.Condition = Reportfilters[i].Condition
filters1.FilterValue1 = Reportfilters[i].FilterVal
filters1.FilterValue2 = Reportfilters[i].FilterVal
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
for the 1st option
inside the loop put this:
if(i==2)
{
//do this
}
else
{
//do that
}
Let me know!
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;
}
}
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.
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.
filters filter = null;
for (int i = 0; i < Reportfilters.Count; i++)
{
filter = this.FindControl("filters"
if(filter != null)
{
filter.ColumnName = Reportfilters[i].Filter_Co
filter.Condition = Reportfilters[i].Condition
filter.FilterValue1 = Reportfilters[i].FilterVal
filter.FilterValue2 = Reportfilters[i].FilterVal
}
}