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

Sam HobbsRank #107 · 20.5K reputationPosted Apr 9, 2011, 11:13 AM EDT
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?
VulpesRank #55 · 29.2K reputationPosted Apr 9, 2011, 11:25 AM EDT
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 HobbsRank #107 · 20.5K reputationPosted Apr 9, 2011, 11:40 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Apr 9, 2011, 12:01 PM EDT
Phung5 reputationPosted Apr 9, 2011, 5:41 PM EDT
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
VulpesRank #55 · 29.2K reputationPosted Apr 10, 2011, 2:19 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 10, 2011, 4:10 AM EDT
Phung5 reputationPosted Apr 11, 2011, 5:51 AM EDT
Object Browser show the type of mySectionList.D at runtime is dynamic{float[]}
VulpesRank #55 · 29.2K reputationPosted Apr 11, 2011, 6:09 AM EDT
Maxime p2 reputationPosted Apr 28, 2016, 6:22 AM EDT