help on generic space


G

GS

I only dabble in C# once in a long while and am I lost. I got problem with
the following code
How should I reconcile
Color c2 = colors.Find(c1)
so I don't get
Error 1 The best overloaded method match for
'System.Collections.Generic.List<System.Drawing.Color>.Find(System.Predicate
<System.Drawing.Color>)' has some invalid arguments

===================
List<Color> colors = new List<Color>(256);
Color c1, c2;
int i, j, nc = 0, ih = bmp.Height, iw = bmp.Width;
const int IMax = 32;
for (i = 0; i < Math.Min(iw, IMax); i++)
{
//find number of colors
for (j = 0; j < Math.Min(ih,IMax); j++)
{
c1 = bmp.GetPixel(i, j);
if (i == 0 & j == 0)
{
colors.Add(c1);
}
else
{
c2 = colors.Find(c1);
if (c2 == null)
{
colors.Add(c1);
if (nc > 256)
{
MessageBox.Show("Warning - input image has
more than 256 colors and maybe rejected by Visual Studio as application
icon");
return false;
}
else nc++;
}
}
}
}
return true;
 
Ad

Advertisements

A

Alberto Poblacion

GS said:
I only dabble in C# once in a long while and am I lost. I got problem with
the following code
How should I reconcile
Color c2 = colors.Find(c1)
so I don't get
Error 1 The best overloaded method match for
'System.Collections.Generic.List<System.Drawing.Color>.Find(System.Predicate
<System.Drawing.Color>)' has some invalid arguments

The error message itself tells you what the argument to "Find" should
be: it is expecting a Predicate<Color>, which means "a delegate to a
function that takes a Color and returns a Boolean". You can provide such a
delegate by means of a Lambda:

Color c2 = colors.Find(c=>c==c1);
 
B

Ben Voigt [C++ MVP]

Alberto said:
The error message itself tells you what the argument to "Find"
should be: it is expecting a Predicate<Color>, which means "a
delegate to a function that takes a Color and returns a Boolean". You
can provide such a delegate by means of a Lambda:

Color c2 = colors.Find(c=>c==c1);

Or you could just use the right method. IndexOf is what most people want
when Find isn't working, for this particular case Contains would solve the
problem better. But using a better container, like a Set, would be even
better.
 
G

GS

thx. I took up your suggestion and replace c2 with into int c1Idx and then
compare to less than 1. worked like a charm..

thank you again.


I must be too tired not able to think clearly.

I am somewhat better after some extra sleep although still not quite enough
quality sleep
 
J

Jeff Johnson

But using a better container, like a Set, would be even better.

What is this "Set" of which you speak? The only thing I found in MSDN was a
C++ STL thing.
 
Ad

Advertisements

P

Peter Duniho

e.g. http://www.codeplex.com/PowerCollections

I thought .NET had added a set class.... apparently it's still in beta:
http://msdn.microsoft.com/en-us/library/bb397727(VS.100).aspx

No, the docs are still in beta. HashSet<T> has been in there since 3.5.

I actually posted a detailed reply to the OP when his question first
appeared, including describing HashSet<T> as a possible solution (with
code example). But, given recent problems with my posts being blocked by
Microsoft's servers, it's possible no one else saw the post:

http://groups.google.com/group/micr....csharp/browse_thread/thread/958a199d179b08d9

Pete
 

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