Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to convert a program coded in VBA (MS Excel) to C#.
I keep getting an IndexOutOfRange Exception, although I see no probable cause for it.

Here is a snippet of my code in VBA:

VB
Dim gAGA8Xj(21) As Double
    gAGA8Xj(1) = Methane
    gAGA8Xj(2) = Nitrogen
    gAGA8Xj(3) = CarbonDioxide
    gAGA8Xj(4) = Ethane
    gAGA8Xj(5) = Propane
    gAGA8Xj(6) = Water
    gAGA8Xj(7) = HydrogenSulfide
    gAGA8Xj(8) = Hydrogen
    gAGA8Xj(9) = CarbonMonoxide
    gAGA8Xj(10) = Oxygen
    gAGA8Xj(11) = iButane
    gAGA8Xj(12) = nButane
    gAGA8Xj(13) = iPentane
    gAGA8Xj(14) = nPentane
    gAGA8Xj(15) = nHexane
    gAGA8Xj(16) = nHeptane
    gAGA8Xj(17) = nOctane
    gAGA8Xj(18) = nNonane
    gAGA8Xj(19) = nDecane
    gAGA8Xj(20) = Helium
    gAGA8Xj(21) = Argon


and in C#:

C#
double[] gAGA8Xj = new double[21];
gAGA8Xj[1] = Methane;
gAGA8Xj[2] = Nitrogen;
gAGA8Xj[3] = CarbonDioxide;
gAGA8Xj[4] = Ethane;
gAGA8Xj[5] = Propane;
gAGA8Xj[6] = Water;
gAGA8Xj[7] = HydrogenSulfide;
gAGA8Xj[8] = Hydrogen;
gAGA8Xj[9] = CarbonMonoxide;
gAGA8Xj[10] = Oxygen;
gAGA8Xj[11] = iButane;
gAGA8Xj[12] = nButane;
gAGA8Xj[13] = iPentane;
gAGA8Xj[14] = nPentane;
gAGA8Xj[15] = nHexane;
gAGA8Xj[16] = nHeptane;
gAGA8Xj[17] = nOctane;
gAGA8Xj[18] = nNonane;
gAGA8Xj[19] = nDecane;
gAGA8Xj[20] = Helium;
gAGA8Xj[21] = Argon;


I have tried pretty much everything I could think of - changing the base element index to 0, changing the size of the array, adding a null element indexed 0 etc.

Any ideas would be appreciated.
Thanks
Joleyn
Posted
Updated 18-May-11 8:33am
v3
Comments
Dalek Dave 18-May-11 14:34pm    
Edited for Grammar and Readability.

hmmmmmmm then try not assigning the values index by index, use initializing list and omit the array size as

C#
double[] array = new double[] { 1, 2, 3, 4, 5, 6 };


For your case

C#
double[] gAGA8Xj = new double[]{ Methane, Nitrogen, . . . ., Argon };


if it is index out of range exception, other two solutions are also right.
 
Share this answer
 
Incidentally, your declaration is not as elegant as it could be...


Double[] gAGA8Xj = {Methane, Nitrogen, CarbonDioxide...Helium, Argon};


This negates the indexing problem altogether.
 
Share this answer
 
There is also the assumption that you declared all the other variables correctly.
(Methane, nitrogen etc as double)
 
Share this answer
 
An array of size 21 can only handle a max index of 20, as arrays are 0 based, so yourArray[21] is going to cause a smash. Start at 0 and your last entry should be at index 20.

Hope this helps
 
Share this answer
 
v2
Comments
#realJSOP 18-May-11 10:27am    
An array of size 21 can indeed handle 21 elements - it's the index that matters in C# (as you stated).
JoleynSephev 18-May-11 10:35am    
Like I said, I tried that- indexing from 0 to 20 did not work. I also changed the size of the array to bigger numbers but it didn't help. The code is pretty straightforward, I don't know where I could've gone wrong.
fjdiewornncalwe 18-May-11 10:50am    
You've been told where you went wrong. Please accept and apply the answers as given, they are both correct.
Arrays are zero-based. Therefore, your index should start at zero and end at 20 (for a grand total of 21 elements).

EDIT ============

Okay, I'll spell it out for you:

C#
double[] gAGA8Xj = new double[21];
gAGA8Xj[0] = Methane;
... blah blah ...
gAGA8Xj[20] = Argon;
 
Share this answer
 
v2
Comments
JoleynSephev 18-May-11 10:27am    
I have tried that already and I'm afraid it doesn't work.
fjdiewornncalwe 18-May-11 10:49am    
Actually, it will work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900