views:

223

answers:

2

I'm having a problem where a collection of objects isn't being accessed correctly when run on a thread from a service. I can run my unit tests fine in VS2008 but when I attach the debugger to the service i can clearly see that it's not starting at the 1 based index but instead at the 0 based index. I've tried everything that I can think of to isolate this and the only difference is that it's in a service and not running as a command line app for example.

When I debug the function I also get a "property evaluation failed" message which I do not when I run it on my unit test.

Please advise.

[EDIT]

I know that arrays normally start from 0. I've always assumed this but today I found this bug and have determined that even though I thought that it started from 0, the debugger shows that it's starts at 1. When I iiterate through the collection it gets the right value in VS but not in the service.

[EDIT]

I'm using Microsoft.VisualBasic.Collection because I can put a key against it. A dictionary object could have also been used but it's now been setup this way and that's how my BLL Base classes and other numerous functions read it so it's not preferable to change it.

[SOLUTION]

I refactored my code using a dicitonary and Generic.KeyValuePair(Of String, Object) so that I wouldn't have to rewrite the code that accesses the keys. Should work fine now as it doens't handle collections int he same way as the collecitons object does.

+2  A: 

All collections in the .NET Framework start at 0, as do arrays. Always.

From your comments, you're using some special Visual Basic collection type. I recommend you get out of that habit, as I believe this type is meant for interoperability with VB6.

John Saunders
They should but for some reason .net adds an entry in the collection object and starts from 1 normally.
Middletone
Insightful, I didn't know that.
Middletone
+4  A: 

By default, .NET languages start with array index 0, except if you specify explicitly to start at 1 for VB.NET.

I do not believe there is a way to enforce this array convention outside of VB assemblies, and as such, when they go through windows remoting or web services they are reverted back to the 0-based index convention.

For the sake of your sanity, I recommend that you refactor all your code to use 0-indexed collections and arrays instead.

Update

Based on your comments you said that you used the Microsoft.VisualBasic.Collection object. The Microsoft.VisualBasic.Collection object uses 1 as its start index. Use an ArrayList object, or better yet a generic List object instead.

Jon Limjap
I always thought that I had. When I debugged it I found that internally it just started from 1 but on the service it starts from 0. I actually didn't know tha tit had even done this until today as I've never had any problems iwht it before.
Middletone
i use the collection because I want to store a key against the object. I could have used a dictionary but didn't and now i really don't want to have tor refactor a ton fo code for the sake of this if possible.
Middletone