[VB . net] passing a class as argument

2,096 views
Skip to first unread message

BB

unread,
Nov 6, 2007, 6:17:06 AM11/6/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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

Andrew Badera

unread,
Nov 6, 2007, 11:43:43 AM11/6/07
to DotNetDe...@googlegroups.com
You're trying to pass the Type of the class? Is that what you're telling us?

If that's the case you want typeof(classname) or objectofclass.GetType()

(In C# at least. Syntax/wording may vary slightly in VB.)

--Andrew Badera

Cerebrus

unread,
Nov 6, 2007, 12:04:15 PM11/6/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Yes, you can retrieve the Class type from an instance. Look up
Reflection.

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.

Charles A. Lopez

unread,
Nov 6, 2007, 1:59:02 PM11/6/07
to DotNetDe...@googlegroups.com
It's been a while since I've seen Delphi (Pascal) code. Please see comments below..

On 11/6/07, BB <eva.m...@gmail.com> wrote:

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;
 
 
The above code doesn't even work.
 
result := vClass.Create ???
 
Couple of things are incorrect. You don't allocate the variable "result".
 
 
CreateAObject := vClass.Create;  // this seems like a more suitable answer.
 
So if the code you are trying to modify is incorrect and you're trying to "fix" it then all I can assume is your organization has employed poor programming practices.
 
I'll have to review Cerebrus code and comment on it.

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

unread,
Nov 6, 2007, 2:02:52 PM11/6/07
to DotNetDe...@googlegroups.com
Reflection is derived from the concept of classes. It must be. Without classes there is no concept of Reflection.


 
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

Peter Groenewegen

unread,
Nov 6, 2007, 11:50:39 AM11/6/07
to DotNetDe...@googlegroups.com
Something like
 
public function CreateType(ByVal t as type) As object
 
Maybe you can do something nice with generics her.
 
Code for calling:

Me

.CreateType(GetType(Object))
Have fun programming :)

 
On 11/6/07, BB <eva.m...@gmail.com> wrote:

Cerebrus

unread,
Nov 7, 2007, 9:16:57 AM11/7/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sure you can... but I wanted to keep out of Generics to keep the code
simple.

BB

unread,
Nov 19, 2007, 5:56:47 AM11/19/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sorry for my late...

Public Function IstanziaNuovoOggetto(ByVal vClass As Type) As Object
Dim obj As Object = Activator.CreateInstance(vClass)
Return obj
End Function

It runs ;-)

Ty guys

Eva
Reply all
Reply to author
Forward
0 new messages