Home All Groups Group Topic Archive Search About

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

Author
22 Sep 2006 9:43 AM
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 =3D 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 cop=
e  =

with.

KF
    =


-- =

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Author
22 Sep 2006 1:34 PM
Herfried K. Wagner [MVP]
"Kristian Frost" <kfr***@gmail.com> schrieb:
>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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>