?
Solved

How to declare an object as class by class name

Posted on 2004-09-15
8
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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