Solved

How to declare an object as class by class name

Posted on 2004-09-15
8
182 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 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
Industry Leaders: 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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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