3

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


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

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

It is not possible in C# to create an array that starts at an index other than 0. (See the link below.) In my opinion that will leave the following two possibilities:

1. Create a doubleArray from 0 to 100

I could create an doubleArray as follows:

Double[] doubleArray = new Double[100];

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

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

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

I Could create a doubleArray as follows:

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

In my opinion this should be the 'cleanest' way to overcome the problem, without unnecessary memory usage. In VBA the array uses [5] to [100], that makes the length 96. The same is the case for this option ( ((100 - 5) + 1) = 96 ).

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


My question is thus, how to handle with this situation?

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


For some more background information. A follow-up from my earlier question: Vba Type-Statement conversion to C#

4
  • I'd do #2, for the reasons you gave. Commented Aug 7, 2015 at 14:41
  • You could create a wrapper around the array with an indexer that could apply the offset. Optionally providing a way to use conventional 0-based indexing. Commented Aug 7, 2015 at 14:53
  • Technically you can create arrays from C# with non-zero lower bounds. I'd probably work to fire anyone who introduced such code into my workplace though. Commented Aug 7, 2015 at 14:54
  • @Damien_The_Unbeliever Like I said below, I did not write the source. Commented Aug 7, 2015 at 15:15

2 Answers 2

3

Three steps:

  1. Create unit tests around every instance of an array in VBA.

  2. Adjust all the VBA arrays to be zero based, checking compliance with your unit tests.

  3. Port to C#.

I'd be keen to avoid introducing errors across language boundaries, as it's more difficult to reconcile issues.

Sign up to request clarification or add additional context in comments.

2 Comments

I like your solution very much, but it can be very hard to make the arrays zero based, since for example; 25 is the age, so at index [25] the double value is the result for somebody with age 25. (I did not write the source code myself, else I would've used classes.)
That's what unit tests are for. Zero based arrays in C# are so idiomatic that everyone will expect a 1 + age notation for that particular array.
1

I'd go for #2 but here is another solution I thought for you.

You can also do a custom class, with just an array inside. You will do 3 methods:

  1. one to set an element( myObject.setElement(6,0.12) )
  2. one to get an element( myObject.getElement(6) )
  3. the first one to set the size of the array ( myObject.setSize(5,100) )

Actually when you will get or set the element, you will go to the index (index-5)

Example of 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's been a year that I didn't program in C# so there might be a few syntax error but the code is for the better understanding)

2 Comments

I get the idea, it is like the wrapper class Jeff Mercado means. I will certainly try it!
One note to go with the wrapper class, it can seriously effect the performance of an application with some larger multidimensional arrays!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.