Ray
Ray

Reputation: 3449

Updating a list using a method?

This is a really simple question concerning a method. I'm pretty new to C# and am testing lists. How do I call the method "addTwo" so that it updates every element in the "Marks" list by two? Please note that I've already created this method (scroll down further below the main method). I just want to know how to call it in the main method.

namespace ParallelLists
    {
        class Program
        {
            static void Main(string[] args)
            {
                //create an list of 4 student names
                List<string> names = new List<string>(4);
                names.Add("Matt");
                names.Add("Mark");
                names.Add("Luke");
                names.Add("John");

                //create a list of 4 integers representing marks
                List<decimal> marks = new List<decimal>(4);
                marks.Add(88m);
                marks.Add(90m);
                marks.Add(55m);
                marks.Add(75m);


                Console.WriteLine("the mark of " + names[0] + " is : " + marks[0]);
                Console.ReadLine();

                //Upgrade everyone by 2 marks
               ...   
            }

            public List<decimal> addTwo(List<decimal> mark)
            {
                List<decimal> temp = mark;
                for (int i = 0; i < temp.Count; i++)
                {
                    temp[i] += 2m;
                }
                return temp;
            }
        }
    }

Upvotes: 1

Views: 1604

Answers (3)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Your list is already getting a reference to 'mark' why return anything when any operation you are doing will operate on that same list.

instead of:


public List<decimal> addTwo(List<decimal> mark)
{
   List<decimal> temp = mark;
   for (int i = 0; i < temp.Count; i++)
   {
       temp[i] += 2m;
   }
   return temp;
}

I would do:


public void addTwo(List<decimal> mark)
{
   for (int i = 0; i < mark.Count; i++)
   {
      temp[i] += 2m;
   }
}

Then in your code call is just as

addTwo(mark);

I would rename it to AddTwo to fit normal c# conventions as well.

Upvotes: 1

Jesse
Jesse

Reputation: 8393

//Upgrade everyone by 2 marks
var plustwo = addTwo(marks);
Console.WriteLine("the mark of " + names[0] + " is : " + plustwo[0]);
Console.ReadLine();

Note, that you will also need to make AddTwo a static method:

  public static List<decimal> addTwo(List<decimal> mark)

Upvotes: 0

DarthVader
DarthVader

Reputation: 55032

You need to make your method static, since you are accessing on a non object. also you need to update your marks collection with the returned value. You dont actually need to return the List because list is mutable.

class Program
{
    static void Main(string[] args)
    {
        //create an list of 4 student names
        List<string> names = new List<string>(4);
        names.Add("Matt");
        names.Add("Mark");
        names.Add("Luke");
        names.Add("John");

        //create a list of 4 integers representing marks
        List<decimal> marks = new List<decimal>(4);
        marks.Add(88m);
        marks.Add(90m);
        marks.Add(55m);
        marks.Add(75m);


        marks = addTwo(marks);

        Console.WriteLine("the mark of " + names[0] + " is : " + marks[0]);
        Console.ReadLine();

        Console.Read();

    } 

    public static List<decimal> addTwo(List<decimal> mark)
        {
            List<decimal> temp = mark;
            for (int i = 0; i < temp.Count; i++)
            {
                temp[i] += 2m;
            }
            return temp;
        }
}

if you dont want to return the list, you can do the following:

class Program
    {
        static void Main(string[] args)
        {
            //create an list of 4 student names
            List<string> names = new List<string>(4);
            names.Add("Matt");
            names.Add("Mark");
            names.Add("Luke");
            names.Add("John");

            //create a list of 4 integers representing marks
            List<decimal> marks = new List<decimal>(4);
            marks.Add(88m);
            marks.Add(90m);
            marks.Add(55m);
            marks.Add(75m);


            addTwo(marks);

            Console.WriteLine("the mark of " + names[0] + " is : " + marks[0]);
            Console.ReadLine();

            Console.Read();

        }

        public static void addTwo(List<decimal> mark)
        {
            List<decimal> temp = mark;
            for (int i = 0; i < temp.Count; i++)
            {
                temp[i] += 2m;
            }

        }
    }

Upvotes: 3

Related Questions