Get 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
 
G

Guest

I'm not really sure if I understand what you want, but I think you have the
name of an enum value stored in a database and want to get back the enum
value from its name, correct?

The Enum class can do this. There's a static method "Parse" doing exactly
this:

Anchor = CType([Enum].Parse(GetType(System.Windows.Forms.AnchorStyles),
"Bottom"), System.Windows.Forms.AnchorStyles)

Regards,
Martin Müller
 

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