473,466 Members | 4,869 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array index

Ben
I am having a problem with a web service I've created getting an "
Index was outside the bounds of the array." when I try to get the
value of ET[i].TerritoryName below. I've verified the length of ET is
4 after reallocating the dynamic array and the error happens when i =
0 so the first iteration of the loop. Does anyone have any idea what
the problem is? I know it's something simple I'm just not seeing.

EventTotalDS[] ET = null;
cnn.Open();

SqlCommand cmd = new SqlCommand("BP_SREventTotals", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p_ContactGUID", SqlDbType.UniqueIdentifier).Value
= ContactGUID;

SqlDataAdapter oDA = new SqlDataAdapter(cmd);
DataSet oDS = new DataSet();

oDA.Fill(oDS,"EventTotals");

ET = new EventTotalDS[oDS.Tables["EventTotals"].Rows.Count + 1];

int i = 0;
foreach(DataRow dtRow in oDS.Tables["EventTotals"].Rows)
{
ET[i].TerritoryName = dtRow["TerritoryName"].ToString();
//above line gets index error on the left hand side
i++;
}

Mar 7 '07 #1
5 1604
You have allocated the array for the EventTotalDS "references", but you have
not actually allocated the EventTotalDS objects themselves.

So you have allocated say 4 "slots" to hold EventTotalDS "pointers", but
each of those slots is initially empty (null).

That's why it crashes on the first index (0).

So put something like:

ET[ i ] = new EventTotalDS( );
before
ET[i].TerritoryName = dtRow["TerritoryName"].ToString();

Kevin

"Ben" <yo*******@gmail.comwrote in message
news:11**********************@c51g2000cwc.googlegr oups.com...
>I am having a problem with a web service I've created getting an "
Index was outside the bounds of the array." when I try to get the
value of ET[i].TerritoryName below. I've verified the length of ET is
4 after reallocating the dynamic array and the error happens when i =
0 so the first iteration of the loop. Does anyone have any idea what
the problem is? I know it's something simple I'm just not seeing.

EventTotalDS[] ET = null;
cnn.Open();

SqlCommand cmd = new SqlCommand("BP_SREventTotals", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p_ContactGUID", SqlDbType.UniqueIdentifier).Value
= ContactGUID;

SqlDataAdapter oDA = new SqlDataAdapter(cmd);
DataSet oDS = new DataSet();

oDA.Fill(oDS,"EventTotals");

ET = new EventTotalDS[oDS.Tables["EventTotals"].Rows.Count + 1];

int i = 0;
foreach(DataRow dtRow in oDS.Tables["EventTotals"].Rows)
{
ET[i].TerritoryName = dtRow["TerritoryName"].ToString();
//above line gets index error on the left hand side
i++;
}

Mar 7 '07 #2
Ben
That was exactly it! It always helps to have an extra set of eyes.
Thank you very much.

Mar 7 '07 #3
Ben <yo*******@gmail.comwrote:
That was exactly it! It always helps to have an extra set of eyes.
Thank you very much.
That doesn't explain why you were getting an
ArrayIndexOutOfBoundsException though. You should have been getting a
NullReferenceException.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '07 #4
Ben
On Mar 7, 5:00 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Ben <your2s...@gmail.comwrote:
That was exactly it! It always helps to have an extra set of eyes.
Thank you very much.

That doesn't explain why you were getting an
ArrayIndexOutOfBoundsException though. You should have been getting a
NullReferenceException.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
This was in a web service and that was the error it was returning.
After I posted this thread I pulled the code out to a windows app to
test and started getting "object reference not set to an instance of
an object". I'm not sure why the web service returned the index error
which is why I had a hard time finding the problem.

Mar 7 '07 #5
Good point! I didn't even read what the actual exception was...

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Ben <yo*******@gmail.comwrote:
>That was exactly it! It always helps to have an extra set of eyes.
Thank you very much.

That doesn't explain why you were getting an
ArrayIndexOutOfBoundsException though. You should have been getting a
NullReferenceException.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
2
by: Ben Katz | last post by:
Is it possible to have a JavaScript object that works just like a standard Array, except that when the array is modified, a function gets called which can then do some processing on the array? ...
1
by: Ben Katz | last post by:
Is it possible to have an Array object (or an object derived from Array) that is 'aware' of other code modifying its contents? I'd like to have such an "onModify" function process an array...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
41
by: puzzlecracker | last post by:
Given an array of size n and populated with consecutive integers from 1 to n i.e. in random order. Two integers are removed, meaning zero is placed in their places. Give O (n) efficient algorithm...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
16
by: Frederick Gotham | last post by:
Inspired by page 219 of Nicolai M. Josuttis's book, I set out to write a class for an intrinsic array which would behave, to as far an extent as possible, like a container. Also though, I wanted no...
4
by: BravoFoxtrot | last post by:
Hi, I'm trying to build an index into a multi dimensional associative array. I may not know how many dimensions there are so i want to pass the array indexes as a variable. $arrayToAccess =...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.