views:

103

answers:

3

'X', here, is a 3rd-party component implementing ICollection, and 'Y' is Count. It compiles, temporarily removing the squigglies, and either shortly after (1-2 seconds), or after any edits are made in the text editor, shows red squigglies under Count. ?! Any help would be mucho appreciated, thx.

edit: for example,

ThirdPartyComponent instanceOfComponent = new instanceOfComponent();
instanceOfComponent.GetResults();

for(int i = 0; i < instanceOfComponent.Results.Count; ++i) {
    // Some stuff happens
}

Here 'Count' is squigglied, even though it compiles fine--and even shows up in Intellisense.

A: 

do you have "using system.linq;"?

Ali Shafai
Negative; when I added it, the entire iterator conditional (i < blahblah) got squigglied and VS said 'Count' was a method group. It still compiles fine however.
Sam Pearson
A: 

I'm not sure what language you're using, but two possible problems here:

  1. Count could be a function rather than a property.

  2. More likely Results is actually a property returning a List<> based class and so you are calling the Count property of this Results object rather than the instanceOfComponent. A simple cast should solve it.

ChrisBD
Yes, I'm thinking it should be Count(), not just Count by itself.
Nicholas H
@Nicholas - Count is a valid read-only property on ICollection<T> and IList<T>; Count() is an extension method on IEnumerable<T>; so both are valid, though the property Count is preferrable.
Bevan
+2  A: 

From your comment above, it looks like VS is complaining about ambiguity between Results.Count and Results.Count(). It will compile fine, but it is warning you about this possible error. A cast to ICollection will explicitly tell the compiler which one to use:

for(int i = 0; i < ((ICollection)instanceOfComponent.Results).Count; ++i)
lc
There's no ambiguity between Count property and a Count() method - different syntax means it is always clear which is required. Also, extension methods [such as Count()] are always last in precedence.
Bevan
I couldn't find the right word, but I meant ambiguity in the sense that "maybe you meant to invoke the method but didn't"
lc
Thanks lc, casting fixed it.
Sam Pearson