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. How can I create a var that doesn't get destroyed after exiting the method?

How can I create a var that doesn't get destroyed after exiting the method?

Scheduled Pinned Locked Moved C#
question
6 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.
  • X Offline
    X Offline
    xkx32
    wrote on last edited by
    #1

    hi can you please tell me how can I create a local var that doesn't get destroyed after exiting the method? thanks

    V M N 3 Replies Last reply
    0
    • X xkx32

      hi can you please tell me how can I create a local var that doesn't get destroyed after exiting the method? thanks

      V Offline
      V Offline
      Vega02
      wrote on last edited by
      #2

      I'm a bit confused by your question, as objects become eligible for garbage collection when no references to them remain in the code. Could you provide specifics on what exactly you're trying to do? What kind of variable is this, and what do you plan on doing with it when the method returns?

      X 1 Reply Last reply
      0
      • X xkx32

        hi can you please tell me how can I create a local var that doesn't get destroyed after exiting the method? thanks

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, If I understand you right??? You have to declare your variable inside your Class and not in the Method. Or you have to store the Value external (Database or XML). Hope I helped you. All the best, Martin

        1 Reply Last reply
        0
        • V Vega02

          I'm a bit confused by your question, as objects become eligible for garbage collection when no references to them remain in the code. Could you provide specifics on what exactly you're trying to do? What kind of variable is this, and what do you plan on doing with it when the method returns?

          X Offline
          X Offline
          xkx32
          wrote on last edited by
          #4

          I am wondering if it's possible to create a variable in a method as follow: string tttt = "Gggg"; and then after exiting the method i can still access that var

          M 1 Reply Last reply
          0
          • X xkx32

            I am wondering if it's possible to create a variable in a method as follow: string tttt = "Gggg"; and then after exiting the method i can still access that var

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            No, I think! But, somebody correct me please. -- modified at 3:38 Friday 21st July, 2006

            1 Reply Last reply
            0
            • X xkx32

              hi can you please tell me how can I create a local var that doesn't get destroyed after exiting the method? thanks

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              Hello One of the main ideas of C# -if not the main- is being managed by the garbage collector. That's why they call it a managed code -while there is an unmanaged code-. This task is quite advanced and is concerned with memory locations. Even then you'd have to store the memory location -otherwise known as pointer- somewhere to access the value. You still can do something similar to that in C# with reference type only using WeakReference class. Revise it in your MSDN Yet again what's exactly your point? Sometimes something that's possible is not always the best solution for a matter!! I'm sure there is a better approach to solve your problem -if it's not a mere programming challenge of some sort- Regards:rose:

              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