I am trying to implement grenade's response from thsi thread on how to randomize a list: Randomize a List<T>. The solution includes creating an extended list. Here is the exact way it's written in my own code:
static class MyExtensions
{
static readonly Random Random = new Random();
public static void Shuffle<T>(this IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
The problem with this is that when I try to run the method in a button click event on a list I created when said event is triggered, VS does not recognize the method and produces this error:
System.Collections.Generic.IList does not contain a definition for 'Shuffle' and no extension method 'Shuffle' accepting a first argument of type 'System.Collections.Generic.IList could be found..."
Here is my attempted usage for reference:
public void Button1_Click(object sender, EventArgs e)
{
IList<int> dayList = new List<int>();
for (int i = 0; i < 32; i++)
{
dayList.Add(i);
}
dayList.Shuffle();
More code...
}
I've searched these boards and found that I need to declare the namespace in which the extension method is, but mine is inline with the rest of my page, so there is no namespace to declare. Suggestions?