Getting a type from a string

G

Guest

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.AnchorStyles.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(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")

'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)


End Sub


'**********************************************
'Entire VB.net example

Option Strict On
Option Explicit On

Imports System
Imports System.Reflection

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.
InitializeComponent()

'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(26, 44)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 50)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = 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(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")

'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)


End Sub


End Class
 
C

Cor Ligthert

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.Bottom
Dim myAnchor As Integer = CInt(Button1.Anchor)
Button1.Anchor = CType(myAnchor, AnchorStyles)
///
Why not give it a try?

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Erol said:
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.AnchorStyles.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 _
)
///
 
G

Guest

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.reflection. The parameters which are passed into my sub routine are
namely the
SetValue(ObjectToSet, 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.AnchorStyles.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 said:
Erol said:
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.AnchorStyles.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 _
)
///
 
C

Cor Ligthert

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 said:
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.reflection. The parameters which are passed into my sub routine are
namely the
SetValue(ObjectToSet, 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.AnchorStyles.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 said:
Erol said:
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.AnchorStyles.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 _
)
///
 
H

Herfried K. Wagner [MVP]

Erol said:
The ObjectToSet in this case would be the button1 object and the value is
a
string value from the database "System.Windows.Forms.AnchorStyles.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.CreateInstance' and then pass the value to 'SetValue'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top