1

I have a list derived from

public class main
{
     public list<myclass> data  { get; set; }
}

 public class myclass
    {
        public string variety { get; set; }
        public string ordertype { get; set; }
        public string producttype { get; set; }
    }

Now I want to convert the List to return an array for VBA interop, how can I loop through my class in a single statement to convert all elements to array in one go. I have a few other classes that have a huge number of elements. I've tried the below code, but it's throwing out of bound error. I need to automatically loop through elements in myclass and assign it to array and so on. Is there any alternative/one-liner statement for this to convert the entire list to array.

string[] NamesArray = new string[list.Count];
string[] NamesArray2 = new string[] { };
for (int i = 0; i < NamesArray.Length; i++)
{
 int idx = 0;
 NamesArray[i] = bres.data[i].ToString();//here I am getting the myclass list not the elements inside the myclass.
foreach (var k in NamesArray[i].)
  {
     NamesArray2[idx++] = k.value.ToString();
  }
 }
braX
  • 11,506
  • 5
  • 20
  • 33
Kiran
  • 167
  • 1
  • 9
  • 1
    I guess `list.Count` != `bres.data.Count` - but you aren't showing what `list` is. Maybe you wanted to use `bres.data` instead of `list` so `string[] NamesArray = new string[bres.data.Count];` – Rand Random Mar 08 '22 at 15:25
  • 1
    Does this answer your question? [c# Paradox: Converting List to Array is Iterating more efficient?](https://stackoverflow.com/questions/44395265/c-sharp-paradox-converting-list-to-array-is-iterating-more-efficient) – HackSlash Mar 08 '22 at 16:53

1 Answers1

6

Why don't you try doing:

myclass[] arr = data.ToArray();

Edit: To return the array so it's visible from VBA, you'd need to have your class as ComVisible.

[ComVisible(true)]
public class main
{
    public list<myclass> data  { get; set; }
}
[ComVisible(true)]
public class myclass
{
    public string variety { get; set; }
    public string ordertype { get; set; }
    public string producttype { get; set; }
}
[ComVisible(true)]
public myclass[] myclasses()
{
     myclass[] arr = data.ToArray();
     return arr;
}

@freeflow has a great reference link that you can use: https://analystcave.com/excel-use-c-sharp-in-excel-vba/

Erik Salazar
  • 111
  • 5
  • 2
    Not enough code, I am getting paid per line. jk – Rand Random Mar 08 '22 at 15:27
  • Generic Types are not permitted in VBA so you can't pass back List myClassData. So the simplest way would be to declare myClass as com visible so that you can work with myClass objects in VBA, then do an explicit cast of List to an Array of myclass and then pass back the array into a variant in VBA using a function that returns a dynamic type. – freeflow Mar 08 '22 at 15:54
  • @freeflow can you just post sample code, so that I can implement the same. Actually, I am new to c# so connecting the dots. – Kiran Mar 08 '22 at 16:46
  • 1
    There are plenty of examples if you google. Here is one for a starter. https://analystcave.com/excel-use-c-sharp-in-excel-vba/ – freeflow Mar 08 '22 at 17:19
  • @Erik Salazar, I was able to convert the List to an array of type myclass, but how can I access the elements of myclass to get accessible from VBA. – Kiran Mar 08 '22 at 17:22