Cloud and Microsoft technologies enthusiast architect in Switzerland RSS 2.0
# Tuesday, May 17, 2005

Ecrire un composant COM en .NET est relativement simple, pour peu que l'on n'essaie pas de faire des spécialités.
Il y a néanmoins quelques petites choses à se remémorer si l'on veut éviter de perdre un temps précieux, et une de celles-ci concerne les tableaux. Imaginons que nous voulions écrire un composant COM en .NET, utilisable depuis le code VBA d'une feuille Excel. Le code pourrait être :

Public Sub mySub
   Dim myArray() as Double
   ReDim myArray(1 to 10) as Double

   Dim comObject As myCOMObject

   for i = 1 to 10
      myArray(i) = i
   next i

   Set comObject = New myCOMObject

   myCOMObject.MyMethod(myArray)

   Set comObject = Nothing
End Sub

Si vous exécutez ce code, une exception sera levée :
System.IndexOutOfRangeException: Index was outside the bounds of the array.

Ce qu'il faut savoir, c'est que sauf sur mention explicite avec la clause Option Base, les tableaux VBA sont indexés à partir de 0. Or, dans notre cas où l'on veut redéfinir la taille du tableau myArray, il faut se veiller à ne pas indiquer la borne inférieure, et ainsi la laisser à 0.
Du côté du composant COM, les valeurs sont bien entendus disponible à partir de l'indice 1. Ne pas essayer de lire les valeurs à l'indice 0 :

Double [] myLocalArray = new Double[myReceivedArray.GetLength(0)];

for(int i = 1; i <= 10; i++)
{
   myLocalArray[i] = myReceivedArray[i];
}

Writing a .NET COM component is fairly simple. As far as we do not try to do specialties. There are come little tricks to remember before spending lot of time. One of them is the arrays. Let's write a .NET COM component that should be used from a VBA code of an Excel sheet. The code could be :

Public Sub mySub
   Dim myArray() as Double
   ReDim myArray(1 to 10) as Double

   Dim comObject As myCOMObject

   for i = 1 to 10
      myArray(i) = i
   next i

   Set comObject = New myCOMObject

   myCOMObject.MyMethod(myArray)

   Set comObject = Nothing
End Sub

If you run this code, an exception will raise :
System.IndexOutOfRangeException: Index was outside the bounds of the array.

What is important to know about is that VB defines arrays using a 0-based indexes unless you specify the Option Base clause. In that case, I redefined the myArray size, but I had to not specify the lower-bound, letting that bound to 0.
On the COM component side, values are available only from the second cell (index equal to 1). Do not try to read the cell with index 0 :

Double [] myLocalArray = new Double[myReceivedArray.GetLength(0)];

for(int i = 1; i <= 10; i++)
{
   myLocalArray[i] = myReceivedArray[i];
}

Tuesday, May 17, 2005 4:39:25 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Programming | Technical
Google Cloud Platform Certified Professional Cloud Architect
Ranked #1 as
French-speaking SharePoint
Community Influencer 2013
Navigation
Currently Reading :
I was there :
I was there :
I was exhibiting at :
I was there :
I was a speaker at :
I was a speaker at :
I was a speaker at
(January 2013 session):
I was a speaker at :
I was a speaker at :
United Nations (UN) SharePoint Event 2011
I was a speaker at :
I was there !
I was there !
I was there !
I was there !
Archive
<June 2022>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2022
Yves Peneveyre
Sign In
Statistics
Total Posts: 290
This Year: 0
This Month: 0
This Week: 0
Comments: 20
Themes
Pick a theme:
All Content © 2022, Yves Peneveyre
DasBlog theme 'Business' created by Christoph De Baene (delarou)