473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a type from a string

How do I get a type from a string? I'm retrieving a string value from my
database so that I can set my property values dynamically. In the event
"Form1_Load ", you will see that I'm trying to set the button's anchor
property to "System.Windows .Forms.AnchorSt yles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good use.
Please help!

'************** *************** *************** *******
Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System. Windows.Forms.B utton).GetPrope rty("Anchor")
Dim fi As FieldInfo =
GetType(System. Windows.Forms.A nchorStyles).Ge tField("Bottom" )

'This line works, but I need the code to get
the type from the string
Anchor.SetValue (Button1, System.Windows. Forms.AnchorSty les.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValu e(Button1,
type.gettype("S ystem.Windows.F orms.AnchorStyl es.Bottom"), Nothing)
End Sub
'************** *************** *************** **
'Entire VB.net example

Option Strict On
Option Explicit On

Imports System
Imports System.Reflecti on

Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(26, 44)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(120, 50)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(292, 262)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System. Windows.Forms.B utton).GetPrope rty("Anchor")
Dim fi As FieldInfo =
GetType(System. Windows.Forms.A nchorStyles).Ge tField("Bottom" )

'This line works, but I need the code to get
the type from the string
Anchor.SetValue (Button1, System.Windows. Forms.AnchorSty les.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValu e(Button1,
type.gettype("S ystem.Windows.F orms.AnchorStyl es.Bottom"), Nothing)
End Sub
End Class

--
Erol
Nov 21 '05 #1
5 1455
Erol,

Are you not trying to do a simple thing difficult. The anchorstyles is an
enum, representing a value.

Therefore this can in my opinion do the job when you save the integer in
your database.

\\\
Button1.Anchor = AnchorStyles.Bo ttom
Dim myAnchor As Integer = CInt(Button1.An chor)
Button1.Anchor = CType(myAnchor, AnchorStyles)
///
Why not give it a try?

I hope this helps?

Cor
"Erol" <Er**@discussio ns.microsoft.co m>
How do I get a type from a string? I'm retrieving a string value from my
database so that I can set my property values dynamically. In the event
"Form1_Load ", you will see that I'm trying to set the button's anchor
property to "System.Windows .Forms.AnchorSt yles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good
use.
Please help!

'************** *************** *************** *******
Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System. Windows.Forms.B utton).GetPrope rty("Anchor")
Dim fi As FieldInfo =
GetType(System. Windows.Forms.A nchorStyles).Ge tField("Bottom" )

'This line works, but I need the code to get
the type from the string
Anchor.SetValue (Button1, System.Windows. Forms.AnchorSty les.Bottom,
Nothing)
'Uncomment to see error
'Anchor.SetValu e(Button1,
type.gettype("S ystem.Windows.F orms.AnchorStyl es.Bottom"), Nothing)
End Sub
'************** *************** *************** **
'Entire VB.net example

Option Strict On
Option Explicit On

Imports System
Imports System.Reflecti on

Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(26, 44)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(120, 50)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(292, 262)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System. Windows.Forms.B utton).GetPrope rty("Anchor")
Dim fi As FieldInfo =
GetType(System. Windows.Forms.A nchorStyles).Ge tField("Bottom" )

'This line works, but I need the code to
get
the type from the string
Anchor.SetValue (Button1, System.Windows. Forms.AnchorSty les.Bottom,
Nothing)
'Uncomment to see error
'Anchor.SetValu e(Button1,
type.gettype("S ystem.Windows.F orms.AnchorStyl es.Bottom"), Nothing)
End Sub
End Class

--
Erol

Nov 21 '05 #2
"Erol" <Er**@discussio ns.microsoft.co m> schrieb:
How do I get a type from a string? I'm retrieving a string value from my
database so that I can set my property values dynamically. In the event
"Form1_Load ", you will see that I'm trying to set the button's anchor
property to "System.Windows .Forms.AnchorSt yles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good
use.
Please help!


Take a look at '[Enum].Parse':

\\\
Me.Anchor = _
DirectCast( _
[Enum].Parse(GetType( AnchorStyles), "Bottom"), _
AnchorStyles _
)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Thanks guys, but my entire projects is dynamic in which all my front-end
forms are designed from scratch using the values entered in my database and
System.reflecti on. The parameters which are passed into my sub routine are
namely the
SetValue(Object ToSet, Value, Index)

The ObjectToSet in this case would be the button1 object and the value is a
string value from the database "System.Windows .Forms.AnchorSt yles.Bottom". If
I use the enum value, 2, which represents the "Bottom" value, it generates an
error. Therefore, without me knowing how to explain this problem in good
programming language, how do convert this string value from my database to
the "Value" parameter in "Setvalue" above?
--
Erol

"Herfried K. Wagner [MVP]" wrote:
"Erol" <Er**@discussio ns.microsoft.co m> schrieb:
How do I get a type from a string? I'm retrieving a string value from my
database so that I can set my property values dynamically. In the event
"Form1_Load ", you will see that I'm trying to set the button's anchor
property to "System.Windows .Forms.AnchorSt yles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good
use.
Please help!


Take a look at '[Enum].Parse':

\\\
Me.Anchor = _
DirectCast( _
[Enum].Parse(GetType( AnchorStyles), "Bottom"), _
AnchorStyles _
)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Erol,

Using in this way system reflection means in my opinion that you are trying
to use VBNet as a kind of scripting language. Or better making your own JIT
pre JIT compiler.

I hope you don't mind that I am not interesting in that and therefore put no
time in that.
Maybe somebody else will do that.

Sorry

Cor

"Erol" <Er**@discussio ns.microsoft.co m>
Thanks guys, but my entire projects is dynamic in which all my front-end
forms are designed from scratch using the values entered in my database
and
System.reflecti on. The parameters which are passed into my sub routine are
namely the
SetValue(Object ToSet, Value, Index)

The ObjectToSet in this case would be the button1 object and the value is
a
string value from the database "System.Windows .Forms.AnchorSt yles.Bottom".
If
I use the enum value, 2, which represents the "Bottom" value, it generates
an
error. Therefore, without me knowing how to explain this problem in good
programming language, how do convert this string value from my database to
the "Value" parameter in "Setvalue" above?
--
Erol

"Herfried K. Wagner [MVP]" wrote:
"Erol" <Er**@discussio ns.microsoft.co m> schrieb:
> How do I get a type from a string? I'm retrieving a string value from
> my
> database so that I can set my property values dynamically. In the event
> "Form1_Load ", you will see that I'm trying to set the button's anchor
> property to "System.Windows .Forms.AnchorSt yles.Bottom" dynamically from
> a
> string (which I commented out). How can I get this code to work. I also
> started to use the variable "fi", but I don't know how to put it to
> good
> use.
> Please help!


Take a look at '[Enum].Parse':

\\\
Me.Anchor = _
DirectCast( _
[Enum].Parse(GetType( AnchorStyles), "Bottom"), _
AnchorStyles _
)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
"Erol" <Er**@discussio ns.microsoft.co m> schrieb:
The ObjectToSet in this case would be the button1 object and the value is
a
string value from the database "System.Windows .Forms.AnchorSt yles.Bottom".
If
I use the enum value, 2, which represents the "Bottom" value, it generates
an
error. Therefore, without me knowing how to explain this problem in good
programming language, how do convert this string value from my database to
the "Value" parameter in "Setvalue" above?


You can try to dynamically instantiate the property's type using
'Activator.Crea teInstance' and then pass the value to 'SetValue'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

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

Similar topics

2
6899
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This...
21
1732
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is...
4
1945
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal iMsg As Integer, ByVal wParam As Integer, ByVal LParam As Integer) As Integer 'Code here
13
10109
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the parent table. ----------------------------
1
2605
by: Chris White | last post by:
Here's my XML <?xml version="1.0" encoding="UTF-8"?> <results> <status code="ok"/> <report-bulk-users> <row principal-id="23859115" type="user"> <login>muser@domain.com</login> <name>My User</name> <email>myuser@domain.com</email> <manager>manager@domain.com</manager>
0
2900
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form action="/login"> <html:text property="userName" value="Bunty"/> <html:submit/>
4
1661
by: jehugaleahsa | last post by:
Hello: Say I were to query the schema tables from a database management system. Say I got a DataType field that held the string 'VARCHAR2'. Now, I know that VARCHAR2 associates to System.String. However, different DBMSs have different types and worse, even some with the same type turn out to be different .NET types. I would like...
3
1394
by: Nathan Sokalski | last post by:
I have the following code: Dim values As New ArrayList() values.Add("Yes") values.Add("No") values.Add("Maybe") values.Add("Whatever") dim x as String()=values.ToArray() However, I recieve an error saying an Object array cannot be implicitly
1
4909
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In...
5
1623
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace MyFunctions Public Class BitHandling
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.