9

I have an enum that has 4 values:

public enum DriveRates 
{ 
    driveSidereal = 0,
    driveLunar = 1, 
    driveSolar = 2, 
    driveKing = 3 
} 

I have an array of values that I want to cast to an array of DriveRates. However when I do var rates = (DriveRates[])ret;, with ret being an object array of numbers (probably integers), it says Unable to cast object of type 'System.Object[]' to type 'ASCOM.DeviceInterface.DriveRates[]'.

ret={0,1,2,3}. How should I do this instead. Again, I am trying to convert an array of enum values to an array of enum...well, values :) But I'm trying to convert from type object[] to type DriveRates[].

3 Answers 3

26

You can't just cast the array, if it's really an object[]. You can create a new array pretty easily though:

var enumArray = originalArray.Cast<DriveRates>().ToArray();

If it were actually an int[] array to start with, you could cast - although you'd have to talk nicely to the C# compiler first:

using System;

class Program
{
    enum Foo
    {
        Bar = 1,
        Baz = 2
    }

    static void Main()
    {
        int[] ints = new int[] { 1, 2 };
        Foo[] foos = (Foo[]) (object) ints;
        foreach (var foo in foos)
        {
            Console.WriteLine(foo);
        }
    }
}

The C# compiler doesn't believe that there's a conversion from int[] to Foo[] (and there isn't, within the rules of C#)... but the CLR is fine with this conversion, so as long as you can persuade the C# compiler to play along (by casting to object first) it's fine.

This doesn't work when the original array is really an object[] though.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm...Yes, it's an array of objects. Apparently an array of objects can't be converted to an array of ints.
@ArlenBeiler: Well it can be converted to a new array - but the existing array can't be just treated as an array of ints, as they have different representations.
It's worth noticing, that even is and as operators may get confused with types of enum arrays, see the last snippet at stackoverflow.com/a/21186137/717732 - all three checks say 'true', while .GetType() of course says not.
1

This isn't possible. There is no way to cast between an array of reference types and an array of value types. You will need to manually copy the elements into the new array

DriveRates[] Convert(object[] source) { 
  var dest = new DriveRates[source.Length];
  for (int i = 0; i < source.Length; i++) { 
    dest[i] = (DriveRates)source[i];
  }
  return dest;
}

Comments

0

...or with linq, especially if you need to do some extra things with every element in one line:

DriveRates[] enumArray = originalArray.Select(o => (DriveRates)o).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.