I need to access parent objects for validation. I've setup a parent property and a SetParent method on all of my classes of the approriate type. However, when I try to do something like:
public void SetParent(ref CSortProfileDB Parent){
m_Parent = Parent;
foreach (CChild m_Child in this.Items){
m_Child.SetParent(
ref this);}
}
It doesn't work. I get a compiler error:
Error 4 Cannot pass '<this>' as a ref or out argument because it is read-only C:\Development\Source\depot\EIIConsole\USPSSortProfileEditor\CSLA Classes\cSortCases.cs 32 42 USPSSortProfileEditor
I'm not using CSLAgen, the m_ is part of our coding standards. I noticed that CSLA already provides a Parent property in the business base class. Whenever I try to use it, it is set to null. Do I have to set the Parent object in the business base?
The parent property is only set on child objects contained within a BusinessListBase collection.
However, if your parent object directly contains a child, your parent object can call _child.SetParent(Me) to set the parent reference. To do this properly, you must make this call after any time you set the value of your _child field AND after deserialization (override the OnDeserialized() method).
Rocky,
That makes sense to me. For some reason my classes that derive from BusinessListBase or BusinessBase do not contain a SetParent method. Do I have to create it?
Note: I'm using the c# version of CSLA.net 2.1.4.
It is part of the IEditableBusinessObject interface – you just
need to cast your child object to that interface to call the method.
Rocky
From: JonM
[mailto:cslanet@lhotka.net]
Sent: Wednesday, April 11, 2007 10:17 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Trouble passing parent to child objects
Rocky,
That makes sense to me. For some reason my classes that derive from
BusinessListBase or BusinessBase do not contain a SetParent method. Do I
have to create it?
Note: I'm using the c# version of CSLA.net 2.1.4.
I'm not sure I follow, but I might.
So you have
Root
contains Col1
contains ChildA objects
contains Col2
contains ChildB objects
CSLA will automatically make sure all the ChildA and ChildB objects' parent values are set. You want to set a parent property on Col1 and Col2?
That isn't directly built into CSLA - you'll have to do that yourself by declaring your own private/protected field in your collection, and implementing your own SetParent() method.
The primary trick here is to make sure to mark the _parent field as NonSerializable and NotUndoable so it is safely ignored by both serialization and n-level undo.
Rocky,
First of all, thanks again for all of your help on this issue, I really appreciate your help and support for us amateurs. Next, you are basically correct, my model is as follows:
Editable Root
contains Editable Child Collection1
contains Editable Child Objects
contains Editable Child Collection2
contains Editable Child Objects
contains editable Child Collection 2a
contains editable child objects
The main reason I need the parent relationship is that one of the fields in the grandchild object in child collection 2a needs to validate against the entries in child collection1. My UI design keeps me from breaking the 2 main child collections into seperate root child collections. Therefore I need to walk up to the parent object and over to the editable child collection1 to validate entries in the grand child collection 2a. So far this is the only way I can think of to accomplish this. Is there a better way?
Thanks again,
Jon
I think you are on the right track by implementing a Parent
property on your collection. As I note in my previous post, that’s not
too hard as long as you remember to attribute the field properly, and remember
to reset the parent reference (from the editable root) after deserialization.
Rocky
From: JonM
[mailto:cslanet@lhotka.net]
Sent: Wednesday, April 11, 2007 10:17 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: Trouble passing parent to child objects
Rocky,
First of all, thanks again for all of your help on this issue, I really
appreciate your help and support for us amateurs. Next, you are basically
correct, my model is as follows:
Editable Root
contains
Editable Child Collection1
contains Editable Child Objects
contains
Editable Child Collection2
contains Editable Child Objects
&nb
sp;
contains editable Child Collection 2a
contains editable child objects
The main
reason I need the parent relationship is that one of the fields in the
grandchild object in child collection 2a needs to validate against the entries
in child collection1. My UI design keeps me from breaking the 2 main child
collections into seperate root child collections. Therefore I need to walk up
to the parent object and over to the editable child collection1 to val idate
entries in the grand child collection 2a. So far this is the only way I can
think of to accomplish this. Is there a better way?
Thanks
again,
Jon
Copyright (c) Marimer LLC