views:

351

answers:

7

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.)

+6  A: 

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.

Jon Skeet
A: 

What's the type of UniqueURLs? It seems that .Count is not a property, but a method.

Philippe Leybaert
+4  A: 

don't forget your parens:

UniqueWords.Count()
Jason
A: 

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?

Rex M
A: 

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];}
Patrik Hägne
+1  A: 

You need the parens after the method name so that it's not comparing i to the method Count itself.

UniqueWords.Count()
Justin Niessner
A: 

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.

datacop