Sub(Thing as Object) {thing = new Thing.GetType} ?

  • Thread starter Thread starter Kristian Frost
  • Start date Start date
K

Kristian Frost

Hello,
Sorry for polluting the VB newsgroup with curly brackets, but I couldn't
think how to get that question onto one line otherwise.
Anyway, I'm trying to write a Sub, or Function which takes an object as an
input and then creates a new object of the same type using that class'
constructor.
VB isn't currently liking any of my attempts, but the general idea is to
have:

Private Function NewInterfaceObjectArray(ByVal InterfaceObject As
Object(), ByVal defaultValue As Integer) As InterfaceObjectClass()
InterfaceObject = New InterfaceObject.GetType
Return InterfaceObject
End Function


That's a bit messy, even for pseudocode, but I think you'll get the idea..

I guess this is a bit of a weird question, but it's mostly because the
idea intrigued me while I was writing a long constructor for an object
made of object arrays, which were all interfaces onto a single object
type. I'm *sure* there was a good reason for coding that at the time...

Anyway, I can get round the problem, but I'm interested in whether the
language supports the idea of the function I'm asking about, and the
question seems to be too obscure for google (or my use of google) to cope
with.

KF
 
Kristian Frost said:
Sorry for polluting the VB newsgroup with curly brackets, but I couldn't
think how to get that question onto one line otherwise.
Anyway, I'm trying to write a Sub, or Function which takes an object as an
input and then creates a new object of the same type using that class'
constructor.

Obtain the type of the object passed to the method by calling its 'GetType'
method. Then pass the type to 'Activator.CreateInstance' to create an
instance of the type.
 
Back
Top