Home > Software design >  Add Items to List depending on Loop Value c#
Add Items to List depending on Loop Value c#

Time:04-29

I have a method that returns a list. It takes 6 parameters (leg numbers). There is also a property that keeps track of the Number of Legs (it can have a value between 1-6). I can't seem to figure out if its possible to incorporate a loop and only add the list items depending on the Number of legs.

private List<T> ReturnListOfTypes<T>(T leg1, T leg2, T leg3,
        T leg4, T leg5, T leg6)
    {
        List<T> legs = new List<T>();

        for (int i = 0; i < NumberOfLegs; i  )
        {
           
        }

        legs.Add(leg1);
        legs.Add(leg2);
        legs.Add(leg3);
        legs.Add(leg4);
        legs.Add(leg5);
        legs.Add(leg6);

        return legs;
    }

If NumberOfLegs = 3, I only want the list to add:

        legs.Add(leg1);
        legs.Add(leg2);
        legs.Add(leg3);

If NumberOfLegs = 1, it should only add leg 1 to the list:

         legs.Add(leg1);

Right now, the list items are outside of the loop, because I can't figure out a way to only add the specified items depending on what the loop value is.

CodePudding user response:

Maybe you could make it a params []:

private List<T> ReturnListOfTypes<T>(params T[] legs)
{
    List<T> legList = new List<T>(NumberOfLegs);
    for (int i = 0; i < Math.Min(NumberOfLegs, legs.Length); i  )
    {
       legList.Add(legs[i]);
    }

    return legList;
}

You can now still use it like in your code, but it's treated as an array. This presumes that the order of "legs" is always the order you want to return.

So you could use it for example so:

List<string> list = ReturnListOfTypes("1", "2", "3");

CodePudding user response:

If I guess correctly, its all about adding number to particular leg. So in this case:

private List<T> ReturnListOfTypes<T>(T leg1, T leg2, T leg3,
        T leg4, T leg5, T leg6)
    {
        int NumberOfLegs = 6;
        int j=1;
        List<T> legs = new List<T>();

        for (int i = 0; i <= (NumberOfLegs 1); i  )
        {
           j = j i;
        
           var newLeg = "leg" j;
///Dont forget here, your for loop starts with 0, if you dont have declared leg0 then there would be error)
           legs.Add(newLeg);
        }

        return legs;
    }

///Start with i=1
        int NumberOfLegs = 6;
        List<T> legs = new List<T>();

        for (int i = 1; i < (NumberOfLegs 1); i  )
        {
           var newLeg = "leg" i;
           legs.Add(newLeg);
        }

        return legs;
    }

Also you could avoid outofRange with if statement before the loop: if(NumberOfLegs > 0)

CodePudding user response:

I'm not sure I understood properly your problem, but the following code should do what you are looking for:

List<T> ReturnListOfTypes<T>(T leg1, T leg2, T leg3, T leg4, T leg5, T leg6)
{
    List<T> legs = new List<T>();
    if (NumberOfLegs >= 1) { legs.Add(leg1); }
    if (NumberOfLegs >= 2) { legs.Add(leg2); }
    if (NumberOfLegs >= 3) { legs.Add(leg3); }
    if (NumberOfLegs >= 4) { legs.Add(leg4); }
    if (NumberOfLegs >= 5) { legs.Add(leg5); }
    if (NumberOfLegs >= 6) { legs.Add(leg6); }
    return legs;
}

Hope it helps

  • Related