ArrayList pass to method

D

Dave

Below is some code snippets that use an ArrayList,
I get the error below when trying to access the collection of PrevPrintJobs.
I can see the object in my debug watch window but the error is thrown at the
line with the asterix.
Is it possible to pass an arraylist of objects to another method?


ArrayList PrevPrintJobsCollection = new ArrayList();

class PrevPrintJobs
{


public string doc;
public string name;
public string time;
public string size;

}


static bool IfprevPrinted(PrevPrintJobs ppj, ArrayList ppjc)
{

System.Collections.IEnumerator myEnumerator =
ppjc.GetEnumerator();
if (ppjc.Count > 0)
{

while (myEnumerator.MoveNext())
if (ppjc[0].doc == ppj.doc)//*************error 1 here
{
return true;
break;

}
}
return false;





Error 1 'object' does not contain a definition for 'doc' and no extension
method 'doc' accepting a first argument of type 'object' could be found (are
you missing a using directive or an assembly reference?)
 
A

AliR \(VC++ MVP\)

I would use List<PrevPrintJobs> instead of ArrayList. Otherwise you will
have to cast the content of the ArrayList in your if statment.

if (((PrevPrintJobs)ppjc[0]).doc = ppj.doc)

if you use a List<PrevPrintJobs> you won't have to use a cast at all.

AliR.
 
S

Stefan Hoffmann

hi Dave,
Below is some code snippets that use an ArrayList,
I get the error below when trying to access the collection of PrevPrintJobs.
I can see the object in my debug watch window but the error is thrown at the
line with the asterix.
An ArrayList stores objectes, so ppj is of type Object and not
PrevPrintJob. You may cast it or simply use a generic:


IList<PrevPrintJobs> PrevPrintJobsCollection =
new List<PrevPrintJobs>();


private static bool IfprevPrinted(PrevPrintJobs ppj,
IList<PrevPrintJobs> ppjc)
{
foreach (PrevPrintJobs p in ppjc)
{
if (ppj.doc.Equals(p.doc)
{
return true;
}
}
return false;
}

or using LINQ:

bool IfprevPrinted = (ppjc.Count(p => p.doc.Equals(ppj.doc)) > 0);



mfG
--> stefan <--
 
J

Jeroen Mostert

Stefan Hoffmann wrote:
bool IfprevPrinted = (ppjc.Count(p => p.doc.Equals(ppj.doc)) > 0);
This is more direct:

bool IfprevPrinted = ppjc.Select(p => p.doc).Contains(ppj.doc);

Or if you're dead set on explicitly using .Equals() instead of whatever the
default equality comparer will be:

bool IfprevPrinted = ppjc.Any(p => p.doc.Equals(ppj.doc));
 
B

Ben Voigt [C++ MVP]

Dave said:
Below is some code snippets that use an ArrayList,
I get the error below when trying to access the collection of
PrevPrintJobs.
I can see the object in my debug watch window but the error is thrown at
the line with the asterix.
Is it possible to pass an arraylist of objects to another method?

This has nothing to do with passing it to a method. You'd have the same
problem trying to use the ArrayList right where you added to it.

The problem is that ArrayList loses all the type information. Use generic
collections such as List<T> instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top