References to objects

HeadNoodle

New member
Joined
Apr 10, 2006
Messages
3
Programming Experience
10+
Hi Guys, just looking for peoples opinion on something that has been bugging me.

I have a class, lets call it AClass with three functions:

addobject(oObject as object)
RemoveObject(iObjectId as integer)
getobjects() as hashtable

Now say that I add two objects then call getobjects and put the results in a hashtable.

Now the application give the user the ability to change the properties, etc of the two objects that have been added to AClass.

So my question is.......

As getobjects will return a hash table with references to the objects originally added, is it acceptable (good practice) to modify the objects from their reference even though they are encapsulated in AClass or should I create a new object with the modification, remove the original object with RemoveObject and add a new object with Addobject.

The only reason I ask this is as I dont really want the code that is accessing AClass to accidently override a businessrule by directly modifying objectdata without using the Add,remove functions. I know I can create a deep copy of the object when returning the hash table via getobjects but that can be tedious if I have a complex object and also cause other unforseen problems.

So, I really just want to know how you guys handle this.

Kindest

HeadNoodle
 
I would typically not pass them the hashtable

I would have a getobject(index or key) that returned the object that they would want from the hash table and I would return a copy

I would then have a updateobject that took in the object after they modified it and updated the "master" in the hash table.

That way they cant modify things by mistake

If they had to enumerate through all the objects (for each) i would add the iEnumeraable interface
 
Back
Top