Serving the Quantitative Finance Community

 
User avatar
PJB
Topic Author
Posts: 0
Joined: April 28th, 2003, 8:10 pm

C# and Handle for an Array

May 5th, 2003, 5:33 pm

Does anyone happen to know how to creat and call a handle that points to an array? Thank you.
 
User avatar
grsmith
Posts: 0
Joined: July 14th, 2002, 3:00 am

C# and Handle for an Array

May 5th, 2003, 7:14 pm

If you want to use safe code you can use an ArrayList. It is dynamic and can hold any type of object.Below is example from Jessie Liberty's Coding in C#. If you want to us pointers and unsafe code I knowthey say you can do it but I have not tried it. If I did not have to leave on a trip in 10 minutes I would tryto help with the unsave code.// Example 09-12: Working with an ArrayListnamespace Programming_CSharp{ using System; using System.Collections; // a simple class to store in the array public class Employee { public Employee(int empID) { this.empID = empID; } public override string ToString() { return empID.ToString(); } public int EmpID { get { return empID; } set { empID = value; } } private int empID; } public class Tester { static void Main() { ArrayList empArray = new ArrayList(); ArrayList intArray = new ArrayList(); // populate the array for (int i = 0;i<5;i++) { empArray.Add(new Employee(i+100)); intArray.Add(i*5); } // print all the contents for (int i = 0;i<intArray.Count;i++) { Console.Write("{0} ", intArray.ToString()); } Console.WriteLine("\n"); // print all the contents of the button array for (int i = 0;i<empArray.Count;i++) { Console.Write("{0} ", empArray.ToString()); } Console.WriteLine("\n"); Console.WriteLine("empArray.Capacity: {0}", empArray.Capacity); } }}
 
User avatar
PJB
Topic Author
Posts: 0
Joined: April 28th, 2003, 8:10 pm

C# and Handle for an Array

May 5th, 2003, 7:23 pm

Thank you, grsmith. I may not make myself clear in my question. I actually want to develop an Excel addin function which can return a handle to an excel cell and then I need call this handle in another function. I want the handle to point to an array of structs (not simple numbers). Or it could be a two dimensional array. Have a nice trip.
Last edited by PJB on May 4th, 2003, 10:00 pm, edited 1 time in total.
 
User avatar
Boofta
Posts: 0
Joined: July 14th, 2002, 3:00 am

C# and Handle for an Array

May 6th, 2003, 9:17 am

PJB, are you trying to store non-atomic (ie int, double, string) data in an Excel cell? Outright, Excel does not allow it, which is a shame, because it would be awesome if you could use cells as a visual handle for manipulating objects... That's not to say it hasn't been tried I just wish the Excel understood the notion natively.
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

C# and Handle for an Array

May 6th, 2003, 9:33 am

You would like class with a Get/Set pair that allows you to store a "pointer" to a given cell ?An object of this class can then be passed to a function that does things with it ?Used something like=Set_Victim("A1")=Use_CurrentVictim()At the risk of saying the obvious, can't you simply pass around the string that defines the range ?
 
User avatar
PJB
Topic Author
Posts: 0
Joined: April 28th, 2003, 8:10 pm

C# and Handle for an Array

May 6th, 2003, 11:47 am

Thank you, guys. Storing and passing the address of the range is actually recommended by MS. But I do not want to display a big array in the spreadsheet. It's not necessary and traders do not have to read and change the array. My current solution is to use C# Marshal methods (AllocCoTaskMem and Copy) to return an IntPtr. However, the Marshal method can only return an IntPtr to a one-dimensional array and my array is two-dimensional. So I generate an IntPtr for each column and then return an IntPtr to these IntPtr's. When I call the array, I pass the IntPtr to my function, get the array of IntPtr's and through them access the array output. This works. But I am not sure whether there's any memeory problems and if any, how to resolve them. Calling AllocCoTaskMem requiring calling FreeCoTaskMem to release the memeory. I am not sure when to free the memeory. Thanks again for your suggestions.