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. Test If Out Parameter has been assigned

Test If Out Parameter has been assigned

Scheduled Pinned Locked Moved C#
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.
  • M Offline
    M Offline
    MarkyBoy768
    wrote on last edited by
    #1

    I thought I understood how Out worked until I found out that you could pass a value through to the function with it. In other words the out parameter can be assigned before the function call. The compiler insists that the parameter is assigned before return even though it could have been assigned prior to the function call. I suspect the answer is no - but is there any way to test if the parameter is assigned and if so get it's value. This would be useful.

    P B C 3 Replies Last reply
    0
    • M MarkyBoy768

      I thought I understood how Out worked until I found out that you could pass a value through to the function with it. In other words the out parameter can be assigned before the function call. The compiler insists that the parameter is assigned before return even though it could have been assigned prior to the function call. I suspect the answer is no - but is there any way to test if the parameter is assigned and if so get it's value. This would be useful.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If you declare your method with an out parameter you are stating that the method will set that parameter, no matter whether the parameter was already set or not. " Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns. " Could you please tell us more about what you are trying to do?

      1 Reply Last reply
      0
      • M MarkyBoy768

        I thought I understood how Out worked until I found out that you could pass a value through to the function with it. In other words the out parameter can be assigned before the function call. The compiler insists that the parameter is assigned before return even though it could have been assigned prior to the function call. I suspect the answer is no - but is there any way to test if the parameter is assigned and if so get it's value. This would be useful.

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        Maybe you want a ref parameter, not an out one? Out, by definition, means that the method is expected to assign something into it and it may not be used for input.

        1 Reply Last reply
        0
        • M MarkyBoy768

          I thought I understood how Out worked until I found out that you could pass a value through to the function with it. In other words the out parameter can be assigned before the function call. The compiler insists that the parameter is assigned before return even though it could have been assigned prior to the function call. I suspect the answer is no - but is there any way to test if the parameter is assigned and if so get it's value. This would be useful.

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

          Even if you pass the out parameter with a value it will need to be assigned a value in the function before being accessed, but if you feel like you want to assign it a value before rather use ref not out.

          "I cannot confirm my earlier denial and I cannot deny my earlier confirmation and DON'T QUOTE ME ON THAT"

          M 1 Reply Last reply
          0
          • C Comfizzy

            Even if you pass the out parameter with a value it will need to be assigned a value in the function before being accessed, but if you feel like you want to assign it a value before rather use ref not out.

            "I cannot confirm my earlier denial and I cannot deny my earlier confirmation and DON'T QUOTE ME ON THAT"

            M Offline
            M Offline
            MarkyBoy768
            wrote on last edited by
            #5

            Yeah, I suppose I wanted to have the option of either assigning the parameter before the function call or within it. I hoped this would have worked... private void SomeFunc(out string TStr) { if(TStr == null) TStr = ""; } But of course it doesn't. No worries ... I'll just use ref ... Thanks anyway.

            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