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. Reading value from a variabile

Reading value from a variabile

Scheduled Pinned Locked Moved C#
tutorialquestion
12 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.
  • A Offline
    A Offline
    Andrei Ungureanu
    wrote on last edited by
    #1

    Hi, I have a small question. I have some variables with almost the same name. For example: string message_108 = "fngfh"; string message_109 = "88"; string message_110 = "22"; ..... string message_200 = "ffb"; All I want is to create some kind of method which receives the number of the message (e.g. 110) and that returns the description(value) of that message (message_110). Can you give me any suggestions.

    Do your best to be the best

    P C E 3 Replies Last reply
    0
    • A Andrei Ungureanu

      Hi, I have a small question. I have some variables with almost the same name. For example: string message_108 = "fngfh"; string message_109 = "88"; string message_110 = "22"; ..... string message_200 = "ffb"; All I want is to create some kind of method which receives the number of the message (e.g. 110) and that returns the description(value) of that message (message_110). Can you give me any suggestions.

      Do your best to be the best

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Well, this sounds like a homework question. Why would anybody else want to use variable names like that? If you really wanted to use *indexed* versions of the variable, you would use an array of some description.

      Deja View - the feeling that you've seen this post before.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Well, this sounds like a homework question. Why would anybody else want to use variable names like that? If you really wanted to use *indexed* versions of the variable, you would use an array of some description.

        Deja View - the feeling that you've seen this post before.

        A Offline
        A Offline
        Andrei Ungureanu
        wrote on last edited by
        #3

        Actually it's not a homework, and I've tried with arrays and arraylists but I need another way that this could be done.

        Do your best to be the best

        C 1 Reply Last reply
        0
        • A Andrei Ungureanu

          Hi, I have a small question. I have some variables with almost the same name. For example: string message_108 = "fngfh"; string message_109 = "88"; string message_110 = "22"; ..... string message_200 = "ffb"; All I want is to create some kind of method which receives the number of the message (e.g. 110) and that returns the description(value) of that message (message_110). Can you give me any suggestions.

          Do your best to be the best

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Rework it into an array or hashtable.


          Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

          1 Reply Last reply
          0
          • A Andrei Ungureanu

            Actually it's not a homework, and I've tried with arrays and arraylists but I need another way that this could be done.

            Do your best to be the best

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            karkster wrote:

            I've tried with arrays and arraylists but I need another way that this could be done

            Then we need to look at the bigger picture, because at the moment what you are doing does not make any sense.


            Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

            A 1 Reply Last reply
            0
            • C Colin Angus Mackay

              karkster wrote:

              I've tried with arrays and arraylists but I need another way that this could be done

              Then we need to look at the bigger picture, because at the moment what you are doing does not make any sense.


              Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

              A Offline
              A Offline
              Andrei Ungureanu
              wrote on last edited by
              #6

              Let me put it in another form. I want to access a Property of a class, but I have the name of the property in a string.

              Do your best to be the best

              P C 2 Replies Last reply
              0
              • A Andrei Ungureanu

                Let me put it in another form. I want to access a Property of a class, but I have the name of the property in a string.

                Do your best to be the best

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Use reflection. There are numerous examples on Google on how to do this. Hint - look at GetType().GetProperty(...).GetValue(...)

                Deja View - the feeling that you've seen this post before.

                C 1 Reply Last reply
                0
                • A Andrei Ungureanu

                  Let me put it in another form. I want to access a Property of a class, but I have the name of the property in a string.

                  Do your best to be the best

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Have you thought of using reflection? Reflection won't work with local variables, but will work with properties.


                  Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Use reflection. There are numerous examples on Google on how to do this. Hint - look at GetType().GetProperty(...).GetValue(...)

                    Deja View - the feeling that you've seen this post before.

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    Ya beat me to it!


                    Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

                    P 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Have you thought of using reflection? Reflection won't work with local variables, but will work with properties.


                      Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

                      A Offline
                      A Offline
                      Andrei Ungureanu
                      wrote on last edited by
                      #10

                      :) Thanks. It works with Reflection.

                      Do your best to be the best

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        Ya beat me to it!


                        Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        :-D

                        Deja View - the feeling that you've seen this post before.

                        1 Reply Last reply
                        0
                        • A Andrei Ungureanu

                          Hi, I have a small question. I have some variables with almost the same name. For example: string message_108 = "fngfh"; string message_109 = "88"; string message_110 = "22"; ..... string message_200 = "ffb"; All I want is to create some kind of method which receives the number of the message (e.g. 110) and that returns the description(value) of that message (message_110). Can you give me any suggestions.

                          Do your best to be the best

                          E Offline
                          E Offline
                          Ennis Ray Lynch Jr
                          wrote on last edited by
                          #12

                          Use reflection. Because you are using members you will have to use FieldInfo not PropertyInfo. Of course, like others have mentioned, I would use a hash instead of members.


                          File Not Found

                          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