Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:43:32+00:00 2026-06-13T01:43:32+00:00

I want to convert string to other type. The problem is that I know

  • 0

I want to convert string to other type.
The problem is that I know the type only in runtime.
I don’t want to use a Select case.
Is a better way?
More information:
I want to build a form at runtime.
So in an xml I have the controls for that form with all the properties that i want to set a value:

 <Controls>
   <Label>
    <Text>Names</Text>
    <AutoSize>False</AutoSize>
    <Enabled>True</Enabled>
   </Label>
   <TextBox>
     <Text>Id:</Text>
     <Enabled>FALSE</Enabled>
   </TextBox>
 </Controls>

No my code is :

      For Each elem As XElement In xmlDoc.Root.Element("Controls").Elements  
         Dim oType As Type
         oType = FindType("System.Windows.Forms." & elem.Name.ToString) 'FindType is a function to return the type
         Dim cnt As New Control
         cnt = Activator.CreateInstance(oType)
                For Each proper As XElement In elem.Elements
                  Dim propName As String = proper.Name.ToString
                  Dim myPropInfo As PropertyInfo = cnt.GetType().GetProperty(propName)
                    If myPropInfo IsNot Nothing Then
                        Dim val As String = proper.Value
                       ' HERE SOMETHING TO CONVERT THE STRING TO myPropInfo.PropertyType
                        ' Setting a value to the property
                        cnt.GetType().GetProperty(propName).SetValue(cnt, val, Nothing)
                    End If
                  Next
            Me.FlowLayoutPanel1.Controls.Add(cnt)
          Next
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:43:33+00:00Added an answer on June 13, 2026 at 1:43 am

    What you’re looking for is the Convert.ChangeType method, which receives two parameters, the string you’re converting from, and the Type you’re converting to:

    Dim val As Object = proper.Value
    Dim targetProperty as PropertyInfo = cnt.GetType().GetProperty(propName)
    Dim convertedVal = Convert.ChangeType(val, targetProperty.PropertyType)
    targetProperty.SetValue(cnt, convertedVal, Nothing)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an string to an enum type using TValue, I googled
I want to convert a string like 2012-03-08T00:00:00+01:00 into Date.. I tried to use
I want to convert String to Date in different formats. For example, I am
I want to convert a string to formatted text in C# in WPF application,
I want to convert a string '30/12/2012' to '2012/12/30'. My application is set to
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert a string representations of few dozen enum types to enum
I want to convert a String to an array of objects of Character class
I want to convert below string to an array in javascript. {a:12, b:c, foo:bar}
In my c# application i want to convert a string characters to special characters.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.