OXO OXO - 21 days ago 5
C# Question

HIDLIbrary compile error

HIDLibrary.HidDevice[] HidDeviceList;
HidDevice HidDevice;

// Enumerate the devices with the Vendor Id
// and Product Id of the IT4600
HidDeviceList = HidDevices.Enumerate(VID, PID);


This code fails in compile at the last line with this error


Error 5 Cannot implicitly convert type
'System.Collections.Generic.IEnumerable' to
'HidLibrary.HidDevice[]'. An explicit conversion exists (are you
missing a cast?)


What cast do I need?

Answer

As the error message says, you cannot assign an IEnumerable to an array. You need to call ToArray() to turn your IEnumerable into an array. Since this is a non-generic implementation you most likely need to call Cast<T>() as well. It would look something like

HidDeviceList = HidDevices.Enumerate(VID, PID).Cast<HidDevice>().ToArray();