Hi Bhavna:
Bhavna said:
Hello,
Does anyone know what this message could be regarding??
The application seems to fall over on the following line..
Dim variable1 As SqlString = Arraylist1(i)
The first time the code runs everything is ok. The problem seems to occur
when the application is run through a second time.
here is the error message im getting.....
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
For some reason, on the second pass, something different must be happening.
Like Jon mentions, this is a pretty specific error and I'd recommend a few
things....
Every method doesn't need to have a try catch and depending on the
circumstances, you don't always want code wrapped in try/catch clauses.
However, Something should probably catch the exception somewhere up the
chain. In this instance, you aren't doing any bounds checking before you
make the reference which invites such problems. granted it's fairly
straightforward to prevent index out of range problems by really
understanding your code, but you should probably verify that you have a good
index before you reference it in the spirit of defensive programming.
Anyway, try this.....Before you call the statement which is throwing the
exception,
Debug.Assert(i <= ArrayList.Count-1) or something like If (i <=
ArrayList.Count AND i > -1) Then
Dim variable1 As SqlString = Arraylist1(i)
End If
It really depends on the whole program and coding strategy. If you opt for
a check before the reference, then you know you'll have a valid index. on
the other hand, you can check everythign under the sun which can be overkill
in situations where you can be quite sure that you have a valid index to
begin with. In such cases, using Assertions liberally will alert you of any
problems before you throw the exception.
There are a lot of things that could be happening on the second pass that
didn't happen the first time which could be causing this. It'd be impossible
to tell based on that code snippet what it could be, but check the Count of
the Array List and it will probably tip you off to the problem. If not, I'd
starting asserting preconditions throughout my code just to be sure that
everything I'm assuming to be true is a correct assumption.
HTH,
Bill