By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Howto create new object, of type of other object?

P: n/a
Hello,

question regarding the following code:

Public Class MyCol
Inherits System.Collections.SortedList
....
Public Sub MyFunction( objMyClass As MyClass )

End Sub
....
End Class

How is it possible to create a NEW instance of the object MyClass inside
MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )

Thanks.
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Qwert,
How is it possible to create a NEW instance of the object MyClass inside
MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )

You mean
Dim objNew as New TheMyClass

You cannot instance an object, that goes only for classes.
(Not to mixup that object is a class, however an object is an instanced
class)

I just am guessing that this was the question.

Cor
Nov 21 '05 #2

P: n/a
Yeah, no, what I mean is this:

MyClass
Public Function GetNew() As MyClass
Return New MyClass()
End Function
End MyClass

now, somewhere else I can do:

Public Sub MyFunction( objObject As Object)
Dim objObject1 As Object = objObject.GetNew()
Dim objObject2 As Object = objObject.GetNew()
End Sub

I want to do this, but without writing the GetNew() function for MyClass.
And MyFunction() does not know what type objObject will be.
"Cor Ligthert" <no************@planet.nl> schreef in bericht
news:uA*************@TK2MSFTNGP12.phx.gbl...
Qwert,
How is it possible to create a NEW instance of the object MyClass inside
MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )

You mean
Dim objNew as New TheMyClass

You cannot instance an object, that goes only for classes.
(Not to mixup that object is a class, however an object is an instanced
class)

I just am guessing that this was the question.

Cor

Nov 21 '05 #3

P: n/a
"Qwert" <no**@nosp.com> schrieb:
How is it possible to create a NEW instance of the object MyClass inside
MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )


Take a look at 'Activator.CreateInstance'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4

P: n/a


Dim z As Object = Activator.CreateInstance(y.GetType())

"Qwert" <no**@nosp.com> wrote in message
news:mq********************@casema.nl...
Yeah, no, what I mean is this:

MyClass
Public Function GetNew() As MyClass
Return New MyClass()
End Function
End MyClass

now, somewhere else I can do:

Public Sub MyFunction( objObject As Object)
Dim objObject1 As Object = objObject.GetNew()
Dim objObject2 As Object = objObject.GetNew()
End Sub

I want to do this, but without writing the GetNew() function for MyClass.
And MyFunction() does not know what type objObject will be.
"Cor Ligthert" <no************@planet.nl> schreef in bericht
news:uA*************@TK2MSFTNGP12.phx.gbl...
Qwert,
How is it possible to create a NEW instance of the object MyClass inside
MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )

You mean
Dim objNew as New TheMyClass

You cannot instance an object, that goes only for classes.
(Not to mixup that object is a class, however an object is an instanced
class)

I just am guessing that this was the question.

Cor


Nov 21 '05 #5

P: n/a
Quert,

You mean simple this?

Dim objObject1 As Object = New Mycl
Dim objObject2 As Object = New Mycl

(I would not use myclass in samples that is a reserved word)

I hope this helps,

Cor
Nov 21 '05 #6

P: n/a
Yes, great, that's it. Thanks all.

"Robin Tucker" <id*************************@reallyidont.com> schreef in
bericht news:d7*******************@news.demon.co.uk...


Dim z As Object = Activator.CreateInstance(y.GetType())

"Qwert" <no**@nosp.com> wrote in message
news:mq********************@casema.nl...
Yeah, no, what I mean is this:

MyClass
Public Function GetNew() As MyClass
Return New MyClass()
End Function
End MyClass

now, somewhere else I can do:

Public Sub MyFunction( objObject As Object)
Dim objObject1 As Object = objObject.GetNew()
Dim objObject2 As Object = objObject.GetNew()
End Sub

I want to do this, but without writing the GetNew() function for MyClass.
And MyFunction() does not know what type objObject will be.
"Cor Ligthert" <no************@planet.nl> schreef in bericht
news:uA*************@TK2MSFTNGP12.phx.gbl...
Qwert,

How is it possible to create a NEW instance of the object MyClass
inside MyFunction (without adding functionality to MyClass)?
Something like:

Dim objNew As Object = CreateNewInstance( objMyClass )

You mean
Dim objNew as New TheMyClass

You cannot instance an object, that goes only for classes.
(Not to mixup that object is a class, however an object is an instanced
class)

I just am guessing that this was the question.

Cor



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.