Solved

How to declare an object as class by class name

Posted on 2004-09-15
8
180 Views
Last Modified: 2010-04-23
I have some form classes (frm_Customers, frm_Products, etc.).  I have a general Sub in a separate module which gets the form name as input. In this Sub I want to assign a new variable as the form class according to the form name. For example:

Public Sub ChangeFormText(frm_name as string)
   dim objForm as ?????
      objForm.Text="New Name"
End Sub


What should replace the ?????

Thanks,
Sima
0
Comment
Question by:harmony1
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12063463
I don't think you can do it that way.
Instead of passing the form name, pass the form instead

Public Sub ChangeFormText(frm as form)
      Frm.Text="New Name"
End Sub
0
 
LVL 6

Accepted Solution

by:
KGreg earned 125 total points
ID: 12065313
You can do this by using reflection:

Public Sub ChangeFormText(frm_name as string)

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

        Dim objForm As Object = asm.CreateInstance(asm.GetName.Name + "." + frm_name)

        Ctype(objForm, Form).Text="New Name"
End Sub

KGreg
0
 

Author Comment

by:harmony1
ID: 12070696
Thanks KGreg for an excellent solution!
I have another question regarding this matter: I need to pass a parameter to the constructor of the form instance. This parameter is the calling form object. Normally I would do the following:

Dim objForm as new MyAssembly.MyForm(Me)
objForm.ShowDialog()

Me - the form from which I open MyForm.
Where do I put the Me in your solution?

Sima
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:harmony1
ID: 12073078
Never mind, I figured that out with additional parameters to the CreateInstance method:

Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim aryObj() As Object = {Me}
Dim obj_frmtplFilters_AdvConditions As Object = asm.CreateInstance(asm.GetName.Name + "."      + "frmtplFilters_AdvConditions", True, Reflection.BindingFlags.Default, Nothing, aryObj, Nothing, Nothing)
CType(obj_frmtplFilters_AdvConditions, Form).ShowDialog()

Sima
0
 
LVL 6

Expert Comment

by:KGreg
ID: 12074225
isn't reflection a wonderful thing?

KGreg
0
 
LVL 6

Expert Comment

by:KGreg
ID: 12074232
let me rephrase.. it's not the artificial heart or the great wall of china.. but it is very cool.

KGreg
0
 

Author Comment

by:harmony1
ID: 12079070
Well, I get excited by 'small' things, like reflection, than the great wall of china...
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13261950
Hi, Greg
I'd agree with you, its Wonderful thing...
It has helped me a lot recently in achieving something which practically impossible without it...


0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question