TruthStands
TruthStands

Reputation: 93

"Index was outside the bounds of the array" when checking non-array variable

I have a class "Address" that has the following check:

if(thisAddress == null)
   thisAddress = new Address();

When the code is run I get "Index was outside the bounds of the array" on the first line. If I remove the IF statement I get the error on the second line.

The class comes from Linq to SQL, but I have extended it. This worked before, I'm not sure why it started happening all of a sudden. thisAddress is a private variable in a UserControl.

Any ideas?

Upvotes: 0

Views: 2866

Answers (3)

TruthStands
TruthStands

Reputation: 93

Figured it out. Apparently I left out a crucial piece of information. The code is inside the get {} section for a control property. The error was a different line in the code (where I was using a split()), but the debugger pointed to the first line of the get{} statement.

Upvotes: 0

Tollo
Tollo

Reputation: 537

Typically linq statements are executed lazily, or when they are used rather than when you wrote the link. Therefore it is entirely possible that the code executing behind the If statement is actually where the fault lies.

If this is the case then you could try stepping into the statement (it could be using an equality check on the thisAddress class) and the debugger should show you the linq being executed.

An alternative and what I use mostly, is setting the debugger to break when exceptions are thrown rather than when they are unhandled, this is a pretty good way of tracking down this sort of issue. (Debugger/Exceptions tick when thrown in VS).

hope that helps

Upvotes: 0

shahkalpesh
shahkalpesh

Reputation: 33474

The code is not in sync with the binary.
Try recompiling the assembly that contains the usercontrol.

Has anything changed in the DB that you think can break the LINQ to SQL mapping?

Upvotes: 1

Related Questions