Click here to Skip to main content
12,887,536 members (26,975 online)
Rate this:
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.
Posted 18-May-11 4:22am
Updated 18-May-11 8:33am
Dalek Dave433.3K
Dalek Dave 18-May-11 14:34pm
Edited for Grammar and Readability.
Rate this: bad
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;
JoleynSephev 18-May-11 10:27am
I have tried that already and I'm afraid it doesn't work.
Marcus Kramer 18-May-11 10:49am
Actually, it will work.
Rate this: bad
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
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.
Marcus Kramer 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
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)
Rate this: bad
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.
Rate this: bad
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.

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

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 18 May 2011
Copyright © CodeProject, 1999-2017
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