Hi,
Would you please help? I have been struggling for two weeks without any success. I try to convert System._COMObject Type to Array type. It worked in VS2008 however when I change to VS2010 it doesn't.
int[] myLengthsArray = new int[1] { nMem };
int[] myBoundsArray = new int[1] { 1 };
myDs = Array.CreateInstance(typeof(double), myLengthsArray, myBoundsArray);//Depth D
Array myDs;
private void PopulateArrays(Multiframe.SectionList mySectionList)
{
myDs = (Array)mySectionList.D;
}
I also try Array.ConvertAll but that didn't work either
myDs = Array.ConvertAll
Loading
Maxime pPosted Apr 28, 2016, 10:22 AM
VulpesPosted Apr 11, 2011, 10:09 AM
PhungPosted Apr 11, 2011, 9:51 AM
Object Browser show the type of mySectionList.D at runtime is dynamic{float[]}
VulpesPosted Apr 10, 2011, 8:10 AM
VulpesPosted Apr 10, 2011, 6:19 AM
PhungPosted Apr 9, 2011, 9:41 PM
Apologies for the confusion.
I'm trying to connect to Multiframe via COM. The COM object (Multiframe.SectionList mySectionList) returns a single dimension array of type float with the first element of the array having index 1 rather than 0.
This is why I have to create an array with first index being 1 rather than 0 (see CreateArray)
Class SectionListArray
{
Array myDs;
public SectPropArray(){}
~SectPropArray(){}
public void Add(ref Multiframe.SectionList mySectionList)
{
int nMem = myMemberList.Count;
CreateArrays(nMem);
FillArrays(mySectionList);
}
private void CreateArrays(int nMem)
{
int[] myLengthsArray = new int[1] { nMem };
int[] myBoundsArray = new int[1] { 1 };
myDs = Array.CreateInstance(typeof(double), myLengthsArray, myBoundsArray);//Depth D
}
private void FillArrays(Multiframe.SectionList mySectionList)
{
myDs = (Array)mySectionList.D;
}
}
When I converted from VS2008 to VS2010 there were no warnings or errors. The exact code executed perfectly in VS2008. The error message is Unable to cast object of type 'System.Single[*]' to type 'System.Single[].
Vulpes:
I tried your code too but the error pops up at line Array temp = (Array)mySectionList.D;
It is a runtime error not a compiling error.
I upgraded the code to VS2010 to utilise Parallel Programming features in .NET 4 but I may have to revert back to VS2008 until the problem is sorted.
Please assist!
Thank you
Phung
VulpesPosted Apr 9, 2011, 4:01 PM
Sam HobbsPosted Apr 9, 2011, 3:40 PM
I hope the problem is solved by copying the data but if that is what is needed then I don't understand why that was not needed for VS 2008. My suspicion is that there is a problem with the COM object or whatever it is.
VulpesPosted Apr 9, 2011, 3:25 PM
The error message is telling you, Rob, that you can't convert a single dimensional float array with an unknown lower bound to a single dimensional float array with a lower bound of zero.
If this is in fact what you're trying to do, then you'll need to copy the elements using GetValue() because a cast won't work.
I imagine mySectionList.D is a System.__COMObject which actually represents a single dimensional float array with a lower bound of 1.
So, if you want to copy that to a zero-based single dimensional double array (I notice you're not using float), I'd try the following code:
Array temp = (Array)mySectionList.D;
double[] myDs = new double[temp.GetLength(0)];
for(int i = 1; i <= temp.GetLength(0); i++)
{
myDs[i - 1] = (float)temp.GetValue[i];
}
Sam HobbsPosted Apr 9, 2011, 3:13 PM
Whenever you say something doesn't work it is nearly always necessary to be specific. Please be specific about what the error is or what does not work or whatever. Also, where does the System._COMObject Type come from?
Also, are you totally sure that your program is checking for errors? Have you used the debugger to ensure that there is valid data?