Do I need Introspection?

Say I have a class ‘Pet’
It has properties Color, Breed, Gender

If I have an array or collection of these, now and then I need to duplicate the array

I know how to duplicate an object that I know the property list for

new = new Pet new.Color = old.color new.breed = old.breed new.sex = old.Gender

If I add a new property, owner, I have to find this code and add a new line.

My question is : is there a syntax where I can do this (pseudocode)

new = new Pet for each p in pet.properties new.property(p) = old.property(p) next

In such a way that the cloning code is self-repairing.

And it would also aid in serialisation.

Add a “copy constructor” to pet

Wont that need to do the “named property” copy that I describe above, though?
Or is a copy contractor some transparent way to do that?

[quote=121947:@Jeff Tullin]Wont that need to do the “named property” copy that I describe above, though?
Or is a copy contractor some transparent way to do that?[/quote]
Yes.

[quote=121947:@Jeff Tullin]Wont that need to do the “named property” copy that I describe above, though?
Or is a copy contractor some transparent way to do that?[/quote]
I don’t think you can do this without manually copy each property over. People have discussed this in the past and there is no transparent way to do this. Sorry.

A quick test shows that this should do what you are looking for:

Function Clone() As Class1
  dim result as new Class1
  dim myProperties() as Introspection.PropertyInfo = Introspection.GetType(self).GetProperties
  dim resultProperties() as Introspection.PropertyInfo = Introspection.GetType(result).GetProperties
  for i as Integer = 0 to myProperties.Ubound
    for j as Integer = 0 to resultProperties.Ubound
      if resultProperties(j).Name = myProperties(i).Name then
        resultProperties(j).Value(result) = myProperties(i).Value(self)
        exit 
      end if
    next j
  next i
  Return result
End Function

You should also make sure the properties of the original are readable and the properties of the clone are writeable, or just skip computed properties entirely.

One could argue that from an OOP perspective, that clone function is risky : an object should know how to clone itself ; this process should not happen from an outside agent.

What Norman was suggesting was something like this:

Class Foobar
  Constructor (other as Foobar) // this is the copy constructor
      me.x = other.x
      me.y = other.y
      me.somethingThatShouldNotBeCopied = nil // don't copy this thing!
   end sub

This gets used as follows:

  dim o1 as new Foobar // original object
  dim o2 as new Foobar(o1) // copy constructor: makes a clone of o1

Using these techniques, the clone (copy) constructor can do the “right” thing - this is especially important when the properties of the class object may themselves be other class objects, and it’s important to, in some cases, copy the pointer, but in other cases, clone the sub object.

Jason’s answer is what I was looking for.
But having played around with it, I realise that a class could contain things that shouldnt be copied or assigned with a simple =

so Norm and Michael get the prize, and I finally learn how to add a custom constructor, which I’d not done before.
(I was convinced that a constructor was a method with the same name as the class. nope)

Result.

The introspection approach can fail in interesting and spectacular ways. Read: can introduce hard to find bugs.

At one point they were.