convert variant array to .net array

hi,
i'm looking for a method to convert a variant array to a .net array.
basically, i have a .net DLL referenced in an Excel (vba) function which passes a variant array.
i need to convert it to a .net array (C# .net 3.5).
the array is passed into the following method:
        public void ConvertArray(object arr)
        {
            // do something
        }
i've read topics about Marshal.GetObjectForNativeVariant but unable to find a example to aid.
thanks,

LVL 6
zell71Asked:
Who is Participating?
 
Meir RivkinFull stack Software EngineerCommented:
public void Create1DTable(object arr)
       {
object[,] myArray = (object[,])arr;
       
            int [] keys = myArray.Cast<object>().Where((n, i) => i % 2 == 0).Select(n => (int)n).ToArray();
            double [] values = myArray.Cast<object>().Where((n, i) => i % 2 != 0).Select(n => (double)n).ToArray();

        }

-> "keys" is integer array of col1 values
-> "values" is double array of col2 values

0
 
Meir RivkinFull stack Software EngineerCommented:
if it is a string as variant array, then use the following code:
 public void ConvertArray(object arr)
        {
              byteArr = (byte[]) arr;
          string data = ASCIIEncoding.ASCII.GetString(byteArr);

        }
0
 
zell71Author Commented:
its an array of floats.
Run-time error: '-2147467262(80004002)':
Unable to cast object of type 'System.Object[,]' to type 'System.Byte[]'
        public void Create1DTable(object arr)
        {
            byte[] byteArry = (byte[])arr;          
        }
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
zell71Author Commented:
i should also point out incase i'm incorrect its a array of values from an Excel range (VBA to c#.net dll):
i.e (col headers not included in range)
Col1 | col2
1           1.1
2           1.2
3           1.3
4           1.4

0
 
Meir RivkinFull stack Software EngineerCommented:
u need to ass the followng namespace:
using System.Linq;
0
 
zell71Author Commented:
thanks! brilliant
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.