DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to translate string to Object

  1. #1
    Join Date
    Mar 2004
    Posts
    72

    How to translate string to Object

    Is there some sort of evaluation function in .Net that will let you take a string and evaluate it as an object?

    ex:

    //Contact is a custom Object that contains properties for FirstName
    //And LastName


    Contact c = new Contact( "Alex Banks" );

    //This would write the value of the FirstName
    //Which in this case is Alex

    Response.Write( c.FirstName );

    //Now lets say we have a string that looks like this:
    string myObjString = "c.FirstName";

    //Is there a way to change this string into regular object code
    //So that we can use this string to access the value of c.FirstName?

    //For example in ActionScript you can say something like this:

    var theName = eval(myObjString);

    -or-

    var theName = eval("c.FirstName');

    -or-

    var theLastName = eval("c").LastName;

    //How can I go about doing something similar in C#.NET?

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I don't know of any way to evaluate the variable name ("c" in your example), but given an object instance, you can use reflection to access its fields, properties and methods: http://www.codeguru.com/Csharp/Cshar...cle.php/c4257/
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center