StartIndex Error When Changing Child Within Collection Add

StartIndex Error When Changing Child Within Collection Add

Old forum URL: forums.lhotka.net/forums/t/3236.aspx


MadGerbil posted on Friday, July 20, 2007

I dunno why this would be but when I change a property on a child object within the Add routine of a collection class I get an error involving the startIndex on the PropertyHasChanged() sub procedure.

For example, I've a LineItem that is part of a LineItems collection.   When the user successfully creates a new LineItem object (ASP.NET) the item is added to the LineItems collection via LineItems.Add(byval child as LineItem).

Within the Add routine there was the following line of code:

child.LineNumber = Me.Count + 1

This is my way of autonumbering line items.  For each record there are line items numbered 1-10.  When the LineNumber property is changed from 0 to 1 and the PropertyHasChanged() fires the startIndex error is thrown.  The LineNumber property is of type Integer.

So is there something that prevents a property on a child object from being changed within a collection class?   If I move the code to outside of the collection class to the line before the Add is called (code behind on the webpage) I can set the line number and add the child to the collection with no errors.

One other odd item, this error only occurred after the silly project was published.  The error was never raised in the development environment.  All other data access/add child stuff works just fine.

RockfordLhotka replied on Friday, July 20, 2007

What are you returning from the child object's GetIdValue() method? Remember that the return value must be unique within the collection.

sgraham replied on Friday, June 18, 2010

Did anybody ever figure this bug out?

I have a very similar issue.  The only difference is that mine crops up when I try to READ properties of the child block from the Add method of the collection.

After some research, I've found that this line in the CanReadProperty method of the BusinessBase.cs class is causing the issue:

 

 

 

string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);

I assume it has something to do with the Substring(4) but can't figure out why it's crashing.

I am also seeing that the issue DOES NOT appear during debug and only appears after compiling and deploying the app.

Here are the rest of the issue details:

************** Exception Text **************
System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex
   at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
   at System.String.Substring(Int32 startIndex)
   at Csla.Core.BusinessBase.CanReadProperty(Boolean throwOnFalse) in C:\DSG\ITS\ITA\ART\csla20cs\Csla\Core\BusinessBase.cs:line 373
   at DSG.PromotionPlanning.BizObj.VersionBlocks.Add(Point maxPoint, Int32 storeCount) in C:\DSG\MKT\ADV\APC\DSG.PromotionPlanning\DSG.PromotionPlanning.BizObj\Classes\VersionBlocks.cs:line 56

For now, I just have to comment out the CanReadProperty check in my get methods of my child object so the issue goes away.  But, that is not my preferred resolution, obviously.

RockfordLhotka replied on Friday, June 18, 2010

That is ancient code, which I obsoleted a few years ago because it isn't reliable on 64 bit environments (because the 64 bit JIT compiler added some optimizations that prevent it from working). You must be using a pretty old version of CSLA.

Use the CanReadProperty() overload that requires the property name be passed in explicitly. And/or upgrade to a more current version of the framework - particularly 3.5 and higher, which provides a more elegant solution than just passing in the string property name.

sgraham replied on Friday, June 18, 2010

Yep.  I'm still on the old stuff.  Just not much time in the project plan to upgrade at this point.  Hope to soon though.  Using 2.x

I'm using the overload and it's now working.

Thanks Rocky!

Copyright (c) Marimer LLC