Click here to Skip to main content
15,886,678 members
Please Sign up or sign in to vote.
1.89/5 (3 votes)
See more:
C#
public static void diff(ArrayList args)
      {
          if (args.Count < 2 || args.Count > 3)
          {
              Console.WriteLine("Insufficent Number of Args");
          }
          else if (args.Count == 3)
          {
              string source_path = get_abs_path(args[1].ToString());
              StreamReader sr = new StreamReader(source_path);
              ArrayList text1 = new ArrayList();
              string [] file = sr.ReadToEnd().Split('\n');
              foreach (string line in file)
              {
                  text1.Add(line);
              }
              sr.Close();
              Console.WriteLine("Content of " + source_path + " :");
              Console.WriteLine(file);

              source_path = get_abs_path(args[2].ToString());
              sr = new StreamReader(source_path);
              ArrayList text2 = new ArrayList();
              string[] file2 = sr.ReadToEnd().Split('\n');
              foreach (string lin in file2)
              {
                  text2.Add(lin);
              }
              sr.Close();
              ArrayList difference = new ArrayList(text1.Except(text2)); // what is the problem here !

          }
      }
Posted
Comments
CHill60 10-Nov-13 9:44am    
Use the Improve question link to add some details ... for example the problem that you are having
Sergey Alexandrovich Kryukov 10-Nov-13 13:18pm    
To start with, stop using obsolete ArrayList, use System.Collections.Generic.List<>.
—SA

The message is accurate:
'System.Collections.ArrayList' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)

But not that helpful.

Basically, you shouldn't be using ArrayList - it's out of date, and was replaced by the Generic collection types nearly ten years ago!

ArrayLists do not support the IEnumerable<T> interface, which allows them to use Linq extension methods. Replace your ArrayList's with List<string> instead and it should work fine.


-------OFF TOPIC ANSWER SECTION-------

"There is something confusing me in what you wrote in your solution: "ArrayLists do not support the IEnumerable interface, which allows them to use Linq extension methods."
I think it should be: "ArrayLists do not support the IEnumerable interface, which would allow them to use Linq extension methods." or maybe "ArrayLists do not support the IEnumerable interface, which prevents you from using Linq extension methods on them."
Cheers!"



Ah! It's understandable, but...
There are two Interfaces involved here: IEnumerable (the old one, that allows you to use foreach loops) and IEnumerable<T> the newer, strongly typed generic version introduced at .NET V2.0

The former is not the same (and IEnumerable<T> derives from IEnumerable) and only the latter will work with Linq methods, because they are all predicated upon IEnumerable<T> - if you look at the MSDN description for a link method: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except(v=vs.110).aspx[^] it is an IEnumerable<T> method, not IEnumerable.

Try it:
C#
public class enumableObject : IEnumerable
    {
    public IEnumerator GetEnumerator()
        {
        throw new NotImplementedException();
        }
    }
public class enumerableGeneric<T> : IEnumerable<T>
    {
    public IEnumerator<T> GetEnumerator()
        {
        throw new NotImplementedException();
        }

    IEnumerator IEnumerable.GetEnumerator()
        {
        throw new NotImplementedException();
        }
    }
Then
C#
List<string> list = new List<string>();
var x = list.Except(list);
Works fine
C#
enumerableGeneric<string> eg = new enumerableGeneric<string>();
var z = eg.Except(eg);
Works fine
C#
enumableObject eo = new enumableObject();
var y = eo.Except(eo);
Gives a compilation error:
MyNamespace.frmMain.enumableObject' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'MyNamespace.frmMain.enumableObject' could be found (are you missing a using directive or an assembly reference?)	
Exactly the same as a ArrayList.
 
Share this answer
 
v2
Comments
Thomas Daniels 10-Nov-13 9:55am    
+5!
Sampath Lokuge 10-Nov-13 9:59am    
+5,Nice one.
Manfred Rudolf Bihy 10-Nov-13 12:24pm    
There is something confusing me in what you wrote in your solution: "ArrayLists do not support the IEnumerable<t> interface, which allows them to use Linq extension methods."
I think it should be: "ArrayLists do not support the IEnumerable<t> interface, which would allow them to use Linq extension methods." or maybe "ArrayLists do not support the IEnumerable<t> interface, which prevents you from using Linq extension methods on them."
Cheers!
OriginalGriff 11-Nov-13 4:15am    
Hi Manfred!
I've answered this in the answer body as an addendum (because I needed to use greater than and less than and that's a PITA in comments), plus I can format it a bit better... :laugh:
The Except method returns IEnumerable, you need to convert the result to list:

C#
list1 = list1.Except(list2).ToList();


Convert ArrayList to List:

XML
using System;
using System.Collections;
using System.Collections.Generic;

static class Extensions
{
    /// <summary>
    /// Convert ArrayList to List.
    /// </summary>
    public static List<T> ToList<T>(this ArrayList arrayList)
    {
    List<T> list = new List<T>(arrayList.Count);
    foreach (T instance in arrayList)
    {
        list.Add(instance);
    }
    return list;
    }
}

class Program
{
    static void Main()
    {
    // Create ArrayList.
    ArrayList arrayList = new ArrayList();
    arrayList.Add(1);
    arrayList.Add(2);
    arrayList.Add(3);

    // Use extension method.
    List<int> list = arrayList.ToList<int>();
    foreach (int value in list)
    {
        Console.WriteLine(value);
    }
    }
}


Output:

1
2
3


I hope this will help to you.
 
Share this answer
 
v3
Comments
OriginalGriff 10-Nov-13 10:01am    
Reason for my vote of one: If you read the question, you will find that the OP is not using generic classes - he is using the much older ArrayList collection - which do not support LINQ methods...
Sampath Lokuge 10-Nov-13 10:09am    
If you want,You can convert ArrayList into List by using Extension method.Please see my answer.I hope you change your vote :)
OriginalGriff 10-Nov-13 10:12am    
Sorry, but that is what we call "A Massive Bodge" - especially when compared to not using an ArrayList in the first place!
BillWoodruff 10-Nov-13 11:36am    
Hi Og, forgive me for getting etymological on you, but I am fascinated by the term "massive bodge" which, to my knowledge, doesn't occur in American slang. The "classic" definition seems to be some kind of pejorative reference to a swollen, or unusually large, nose. But, it has the meanings of a "big mess," in general ? Possibly a meaning similar to "kludge," meaning an inelegant, messy, patching together of something to get it working ?
OriginalGriff 10-Nov-13 11:54am    
A "bodge" is a "kludge" that you have beaten repeatedly with a large hammer in order to fit it into a hole for which it is the incorrect shape. And then glued the bits that fell off back on to make it look like nothing happened.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900