Results 1 to 3 of 3

Thread: HowTo: Make a string VALUE into a VARIABLE?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Question HowTo: Make a string VALUE into a VARIABLE?

    I need a way to "translate" a string into a field/variable name?

    For example, I have a string with the value of "FirstName" and I want to then programmatically access user.FirstName

    How would I do this?

    In C, I'd use pointers, but is there a way to do it in C# and .Net?

    Any help would be much appreciated!

    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HowTo: Make a string VALUE into a VARIABLE?

    There seems to be have been a spate of questions like this lately. In most cases when people ask how to do this it is just the case that they have designed their app incorrectly. There aren't many situations where doing something like this is genuinely needed. If you are in a situation where it is needed then you need to use reflection. You get a Type object for your object by calling GetType. The Type class has methods to get FieldInfo, PropertyInfo, MethodInfo, etc. objects that you can use to indirectly access members of an instance of that type. In your case you should take a look at the FieldInfo class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Re: HowTo: Make a string VALUE into a VARIABLE?

    Thanks for the feedback.

    I'll ping the developer further to see whether what they are doing makes sense. This particular developer is usually a reliable one, when it comes to design, so it didn't occur to me to question them on why they needed what they needed.

    Thanks for making me double-check!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width