Solved

How to declare an object as class by class name

Posted on 2004-09-15
8
179 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

825 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