Howto create new object, of type of other object?

Q

Qwert

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.
 
C

Cor Ligthert

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
 
Q

Qwert

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.
 
H

Herfried K. Wagner [MVP]

Qwert said:
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'.
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dlls in VB.NET (VS 2008). 1
Design Problem: Abstract Class inheriting from a List(Of AnotherAbstactClass) 4
Inheritance 3
type mismatch 3
Event with response? 3
DLLImports 2
My Class 16
currency manager 5

Top