[Newbie] Method Calls and Dynamic Casting

  • Thread starter Thread starter Melonie Brown
  • Start date Start date
M

Melonie Brown

How do you call a method on an object that you know the type of as a string?

I can say

Dim x as New MyClass
x.doSomething()

but I need
Dim x as New SomeMagicFunction("MyClass")
x.doSomething()

Is this even possible? I've searched through the newsgroup and did a
goodle search, but I may not be searching on the right keywords.
 
Dim x as New SomeMagicFunction("MyClass")
x.doSomething()

Is this even possible? I've searched through the newsgroup and did a
goodle search, but I may not be searching on the right keywords.

Yes it's possible. Look up the Activator.CreateInstance method in the
docs. That should get you going. I don't the exact syntax, but that issue
has been discussed here in the groups recently.

Chris
 
Melonie Brown said:
How do you call a method on an object that you know the type of as a
string?

I can say

Dim x as New MyClass
x.doSomething()

but I need
Dim x as New SomeMagicFunction("MyClass")
x.doSomething()

Is this even possible? I've searched through the newsgroup and did a
goodle search, but I may not be searching on the right keywords.

Have a look at the method System.Activator.CreateInstance to create an
instance by name. See also the System.Type.InvokeMember instance member. You
get the System.Type object by calling the GetType method of the object
created by System.Activator.CreateInstance.
 
Melonie,
You need to use the System.Activator.CreateInstance method.

Something like:
Dim t As Type
t = Type.GetType("MyClass")

Dim x As MyClass
x = Activator.CreateInstance(t)

x.DoSomething()

Hope this helps
Jay
 
Hello,

Jay B. Harlow said:
You need to use the System.Activator.CreateInstance method.

Something like:
Dim t As Type
t = Type.GetType("MyClass")

Dim x As MyClass
x = Activator.CreateInstance(t)

x.DoSomething()

This will only work if you know the type when writing code.
 
Hello,

Melonie Brown said:
How do you call a method on an object that you know the type
of as a string?

I can say

Dim x as New MyClass
x.doSomething()

but I need
Dim x as New SomeMagicFunction("MyClass")
x.doSomething()

\\\
Imports System.Reflection

Public Class Main
Public Shared Sub Main()
Dim sc As New SampleClass()
Dim obj As Object = sc ' ;-)))
Dim mi As MethodInfo = obj.GetType().GetMethod("SampleMethod")
mi.Invoke(obj, Nothing)
End Sub

Public Class SampleClass
Public Sub SampleMethod()
MsgBox("Hello World!")
End Sub
End Class
End Class
///

Have a look at the 'CallByName' method too.
 
Herfried,
This will only work if you know the type when writing code.
Of course.

Normally when I am doing this sort of thing the Type that I am using
Activator.CreateInstance on is derived from a base class or implements a
specific interface. I then use this base class or interface for the
variable. Allowing me to work with the dynamically created object in an
early bound fashion, avoiding any late binding.

Something like (where MyDerivedClass inherits from MyBaseClass):
Dim t As Type
t = Type.GetType("MyDerivedClass")

Dim x As MyBaseClass
x = Activator.CreateInstance(t)

x.DoSomething()

Of course DoSomething normally is Overridable or MustOverride method
(property, sub, function) of MyBaseClass, with MyDerivedClass overriding it.

I should have pointed that out for Melonie.

Thanks for the catch.

Jay
 
Doh!

And of course I have a DirectCast on the call to CreateInstance!
Dim x As MyBaseClass
x = DirectCast(Activator.CreateInstance(t), MyBaseClass)

As I run with Option Strict On!

Jay
 
Hi,
You can also use system. reflection to acheive this.
Here's the code
objClass =
System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("Myclass")
objClass.dosomething()

Thanks,
Sarika
 
Back
Top