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. Variable Declared

Variable Declared

Scheduled Pinned Locked Moved C#
json
12 Posts 7 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.
  • M Muhammad Nauman Yousuf

    Dear All, Is there any way that we can check if a specific variable is declared or not Thank you! M. Nauman Yousuf

    "Mess with the Best, Die like the rest"

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

    You can use reflection, but it seems odd to me that you wouldn't know, that's a compile time thing.

    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    1 Reply Last reply
    0
    • M Muhammad Nauman Yousuf

      Dear All, Is there any way that we can check if a specific variable is declared or not Thank you! M. Nauman Yousuf

      "Mess with the Best, Die like the rest"

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #4

      M. Nauman Yousuf wrote:

      Is there any way that we can check if a specific variable is declared or not

      Yes. Use Reflection for that.

      SSK.

      1 Reply Last reply
      0
      • G Guffa

        M. Nauman Yousuf wrote:

        Is there any way that we can check if a specific variable is declared or not

        Perhaps using reflection. However, if a solution contains the words "using reflection", it's usually an indication that you are thinking backwards somehow. What is it that you are trying to accomplish really?

        --- single minded; short sighted; long gone;

        M Offline
        M Offline
        Muhammad Nauman Yousuf
        wrote on last edited by
        #5

        I am designing a simple display / edit page, by passing a variable in the url of that page. On page load i check for the variable and populate the edit more form accordingly. But always get an error if the variable is not present. A simple technique of try catch would be my final option if no other alternative is available Thank you! M. Nauamn Yousuf

        "Mess with the Best, Die like the rest"

        C M L 3 Replies Last reply
        0
        • M Muhammad Nauman Yousuf

          I am designing a simple display / edit page, by passing a variable in the url of that page. On page load i check for the variable and populate the edit more form accordingly. But always get an error if the variable is not present. A simple technique of try catch would be my final option if no other alternative is available Thank you! M. Nauamn Yousuf

          "Mess with the Best, Die like the rest"

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

          I recommend reading the guide on how to ask questions, so they can be understood. Also, this is an ASP.NET question. However, the answer to what you're asking now ( as opposed to what you asked before ) is that, trivially, the query string is available to you as part of the URL, or as a collection of variables. You can iterate through it for the value you need, or look for it within the string. This does not require reflection, in fact the query string variables do not exist as variables in your code, until you declare them and search for the values in the query string to set them.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          M 1 Reply Last reply
          0
          • M Muhammad Nauman Yousuf

            I am designing a simple display / edit page, by passing a variable in the url of that page. On page load i check for the variable and populate the edit more form accordingly. But always get an error if the variable is not present. A simple technique of try catch would be my final option if no other alternative is available Thank you! M. Nauamn Yousuf

            "Mess with the Best, Die like the rest"

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #7

            Do you mean if the variable is part of the url... http://www.mysite.com//myPage.aspx?MyVariable1=this&Variable2=that And you want to check for MyVariable1 or MyVariable2 existing in the Request collection?

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            M 1 Reply Last reply
            0
            • C Christian Graus

              I recommend reading the guide on how to ask questions, so they can be understood. Also, this is an ASP.NET question. However, the answer to what you're asking now ( as opposed to what you asked before ) is that, trivially, the query string is available to you as part of the URL, or as a collection of variables. You can iterate through it for the value you need, or look for it within the string. This does not require reflection, in fact the query string variables do not exist as variables in your code, until you declare them and search for the values in the query string to set them.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              M Offline
              M Offline
              Malcolm Smart
              wrote on last edited by
              #8

              Cheers CG - that's twice you've just beaten me to it, but I'm not deleting my answer this time.

              "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

              1 Reply Last reply
              0
              • M Malcolm Smart

                Do you mean if the variable is part of the url... http://www.mysite.com//myPage.aspx?MyVariable1=this&Variable2=that And you want to check for MyVariable1 or MyVariable2 existing in the Request collection?

                "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                M Offline
                M Offline
                Muhammad Nauman Yousuf
                wrote on last edited by
                #9

                yes Malcolm thats exactly the case

                "Mess with the Best, Die like the rest"

                J 1 Reply Last reply
                0
                • M Muhammad Nauman Yousuf

                  yes Malcolm thats exactly the case

                  "Mess with the Best, Die like the rest"

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #10

                  if(string.IsNullOrEmpty(Request.QueryString["myVariable"])) { // the variable does not exist in the URL }

                  --- How to get answers to your questions[^]

                  G 1 Reply Last reply
                  0
                  • J J4amieC

                    if(string.IsNullOrEmpty(Request.QueryString["myVariable"])) { // the variable does not exist in the URL }

                    --- How to get answers to your questions[^]

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    J4amieC wrote:

                    if(string.IsNullOrEmpty(Request.QueryString["myVariable"])) { // the variable does not exist in the URL }

                    Actually, if the string is an empty string, the variable does exist in the url.

                    if (Request.QueryString["myVariable"] == null) {
                    // the variable does not exist in the URL
                    }

                    if (string.IsNullOrEmpty(Request.QueryString["myVariable"])) {
                    // the variable does not exist in the URL, or is empty
                    }

                    --- single minded; short sighted; long gone;

                    1 Reply Last reply
                    0
                    • M Muhammad Nauman Yousuf

                      I am designing a simple display / edit page, by passing a variable in the url of that page. On page load i check for the variable and populate the edit more form accordingly. But always get an error if the variable is not present. A simple technique of try catch would be my final option if no other alternative is available Thank you! M. Nauamn Yousuf

                      "Mess with the Best, Die like the rest"

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      try-catch is two orders of magnitude easier than reflection !

                      Luc Pattyn


                      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                      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