Hello all,
I've consulted several books on C# but haven't found a suitable answer to the following: How does one create a dynamic 1 and 2-dimensional array of floats that can be resized at runtime based on some user-selectable parameter WITHOUT using ArrayList In my past C++ days I've always just used the looped " float** " approach. My requirement now is to pass this C# array to a legacy C++ DLL using P/Invoke...I understand it's tricky using this mechanism and therefore want to avoid collections (ArrayList) and stick with something simple - the unmanaged function call takes a float** argument.
Thanks for any and all help!

Dynamic array of floats?
DOSrelic
I would be tempted to say "use a generic list": List<float> provides both good performance and the ToArray() method that will return a float[] whenever you need to call your dll.
Anyway, something in your post makes me wonder. What do you mean exactly by "resized at runtume based on some user-selectable parameter" If you means something like (C):
float* myArray = (float*) malloc (numberOfElements * sizeof (float));
then you can simply write (C#)
float [] myArray = new float [numberOfElements];
HTH
--mc
Peter Lillevold
Hi LKeene,
Yes, you can create bidimensional arrays like that... and yes, generic lists are a new feature of 2.0
As for your last question, I'm not sure it's the best way, but you can get an array from the list with a single call to List<T>.ToArray(), and simply pass the array.
HTH
--mc
FSchmid
I'd like to avoid ArrayList because I think the overhead will be a problem (the app is a little performance sensitive - I've hand written some fast functions in C that I'd like to use in C#).
I found the following article:http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnnetcomp/html/netcfmarshallingtypes.asp
Granted it deals with compact framework but I think the same will work in "regular" .net framework. From the article:
"Array parameters have different representations in managed and unmanaged environments. In C/C++, arrays are representations as pointers to a contiguous region of memory with the array elements addressed as offsets from this pointer, starting with zero. The marshaler in the .NET Compact Framework Common Language Runtime (CLR) ensures that managed arrays adhere to this format when passed to unmanaged code. In this C/C++ example, an array is passed into a function and is searched to locate the smallest value:
extern "C" _declspec(dllexport) int MinArray(int* pData, int length) { // Initialise minData to the first element of the pData Array int minData = pData[0]; int pos; // Loop through the array for(pos = 1; pos < length; pos++) { // If the current element is less than minData, // set minData to the value of current element if(pData[pos] < minData) minData = pData[pos]; } return minData; }The following code declares the above method and calls it from C#:
[DllImport("MarshalArray.dll")] extern static int MinArray(int[] pData, int length); . . . int[] sampleData = int[] {5, 1, 3 }; int result = MinArray(sampleData, sampleData.Length); MessageBox.Show(String.Format("Smallest integer is {0}, result));IGiberson
Or am I barking up the wrong tree
Wayne R
Thanks Mario, that is indeed what I was getting at. Can I assume the following is correct as well:
float [,] myArray = new float[numberOfElements,numberOfElements];
Generics are a new feature introduced in 2.0 aren't they What are it's P/Invoke data marshalling issues i.e. does it have an equivalent type in unmanaged code
Thanks everyone.