Child_Update/Child_Insert from 3.8 > 4.0

Child_Update/Child_Insert from 3.8 > 4.0

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


NemisisJedi posted on Thursday, May 05, 2011

Hi all, hope you are all doing well.

We have just upgraded to .NET 4 and thought we might as well go full steam ahead and update to Csla 4.0 as well.

One of the problem i have though is that when i populate a child item i would usually pass it a DataReader like so

Dim lList As New List(Of ChildItem)

Dim lChild as ChildItem = ChildItem.GetChild(dr)   ' dr is my data reader

Within this child i would have the following factory method

Friend Shared Function GetChild(ByRef dr As Object) As ChildItem
   DataPortal.ChildFetch(Of ChildItem)(dr)
End Function 

Private Sub Child_Fetch(ByRef dr As Object) 
.........


But in version 4 i get errors with this code and i have to pass everything ByVal instead of ByRef which is bad for memory etc.

I dont really want to pass a whole DataReader by value in each function, so i can only think that i have to pass each value as a separate parameter??

Am i missing something or was this done for a reason?? 

You get the following error by using ByRef

xpression of type 'System.Object&' cannot be used for parameter of type 'System.Object' of method 'Void Child_Fetch(System.Object ByRef)'

 

RockfordLhotka replied on Thursday, May 05, 2011

ByVal and ByRef are essentially meaningless when passing reference types like a data reader.

When you pass an object reference ByVal, you are passing the reference pointer (basically an int) by value to the method. The actual referenced object (the data reader) isn't passed at all - only a pointer is passed.

When you pass an object reference ByRef, you are still passing the reference pointer (basically an int) by reference. The only difference here, is that the method can change the reference to point to a new object, and the calling code would see that as a side-effect.

Anyone who did any FORTRAN in the past knows that side-effect-based APIs are almost always a disaster, which is why you rarely see the use of ByRef semantics in anyone's code, or in the .NET APIs.

CSLA actually does use a ByRef semantic in the LoadProperty and SetProperty methods that support private backing fields. This was a concious choice on my part - but having used FORTRAN years ago, I can tell you I thought twice before implementing that API!!

In any case, my ramblings aside, you are misunderstanding the way ByVal works, and since a data reader is a reference type you shouldn't worry about it having negative consequences.

Copyright (c) Marimer LLC