Home > Enterprise >  How can I add values to a list-method or deal with it in general?
How can I add values to a list-method or deal with it in general?

Time:01-04

I came across a predefined method which has a List<int> as return value:

public static List<int> FindMultiples(int integer, int limit){}

When I tried to add values to it with FindMultiples.Add(), it wouldn't compile, saying "...FindMultiples(int,int) is a method, which is not valid in the given context."

I expected the list to take the values I tried to add inside an if-statement, but it wouldn't.

public static List<int> FindMultiples(int integer, int limit)
            {
                int _integer = integer;
                int _limit = limit;
                
                FindMultiples(4, 20);
                
                if (FindMultiples.Count <= limit)
                {
                    FindMultiples.Add(integer * 1);
                    FindMultiples.Add(integer * 2);
                    FindMultiples.Add(integer * 3);
                    FindMultiples.Add(integer * 4);
                    FindMultiples.Add(integer * 5);
                }

                return FindMultiples();
            }

I'm stumped right now. Thanks in advance for helping a beginner!

CodePudding user response:

You need to declare a List<int> in which you can perform Add operation & then return the list. You were adding values in FindMultiples() which is a method not a collection.

    public static List<int> FindMultiples(int integer, int limit)
        {
            
            List<int> lst = new List<int>();

            
            if (lst.Count <= limit)
            {
                lst.Add(integer * 1);
                lst.Add(integer * 2);
                lst.Add(integer * 3);
                lst.Add(integer * 4);
                lst.Add(integer * 5);
            }

            return lst ;
        }
  • Related