tags:

views:

245

answers:

6

Hello,
I am writing some class and it wont compile without "using System.Linq". But i don't understand why its needed. What i am writing has nothing to do with Linq. How can i find out where a namespace is used?
And the very badly written piece of code (I am trying to figure out what i want it to do):

using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace DateFilename
{
  public class FailedFieldsList
  {
    private static List<FailedFields> ErrorList = new List<FailedFields>();
    public void AddErrorList(FailedFields errs)
    {
        ErrorList.Add(errs);
    }
    public void addSingleFailedField(string vField, string vMessage) 
    {
        //FailedFields
    }
    public List<FailedFields> GetErrorList()
    {
        return ErrorList;
    }
    public class FailedFields
    {
        public List<FailedField> ListOfFailedFieldsInOneRecord = new List<FailedField>();
        public class FailedField
        {
            public string fieldName;
            public string message;
            public FailedField(string vField, string vMessage)
            {
                this.fieldName = vField;
                this.message = vMessage;
            }
            public override string ToString()
            {
                return fieldName + ", " + message;
            }
        }
        public void addFailedField(FailedField f)
        {
            ListOfFailedFieldsInOneRecord.Add(f);
        }
        public int getFailedFieldsCount()
        {
            return ListOfFailedFieldsInOneRecord.Count();
        }
    }

  }
}

Error message produced when i dont include the linq namespace:
Error 4 Non-invocable member 'System.Collections.Generic.List<DateFilename.FailedFieldsList.FailedFields.FailedField>.Count' cannot be used like a method. D:\Slabo\My Documents\Visual Studio 2008\Projects\DateFilename\DateFilename\FailedFieldsList.cs 47 54 DateFilename

Thanks

+1  A: 

How about checking where the compiler tells you your error is when you remove the "using System.Linq"???

LorenVS
+1  A: 

A using declaration is never "needed," since you can always reference System.Linq.Foo explicitly wherever it is that you can reference Foo when you are using the namespace.

Hence, the fastest and easiest way to figure out where a namespace is used is to try compiling the program and seeing where any errors come up. If Foo is suddenly undefined, you've found the culprit!

VoteyDisciple
+3  A: 

You could be using some Extension Methods included in the System.Linq namespace (like Distinct, Select, Where, or some other collection methods). The build output window should give you a pretty good indication where the necessary using statement should be.

EDIT:

'System.Collections.Generic.List<DateFilename.FailedFieldsList.FailedFields.FailedField>.Count cannot be used like a method.

The only place that contains the word count in your code sample is:

ListOfFailedFieldsInOneRecord.Count() in getFailedFieldsCount()

Count is an Extension Method introduced in the System.Linq namespace, namely via the Enumerable static class.

Here are all of the Extension Methods this class affords you.

Pwninstein
+5  A: 

The problem is in the last method:

public int getFailedFieldsCount() {
    return ListOfFailedFieldsInOneRecord.Count();
}

The method Count() is not a member of a List(T). The property Count, however, is. If you replace Count() by Count, this will compile without the need for using System.Linq.

By including System.Linq, you enable the extension method Count(), which, confusingly enough, does exactly the same thing.

See List(T) Members on msdn for a breakdown of what's part of a List(T).

Dr_Asik
A: 

A more general (and proactive) way is to use a tool like NDepend.

Nick Monkman
A: 

One quick way is to open it in ILDasm - it almost always shows the complete namespaces of each type and method in use in your code ... however you may perfer using Reflector so you can switch from IL to C# ... of course you'll need to get it to compile first :)

Jason Haley