Web Developer, Software Engineer and Mixed Language Artist
RSS icon Email icon Home icon
  • Get random item from IList< T >

    Posted on November 3rd, 2010 Jamie No comments

    In what could turn out to be a series of posts about helpful extension methods I thought I’d include this quick little method for getting a random item from an IList collection.

    ?View Code CSHARP
    public static T GetRandomItem<T>(this IList<T> collection)
            {
                return collection[(new Random().Next(0, collection.Count))];
            }

    This can then be used as follows:

    ?View Code CSHARP
    var collectionList = new List<string> {"String 1", "String 2", "String 3"};
    string item = collectionList.GetRandomItem();
  • ContainsAny & EquelsAny

    Posted on November 2nd, 2010 Jamie No comments

    This post is a bit of a reference for myself and also a handy tip for those wondering how to perform Contains and Equels on a collection of strings.

    For the purposes I needed I simply wanted to check if a string Equels Any or Contains Any of an array of strings. I put these together as a couple of extension methods:

    ?View Code CSHARP
    public static bool ContainsAny(this string input, string[] items)
            {
                return items.Any(input.Contains);
            }
    ?View Code CSHARP
            public static bool EqualsAny(this string input, string[] items)
            {
                return items.Any(input.Equels);
            }

    That’s all that’s required.

    This means we can now do things like:

    ?View Code CSHARP
    bool isWomenOrUnisex = category.EquelsAny(new []{"women","unisex"});

    Simples!

  • Passing Parameters to an ActionFilter in ASP.NET MVC

    Posted on August 5th, 2010 Jamie 1 comment

    Creating action filters is quite a simple process. You can simply inherit from ActionFilterAttribute and bingo, you’ve got yourself a custom ActionFilter.

    There’s a lot of articles out in the wilderness of the web that explain how to go about creating custom ActionFilters and most of them are pretty good articles.

    The one thing I’ve not been able to find online is some instruction of how to create action filters that accept some kind of parameters. We already see this concept being used by such things as the OutputCacheAttribute action method where we can pass in a Duration, and various other parameters:

    ?View Code CSHARP
    [OutputCache(Duration = 5)]

    So how do we allow our own custom action filters to accept parameters in this same way?

    The answer seems really simple once you know however a search on Google doesn’t seem to return much in the way of any answers to this question (as with many of my posts, they’re usually things that I haven’t been able to find online already).

    The simple answer is that: Action filter parameters are properties of the action filter.

    Here’s a short example custom action filter that exposes a property which I then use when attaching the attribute to a class.

    ?View Code CSHARP
    public class CustomActionAttrbitue : ActionFilterAttribute
    {
         public int Id {get; set;}
         public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Do some work here.
        }
    }

    and in attaching this attribute to a class:

    ?View Code CSHARP
    [CustomAction(Id = 5)]
    public class MyController  : Controller
    {
     
    }

    Like I said above, once you know that the parameter is a property of the action filter, it seems obvious. Hope this helps someone else looking for this information.

  • Twitter 101 – Open for business

    Posted on July 24th, 2009 Jamie No comments

    Over the past couple of years we’ve seen a huge uptake in people using Twitter. Now the company has decided to release a new page on their website called Twitter 101 for Business – A Special Guide.

    The aim of the new information seems to be to educate businesses on how Twitter can work for them. This is probably a good thing since most people I talk to about Twitter, who don’t use it, usually reply with “I just don’t get it. Why would anyone care what I’m doing?” and it’s a fair point too.

    Now there’s a place business owners can go to find out exactly what Twitter is for and why people might care. All in all I think Twitter have made a good decision doing this. Let’s see how it affects the way more businesses do business in the future.