[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 881
  • Last Modified:

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,

0
zell71
Asked:
zell71
  • 3
  • 3
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:
u need to ass the followng namespace:
using System.Linq;
0
 
zell71Author Commented:
thanks! brilliant
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now