Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
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#:
 
            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 18-May-11 4:22am
Edited 18-May-11 8:33am
Dalek Dave432.5K
v3
Comments
Dalek Dave at 18-May-11 14:34pm
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
double[] gAGA8Xj = new double[21];
gAGA8Xj[0] = Methane;
... blah blah ...
gAGA8Xj[20] = Argon;
  Permalink  
v2
Comments
JoleynSephev at 18-May-11 10:27am
   
I have tried that already and I'm afraid it doesn't work.
Marcus Kramer at 18-May-11 10:49am
   
Actually, it will work.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
John Simmons / outlaw programmer at 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 at 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.
Marcus Kramer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is also the assumption that you declared all the other variables correctly.
(Methane, nitrogen etc as double)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Incidentally, your declaration is not as elegant as it could be...
 

Double[] gAGA8Xj = {Methane, Nitrogen, CarbonDioxide...Helium, Argon};
 
This negates the indexing problem altogether.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

hmmmmmmm then try not assigning the values index by index, use initializing list and omit the array size as
 
 
double[] array = new double[] { 1, 2, 3, 4, 5, 6 };
 
 
For your case
 
 
double[] gAGA8Xj = new double[]{ Methane, Nitrogen, . . . ., Argon };
 
 
if it is index out of range exception, other two solutions are also right.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 480
1 mhegazy94 460
2 Kornfeld Eliyahu Peter 315
3 Ravi Bhavnani 240
4 Shemeemsha RA 185
0 Sergey Alexandrovich Kryukov 6,890
1 OriginalGriff 6,761
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 18 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100