473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

converting string to object

Hi,

I want to have a sub that takes string as argument and then opens a form
that has that string as a class name.

Say:

Class users
Inherits System.Windows.Forms.Form
'display form content
End Class

Sub functions(str As String)
Dim frm As New str 'this is want I'm trying to accomplish - obviously
doesn't work this way
frm.ShowDialog()
End Sub

And then call it as Sub("users") If I want to accomplish next:
Dim frm As New users
frm.ShowDialog()
Obviously, what I'm trying to do is much more complicated but if I would
know how to accomplish example above I'd be able to do what I need.
Thanks,

Vlado
http://www.excelleinc.com
Nov 21 '05 #1
3 1492
On Tue, 15 Mar 2005 13:11:34 -0600, "excelleinc.com"
<vj******@excelleinc.com> wrote:
Obviously, what I'm trying to do is much more complicated but if I would
know how to accomplish example above I'd be able to do what I need.


Look up Activator.CreateInstance() method in the MSDN.

Daniel Klein

Nov 21 '05 #2
You'll need to use reflection:

Imports System.Reflection

Sub ShowForm(str As String)
Dim ty As Type = Type.GetType(str)
Dim frm As Object = Activator.CreateInstance(ty)
DirectCast(frm, Form).ShowDialog()
End Sub
Then call the above method as:
ShowForm("MyApplication.users")

Note that the method Type.GetType requires a fully qualified name including
namespace. Since by default Vb.NET adds the project as the default namespace,
you would need to append it to the form name before passing it as a parameter
to Type.GetType (assuming you are using the default settings only). I would
suggest reading up on the documentation of Type.GetType to make sure you
don't end up with null references.
hope that helps..
Imran.

Hi,

I want to have a sub that takes string as argument and then opens a
form that has that string as a class name.

Say:

Class users
Inherits System.Windows.Forms.Form
'display form content
End Class
Sub functions(str As String)
Dim frm As New str 'this is want I'm trying to accomplish -
obviously
doesn't work this way
frm.ShowDialog()
End Sub
And then call it as Sub("users") If I want to accomplish next:
Dim frm As New users
frm.ShowDialog()
Obviously, what I'm trying to do is much more complicated but if I
would know how to accomplish example above I'd be able to do what I
need.

Thanks,

Vlado

http://www.excelleinc.com


Nov 21 '05 #3
Worked great.

I was getting null references but when I turned IgnoreCase to True
Dim ty As Type = Type.GetType(Str, False, True)
everything worked just great.

Thank you both very much.
"Imran Koradia" <no****@microsoft.com> wrote in message
news:19**********************@news.microsoft.com.. .
You'll need to use reflection:

Imports System.Reflection

Sub ShowForm(str As String)
Dim ty As Type = Type.GetType(str)
Dim frm As Object = Activator.CreateInstance(ty)
DirectCast(frm, Form).ShowDialog()
End Sub
Then call the above method as:
ShowForm("MyApplication.users")

Note that the method Type.GetType requires a fully qualified name
including namespace. Since by default Vb.NET adds the project as the
default namespace, you would need to append it to the form name before
passing it as a parameter to Type.GetType (assuming you are using the
default settings only). I would suggest reading up on the documentation of
Type.GetType to make sure you don't end up with null references.
hope that helps..
Imran.

Hi,

I want to have a sub that takes string as argument and then opens a
form that has that string as a class name.

Say:

Class users
Inherits System.Windows.Forms.Form
'display form content
End Class
Sub functions(str As String)
Dim frm As New str 'this is want I'm trying to accomplish -
obviously
doesn't work this way
frm.ShowDialog()
End Sub
And then call it as Sub("users") If I want to accomplish next:
Dim frm As New users
frm.ShowDialog()
Obviously, what I'm trying to do is much more complicated but if I
would know how to accomplish example above I'd be able to do what I
need.

Thanks,

Vlado

http://www.excelleinc.com


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Francesco | last post by:
Ciao, I'm new in c++ programming and I've experimented some method to convert real number into c++ string object, at present I've found as best solution use the C command "sprintf":...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
6
by: Christopher Benson-Manica | last post by:
<html> <head> <script> var s=String( 'foo' ); alert( s ); s.bar='bar'; alert( s.bar ); </script></head></html> Why does the second alert produce 'undefined'? Are string objects
2
by: cnickl | last post by:
I’m not sure where to post this, so I post it here. It’s more like a VC++.NET / Managed Code problem. Probably easy to fix for you guys. I want to use a String object as a parameter for a...
2
by: Mike Moore | last post by:
does anyone have an example of how to get the connection string object converted to a string variable type in order for me to call a function?
5
by: rengeek33 | last post by:
I am building a SQL statement for Oracle and need one part of it to read: , myvar = null, myvar2 = "1", myvar3 = "0", myvar4 = null, etc. I am constructing this string using the String...
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
3
by: silverburgh.meryl | last post by:
Hi, If I have a std string object , how to convert a STD string object to an integer? Thank you.
1
by: OccasionalFlyer | last post by:
I'm trying to overcome limitations in a function by passing a string object rather than a string primitive. The code to set the string object's attributes, however, seems to have no effect: var...
8
by: David Lazos | last post by:
Hi All, I use Contains method of String object to determine if a string variable has another string, like that. *************************** ipAddress.Contains("127.0.0")...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.