Solved

How to declare an object as class by class name

Posted on 2004-09-15
8
178 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:harmony1
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:KGreg
Comment Utility
isn't reflection a wonderful thing?

KGreg
0
 
LVL 6

Expert Comment

by:KGreg
Comment Utility
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
Comment Utility
Well, I get excited by 'small' things, like reflection, than the great wall of china...
0
 
LVL 19

Expert Comment

by:arif_eqbal
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now