Reputation: 5013
Why is the ( i < UniqueWords.Count ) expression valid in the for loop, but returns "CS0019 Operator '<' cannot be applied to operands of type 'int' and 'method group'" error when placed in my if? They are both string arrays, previously declared.
for (int i = 0;i<UniqueWords.Count;i++){
Occurrences[i] = Words.Where(x => x.Equals(UniqueWords[i])).Count();
Keywords[i] = UniqueWords[i];
if (i<UniqueURLs.Count) {rURLs[i] = UniqueURLs[i];}
}
EDITED to add declarations:
List<string> Words = new List<string>();
List<string> URLs = new List<string>();
//elements added like so. . . .
Words.Add (referringWords); //these are strings
URLs.Add (referringURL);
UniqueWords = Words.Distinct().ToList();
UniqueURLs = URLs.Distinct().ToList();
SOLVED. thank you, parentheses were needed for method .Count() I still do not fully understand why they are not always necessary.
Jon Skeet, thanks, I guess I don't understand what exactly the declarations are either then? You wanted the actual values assigned? They are pulled from an external source, but are strings.
I get it! Thanks. (the ()'s at least.)
Upvotes: 3
Views: 2366
Reputation: 602
SOLVED. thank you, parentheses were needed for method .Count() I still do not fully understand why they are not always necessary.
Jon Skeet, thanks, I guess I don't understand what exactly the declarations are either then? You wanted the actual values assigned? They are pulled from an external source, but are strings.
I get it! Thanks. (the ()'s at least.)
here's the difference:
Declared as a method...
public class UniqueWords {
public int Count() {
// Code to get the count
}
}
is used with parens:
if (i < UniqueWords.Count()) {}
If declared as a property...
public class UniqueWords {
public int Count {
get {
// code to get the count
}
}
}
is used without parens:
if (i < UniqueWords.Count) {}
So, if it's a method, use parens.. if it's a property, you don't.
Upvotes: 0
Reputation: 245499
You need the parens after the method name so that it's not comparing i to the method Count itself.
UniqueWords.Count()
Upvotes: 1
Reputation: 17191
I would suspect that your UniqueURLs-collection is an IEnumerable rather than an ICollection or derivative and it doesn't have the Count-property. You're invoking the Enumerable.Count-extension method, use parenthesis and you'll be fine:
if (i<UniqueURLs.Count()) {rURLs[i] = UniqueURLs[i];}
Upvotes: 0
Reputation: 144202
Without knowing the types of the objects you're working with, it's difficult to say for sure. Does UniqueURLs have a Count property, or only a Count() extension method?
Upvotes: 0
Reputation: 171914
What's the type of UniqueURLs? It seems that .Count is not a property, but a method.
Upvotes: 0
Reputation: 1503899
Are you absolutely sure they're both string arrays?
Are you sure one isn't a string array, and the other an IEnumerable<string>
? That would certainly explain it. (The method group in question would be the Enumerable.Count()
extension method.) If this is the case, you won't then be able to use the indexer within the block, either.
If that's not it, please remove any extraneous code but include the declarations so that we have a short but complete program to test against.
In fact, given your edit, they can't be declared as string arrays - because you're assigning List<string>
values to them. I suspect you'll find that UniqueWords
is declared as List<string>
or IList<string>
, but UniqueURLs
is declared as IEnumerable<string>
. This may be implicit if you're using var
though. Hover over the variable name to find out the type - and if var
is being more of a curse than a blessing, go back to explicitly typing your variables.
Upvotes: 7