Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need to use a String as a command.

Need to use a String as a command.

Scheduled Pinned Locked Moved C#
csharptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PyroManiak
    wrote on last edited by
    #1

    I've got something like this String[] thing = {"name", "number"} int count = 0 for (count < 3; count++) { String command = "this." + thing[count]; command = input.readline(); } this.name and this.number are datatypes I've created and need to dynamically get access to to create entries. Unfortunatly I dont know how to get c# to use command literally, rather then just overwrite the string. Anyone got this figured out? Thanks!

    S 1 Reply Last reply
    0
    • P PyroManiak

      I've got something like this String[] thing = {"name", "number"} int count = 0 for (count < 3; count++) { String command = "this." + thing[count]; command = input.readline(); } this.name and this.number are datatypes I've created and need to dynamically get access to to create entries. Unfortunatly I dont know how to get c# to use command literally, rather then just overwrite the string. Anyone got this figured out? Thanks!

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Reflection is your friend:

      for (int count = 0;count < thing.Length; count++)
      {
      FieldInfo info = this.GetType().GetField(thing[count], BindingFlags.NonPublic);
      info.SetValue(this, input.readline());
      }

      Note that with BindingFlags.NonPublic I assume that both variables are declared private. If not, take a look at the help for the GetField method and the BindingFlags enumeration and you should be able to figure out what needs to be changed.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups