I'm trying to translate some code from delphi to vb .net.
Now I need to pass a class (classname or classtype call it as you
want) as a parameter.
My Delphi code was this:
function CreateAObject(vClass: TClass): TObject;
begin
result := vClass.Create;
end;
Vb will be something like this:
function CreateAObject(ByVal vClass as Class(???)) as Object
return new vClass
end function
I'm getting crazy to do this with vb .net. Some advice? But most
important, is it possibile?
If not, can I retrieve class from a instance and then inizialize
another object from the retrieved class?
Ty in advance
I had a little time on hand and decided to mash up some code
-------------
Imports System.Reflection
Imports System.Runtime.Remoting
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim obj As New YourClass()
'Get Type name
Dim typeName As String = obj.GetType.FullName()
'Including the one above, 4 instances of YourClass are created. Can
be checked from the public property .
For j As Integer = 1 To 3
CreateOtherObjects(typeName)
Next
Console.WriteLine(YourClass.ValueOfNum())
End Sub
Function CreateOtherObjects(ByVal className As String) As Object
Dim objHandle As System.Runtime.Remoting.ObjectHandle =
Activator.CreateInstance(Nothing, className)
Dim obj As Object = objHandle.Unwrap()
Return obj
End Function
' This is the Class we're playing with.
Private Class YourClass
Shared num As Integer = 0
Public Sub New()
num += 1
End Sub
Public Shared ReadOnly Property ValueOfNum() As Integer
Get
Return i
End Get
End Property
End Class
-------------
HTH,
C.
Hi All,
I'm trying to translate some code from delphi to vb .net.
Now I need to pass a class (classname or classtype call it as you
want) as a parameter.
My Delphi code was this:
function CreateAObject(vClass: TClass): TObject;
begin
result := vClass.Create;
end;
Vb will be something like this:
function CreateAObject(ByVal vClass as Class(???)) as Object
return new vClass
end function
I'm getting crazy to do this with vb .net. Some advice? But most
important, is it possibile?
If not, can I retrieve class from a instance and then inizialize
another object from the retrieved class?
Ty in advance
--
Charles A. Lopez
charle...@gmail.com
http://cs.nyu.edu/web/People/alumni.html
Thanks for the code. How does it address what the original poster's problem?
charle...@gmail.com
http://cs.nyu.edu/web/People/alumni.html
Me
.CreateType(GetType(Object))