HighTechTalks DotNet Forums  

Get a type from a string

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Get a type from a string in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Erol
 
Posts: n/a

Default Get a type from a string - 12-15-2004 , 04:29 AM






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).GetFiel d("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.Bo ttom"), 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).GetFiel d("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.Bo ttom"), Nothing)


End Sub


End Class

--
Erol

Reply With Quote
  #2  
Old   
Alan Pretre
 
Posts: n/a

Default Re: Get a type from a string - 12-16-2004 , 11:13 AM






"Erol" <Erol (AT) discussions (DOT) microsoft.com> wrote

Quote:
How do I get a type from a string?
See the System.Type.GetType() function.

http://msdn.microsoft.com/library/de...ttypetopic.asp

-- Alan




Reply With Quote
  #3  
Old   
Erol
 
Posts: n/a

Default Re: Get a type from a string - 12-17-2004 , 01:33 AM



Hi Alan

In my example you'll see that I'm using the System.Type.GetType function,
but it's giving me an error. Could you please shed some light onto my
problem. Just uncomment the line in "form load" that says "Uncomment to see
error"


"Alan Pretre" wrote:

Quote:
"Erol" <Erol (AT) discussions (DOT) microsoft.com> wrote in message
news:B802B0D6-DA39-42D4-A45B-8398C639DFCD (AT) microsoft (DOT) com...
How do I get a type from a string?

See the System.Type.GetType() function.

http://msdn.microsoft.com/library/de...ttypetopic.asp

-- Alan




Reply With Quote
  #4  
Old   
Alan Pretre
 
Posts: n/a

Default Re: Get a type from a string - 12-17-2004 , 09:30 AM



"Erol" <Erol (AT) discussions (DOT) microsoft.com> wrote

Quote:
In my example you'll see that I'm using the System.Type.GetType function,
but it's giving me an error. Could you please shed some light onto my
problem. Just uncomment the line in "form load" that says "Uncomment to
see
error"
Sorry I did not read your OP thoroughly enough...

I am not a VB guy, but it does not seem to me that you need to go about it
the way you are.

You should be able to just get and set the properties directly. For
example,

'Fetching
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = Button1.Anchor 'Store value in DB

'Setting
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = System.Windows.Forms.AnchorStyles.Bottom 'Value from DB
Button1.Anchor = Anchor


-- Alan




Reply With Quote
  #5  
Old   
Erol
 
Posts: n/a

Default Re: Get a type from a string - 12-20-2004 , 03:39 AM



Hi Alan

In your example;

'Fetching
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = Button1.Anchor 'Store value in DB

'Setting
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = System.Windows.Forms.AnchorStyles.Bottom 'Value from DB
Button1.Anchor = Anchor

particularly your line of code that says the following: Anchor =
System.Windows.Forms.AnchorStyles.Bottom 'Value from DB

This value is in string format: "System.Windows.Forms.AnchorStyles.Bottom"
'Value from DB

How would I get the type from this string value, because Anchor =
System.Windows.Forms.AnchorStyles.Bottom 'Value from DB does not work,
unless I'm misinterpreting what you are trying to tell me.

I can store the enum value in the database which is "2" for "Bottom", but
when assigning the value "2" in the setvalue method, I get an error, because
it's not a type, System.Windows.Forms.AnchorStyles.Bottom, but a value of 2.
When assigning the value directly to the button's anchor property, it works
fine. It's just the setvalue method that does not work. I need to somehow
find out how to get setvalue to work properly. Please help(perhaps I'm not
getting my question across properly, let me know if this is the case -
thanks)!

Erol

"Alan Pretre" wrote:

Quote:
"Erol" <Erol (AT) discussions (DOT) microsoft.com> wrote in message
news:17593F83-D811-497F-9F44-8F7D233E6205 (AT) microsoft (DOT) com...
In my example you'll see that I'm using the System.Type.GetType function,
but it's giving me an error. Could you please shed some light onto my
problem. Just uncomment the line in "form load" that says "Uncomment to
see
error"

Sorry I did not read your OP thoroughly enough...

I am not a VB guy, but it does not seem to me that you need to go about it
the way you are.

You should be able to just get and set the properties directly. For
example,

'Fetching
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = Button1.Anchor 'Store value in DB

'Setting
Dim Anchor As System.Windows.Forms.AnchorStyles
Anchor = System.Windows.Forms.AnchorStyles.Bottom 'Value from DB
Button1.Anchor = Anchor


-- Alan




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.