Return types

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
When overriding the clone method of an arraylist should i let it Return Object as default or change this to MyClassName?
i have a feeling i'm supposed to leave it as object but i don't understand why...

any thoughts, ideas and crude remarks appreciated
 
Either will probably work becuase object can be turned into MyClassName and MyClassName can be turned into object
 
yes, but i feel there is some generally accepted principle i am missing in this instance
ie. sender is always object in an event, this is ovious but why not make it the same type as the calling object? (in this case could delegates be involved... i'll save those questions for another day)
 
Becuase events can be triggered by different classes with no real connection to each other(ie. MyEvent(Sender as Object,e as ...), the sender could be a windows control or a class which are not neccessarily related(derived from the same base)). You'll probably want to return it as an Object becuase if its a clone, you'll want it programmer to be able to attach the clone as a derived object
 
Why would you inherit from ArrayList? If you need to make a collection of your own type, you should inherit from CollectionBase and provide your own accessor and add, remove functions etc.
 
Aha, once again i've made a foolish mistake...
This brings me to another question.. suprise suprise

Public Overrides Function Clone() as Object

Return Me

End Function

Would this return a deep copy? How do i return a shallow copy?
 
You need to create a new instance of your class type (whatever it is), and set all properties of the new class to the current instance (Me). Return the new instance in the Clone function.

If all of your private fields are value types, you can use Me.Clone() instead. If you have ANY fields that are objects, you'll have to do it manually.

-Nerseus
 
isee what i need to do now but does that mean i define what a shallow copy is in this method myself?

thanks
 
Could it be that a value type would be more suited for your needs, in this instance? That way when you pass them only a copy is passed.
 
To be clearer (in case you didn't know what divil meant), could you make your class a struct instead? Then using asignment would make an actual copy instead of just assigning the pointer to the same instance. Also, passing a struct ByRef would pass a copy of the object rather than just a reference.

-Nerseus
 
I see what you mean... I think it could be possible
So if i passed a structure instead of my class itself the properties would not be altered?
I need to keep it as an object because i need to inherit from it for the UI's class
i also have trouble understanding the difference in types and objects. i've read a little on it but i still feel kinda confused about it.

you guys are great thanks
 
Back
Top