views:

692

answers:

3

Hi guys

I've got another simple one (I think) that's stumping me. I have written a method in one of my controls that gets the latest version of a file in a CMS given it's filename (i.e. regardless of what folder the file resides in). I found it useful enough that I thought I'd chuck it in my CMSToolbox class, but when I do this I can no longer use the Where() method of a FileManager class provided by the CMS (which returns a list).

Here's a simplified example of my class:

using System;
using System.Collections.Generic;
using CMS.CMS;
using CMS.Core;
using CMS.Web;

namespace CoA.CMS {
    public class ToolBox
    {
        public CMS.CMS.File getLatestFileVersionByFilename(string filename, int GroupID)
        {
            IList<CMS.CMS.File> fileWithName = FileManager.GetGroupAll(false, GroupID).Where(file => currentFileVersionIsNamed(file, filename)).ToList<CMS.CMS.File>();
      return getLatestFileFromListOfFiles(fileWithName);

        }
        protected bool currentFileVersionIsNamed(CMS.CMS.File file, string name)
        {
        }
     protected CMS.CMS.File  getLatestFileFromListOfFiles(CMS.CMS.File file)
     {
     }
    }
}

When I do exactly the same thing in the context of a Control (really a class provided by the CMS which extends Control) I have access to the Where() method, but in my ToolBox class I don't. What gives? I figured that an IList would always allow access to the same methods from wherever you use it.

I'm a wrong again, haha :)


Edit: Filemanager.GetGroupAll() returns a CMSList which extends IList

+10  A: 

You need a using directive for System.Linq. .Where() is an extension method on IEnumerable<T> (which IList<T> implements) that is defined in the System.Linq namespace.

Joel Coehoorn
You, sir, are an absolute bl**dy legend! Well done! :)
Iain Fraser
A: 

IIRC, that .Where method is part of LINQ, and you need to add those using statements in your class to get the extension methods for the IEumerable interface.

Nate Bross
+3  A: 

Joel was first, but to expand on that: Where() is an extension method. Extension methods are static methods that act like real methods, and are declared like this:

static class NameNeverUsed
{
    public static void AnExtensionMethod(this string x)
    {
    }
}

And called like:

"hello".AnExtensionMethod();

They need to imported by using statements like anything else. So unlike Java/C++ a class can have methods declared outside it.

Robert Fraser
Excellent explanation, thank you Robert :). Love this place, I can't believe how much I've picked up from you guys. Thanks again.
Iain Fraser
Nice last name :-)
Robert Fraser
@Iain I'll tell you a "secret": when I started using SO about a year ago I knew _nothing_ about anything new in .Net 3.5. I used SO as a way to help myself learn about what was new in VS2008 and even a few tricks for VS2005, and it was effective enough to quickly help me soon be considered an expert here.
Joel Coehoorn
@Robert, right back atcha ;)@Joel, thanks for the tip mate. I can definately see how you can very quickly get up to speed in just about anything using SO. What strikes me is how free and open people are with their knowledge and how tolerant they are of newbies. SO seems to attract the right sort of people, unlike a lot of other Q/A services, knowledgable without being pretentious and so complete with their anwsers.
Iain Fraser