Converting Arrays - From VBA to C # (Best Practice)

I have converted about 4000-5000 lines of code from VBA to C #. The only problem I am having is the following:


In VBA, you can create an array of type and also set a starting index. For example:

//This creates an Array with an index 5 to index 100.
doubleArray(5 To 100) As Double

      


In C # it is not possible to create an array starting at an index other than 0. (See link below.) In my opinion, this would leave the following two possibilities:

1. Create a doubleArray from 0 to 100

I could create a doubleArray like this:

Double[] doubleArray = new Double[100];

      

This has the advantage that I can set the index using the index number described in the VBA code. This means the following: The VBA code assumes that the array exists from [5] to [100] and I can also set the number from 5 to 100 in C #.

This is pretty messy in my opinion, because you will always create arrays with unused memory (in this case, index [0] for index [4]). Because of this, I was thinking about the second option.

2. Create a doubleArray from 0 to ((topindex - bottomindex) + 1)

I could create a doubleArray like this:

Double[] doubleArray = new Double[((topindex - bottomindex) + 1)];

      

In my opinion, this should be the "cleanest" way to overcome the problem, without wasting memory. In VBA, the array uses [5] - [100], which makes the length 96. The same goes for this option (((100 - 5) + 1) = 96).

However, the big drawback is that this would also require you to skip all the code that has already been written. If any function calls doubleArray [97], it must be doubleArray [((97 - 5) + 1)].


My question is thus how to deal with this situation?

If anyone else sees another option / opportunity, suggestions are more than welcome.


For more information. Following from my previous question: Converting Vba type to C #

+3


source to share


2 answers


I would go for # 2, but here is another solution I was thinking for you.

You can also create your own class containing only the array inside. You will be doing 3 methods:

  • one for setting the element (myObject.setElement (6,0.12))
  • one to get the element (myObject.getElement (6))
  • the first one to set the size of the array (myObject.setSize (5100))

In fact, when you get or set an element, you will go to index (index-5)



An example of a class you can do

    public class CustomArray {
      private Double[] myArray;
      private int spread;

      public CustomArray(){
        setSize(5,100);
      }

      public void setSize (int lowerIndex, int upperIndex){
        myArray = new Double[(upperIndex-lowerIndex)+1]
        spread = lowerIndex
      }

      public Double getElement(int index){
        return myArray[index-spread];
      }

      public void setElement(int index, Double element){
        myArray[index-spread] = element;
      }
    }

      

(PS: It was a year when I was not programming in C #, so there may be a few syntax errors, but the code is for better understanding)

+1


source


Three steps:

  • Create unit tests around each instance of an array in VBA.

  • Correct all VBA arrays to zero based checking against your unit tests.

  • Port in C #.



I would be very interested in not introducing errors into language boundaries as it is more difficult to come to terms with problems.

+2


source







All Articles