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. get the name of a parameter as a string

get the name of a parameter as a string

Scheduled Pinned Locked Moved C#
question
5 Posts 4 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.
  • C Offline
    C Offline
    C Scharbe
    wrote on last edited by
    #1

    how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------

    C C B 3 Replies Last reply
    0
    • C C Scharbe

      how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Reflection is how you do this.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • C C Scharbe

        how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------

        C Offline
        C Offline
        Coding C
        wrote on last edited by
        #3

        Hi, Describe the problem completely. do you want the list of members from class abc in to the class ReadParametername? and if u want to pass name of the parameters as string variable then why do u want to do it? thanks.

        C 1 Reply Last reply
        0
        • C Coding C

          Hi, Describe the problem completely. do you want the list of members from class abc in to the class ReadParametername? and if u want to pass name of the parameters as string variable then why do u want to do it? thanks.

          C Offline
          C Offline
          C Scharbe
          wrote on last edited by
          #4

          ok, i read an article about reflection! and found out som new: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { Type TypeToReflect = typeof(abc); System.Reflection.MemberInfo[] Members = TypeToReflect.GetMembers(); // now i get an array with the members of the object 'abc' // can i get an enum of the membernames in realtime? ??? // so that i can select any membername from the enum like: string anyMemberName = enumAbcMembers.parametername.ToString(); } -------------------------------------

          1 Reply Last reply
          0
          • C C Scharbe

            how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------

            B Offline
            B Offline
            beatles1692
            wrote on last edited by
            #5

            Hi Here is another way to do the same thing. If I'm right it's one of the refactoring practices. Use an object instead of a struct(int,string,...) You can have a parameter class: ------------- Parameter ------------- +Name:string +Value:object ------------- ------------- Then one may write Parameter param1=new Parameter("abc",0); Parameter param2=new Parameter("anotherParam",1); and later: string paramName=param1.Name; I prefer this method because my intention is implied(Everyone can look at the class interface and see what's going on) Regards

            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