2
class Calculate
{
     public void extract(// code to get the values of  array list)
     {
            if ( numbers < Greatest)
            {
                  list.remove(0);
            }
     }
}
class MainClass
{
   public static void Main()
   {
         int[] numbers = new int[] {1, 2, 3, 4, 5 }// modifying the value; 
         numbers = list.ToArray(typeof(int)) as int
         ArrayList list = new ArrayList(numbers);
         int Greatest = 6;
   }
}

I want to send the array list values to the parameter of the extract function. After subtracting that one element i want that too be reflected in my numbers array

Darren
  • 68,902
  • 24
  • 138
  • 144
  • 9
    ArrayList? In 2013? – Sergey Berezovskiy Sep 09 '13 at 11:54
  • 3
    I don't understand. What do you want as a result based on your example? And please don't use `ArrayList` anymore :) `ArrayList` belongs to the days that C# didn't have generics. Use `List` insted. Check out http://stackoverflow.com/questions/725459/c-sharp-when-should-i-use-list-and-when-should-i-use-arraylist – Soner Gönül Sep 09 '13 at 11:55
  • 1
    @lazyberezovsky Might still be found in teaching material that hasn't been updated. Perhaps this is homework? – Chris Mantle Sep 09 '13 at 11:57
  • 1
    Are you wanting to check whether each element of list is less than Greatest? Like everyone else, I'm confused about what you are really looking for. If you want to pass the ArrayList as a parameter, that is simple: public void extract(ArrayList list){... – Sven Grosen Sep 09 '13 at 12:00
  • Your updated Main() method won't even compile, you are referencing list before it is declared. – Sven Grosen Sep 09 '13 at 12:02

1 Answers1

2

You'll need the following declaration:

 public void extract(ArrayList list) {

 }

Then to call the method:

Calculate calc = new Calculate();
calc.extract(list);

However as the others have mentioned use List<T> rather than a ArrayList.

In this example you could use List<int> since numbers are integers. Your method declaration could then look like:

public void extract(List<int> intList) {

}

Also looking at the logic in in your extract method class doesn't seem accurate.

Greatest is not in the same scope as where you think you've declared it, you will need to declare this variable in the extract method. Also comparing numbers against greatest will not compare all of the numbers, in fact the compiler will complain, you need to iterate over the ArrayList and compare each element against Greatest.

public void extract(ArrayList list) {
     int greatest = 6;
     foreach (var item in list) {
         // comparison here
     }
} 
Darren
  • 68,902
  • 24
  • 138
  • 144