Hello all,

I am getting the following run-time error every time I try to run a program I've converted from Lisp to C#:

System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Single[]' to type 'System.Collections.Generic.List`1[System.Single]'."

In the function below the problem is occurring at line 16. The datatypes are all correct and verified elsewhere, so it is not a problem of the mismatched types in the that sense. However, it appears that C# does not allow you to cast an object to a list... I hope that isn't the case, especially since that would mean me rewriting hundreds of lines of code.

Here are portions of the code where I am receiving errors:

public static List<List<int>> run_neural_net()
        {
            initialize_network(5, 5);
            initialize_the_network();
            learn_the_patterns(50);
            List<int> learnedCats_second = new List<int>();
            for (int i = 0; i < learned_categories.Count; ++i)
            {
                learnedCats_second.Add((int)(((List<object>)learned_categories[i])[1]));
            }
            var pairs = pair(input_patterns.ToList<float[]>().ToArray(), learnedCats_second.ToArray());
            var first_5_highest = firstN(5, count_highest(pairs.ToList<List<object>>()));
            var list_to_translate = new List<List<float>>();
            foreach (List<object> list in first_5_highest)
            {
                list_to_translate.Add((List<float>)list[0]);
            }
            //IEnumerable<List<List<float>>> fl = list_to_translate.Cast<List<List<float>>>();
            return translate_into_events(translate_to_pitches(list_to_translate));
        }

Does anyone know how I can get around this error?

You haven't posted enough code to indicate what the problem is. Can you post a snippet of code that will compile and generate the error? From the exception message I can deduce what is happening here but can't recommend the best way for you to fix it. You're trying to cast an float[] to a List<float> when you should be casting it to a float[] and calling .ToList() then add it to your list of lists.

button1_click generates the same exception message you received. button2_click shows how you should handle it.

private void button1_Click(object sender, EventArgs e)
    {
      List<List<float>> list_to_translate = new List<List<float>>();
      object o = new float[] { 1, 2, 3 };
      try
      {
        list_to_translate.Add((List<float>)o);
      }
      catch (Exception ex)
      {
        //Results in:
        //Unable to cast object of type 'System.Single[]' to type 'System.Collections.Generic.List`1[System.Single]'.
        Console.WriteLine(ex.Message);
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      List<List<float>> list_to_translate = new List<List<float>>();
      object o1 = new float[] { 1, 2, 3 };
      object o2 = new float[] { 1, 2, 3 };
      object[] loopArray = new object[] { o1, o2 };
      foreach (object obj in loopArray)
      {
        list_to_translate.Add(((float[])obj).ToList());
      }
    }

I don't think it is possible to cast generic lists like that although object is the base and float inherits object that is not the cas when it comes to generic lists.

I think i'd try somthing like this:

List<object> temp = new List<object>();
temp.Add((float)20.5);
temp.Add((float)21.5);
temp.Add((float)22.5);

List<float> temp2 = new List<float>();

foreach(object obj in temp)
{
     if (obj is float)
    {
        temp2.Add((float) obj);
    }
}
This article has been dead for over six months. Start a new discussion instead.