I have a condition let's assume for example Animal = {Dog,Cat,Elephant}
Now I want to make a for loop with if conditions in it (not a simple for loop), inside this for loop I do some code based on the animal type for example:
for(int i=0;i<100;i++)
{
if(some conditions on i)
{
for(int j =0;j<100;j++)
{
if(some condition on j)
{
switch(animaltype)
{
case Dog: //call function 1
case Cat: //call function 2
case Elephant: //call function 3
}
}
}
}
}
So for performance optimization in case of large loops I made the switch-case outside the for loop so the code became something like this:
switch (animaltype)
{
case Dog :
for(int i=0;i<100;i++)
{
if(some conditions on i)
{
for(int j =0;j<100;j++)
{
if(some condition on j)
{
//call function 1
}
}
}
}
//-------------
case Cat :
for(int i=0;i<100;i++)
{
if(some conditions on i)
{
for(int j =0;j<100;j++)
{
if(some condition on j)
{
//call function 2
}
}
}
}
//----------------------
case Elephant :
for(int i=0;i<100;i++)
{
if(some conditions on i)
{
for(int j =0;j<100;j++)
{
if(some condition on j)
{
//call function 3
}
}
}
}
}
The problem here is that I repeated the code 3 times (or as the number of cases) and this violates the once and only once principle of the Software Design.
I tried to pass a delegate but the 3 functions that I supposed to call have different arguments, can anyone tell me a neat solution to this case?
EDIT I mean by "different arguments" that they do not take the same number of arguments. For example:
function1(string s,int age)
function2(float height,bool CanMove)
function3(int legs,string name,string place)
switch.