none
Guid becomes NULL after a while RRS feed

  • Question

  • Hi to everyone

    Currently I'm facing a very weird issue.

    I have a GUID variable which is static and which has a value from DB (and everything it's fine) and I use it through the code 

    but after a while it just became NULL, without assigning any values..

    and I don't have any ideas how that can be, the variable suppose to keep it's value if it's static, right?

    Thank you in advance for helping me to solve this problem.

    Vadim

    Saturday, September 14, 2013 1:16 AM

Answers

  • Static just means that the variable is shared among instances of the class.  If you want a variable that does not change its value after one is assigned, you should use const.  

    public const string sMyVar = "Value";


    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:23 AM
    Saturday, September 14, 2013 1:42 AM
  • Hi vadimaus,

    Maybe you can try to create a static class and create a static property in it like the following code.

     public static class StaticClass
        {
           public static string GUID { get; set; }
        }

    You can use it anywhere you need like below.

    StaticClass.GUID=...

    Hopt it can help u!

    Regards


    If my posting is the answer that what you need, please mark as answer.Thanks.


    • Edited by Ada Semon Tuesday, September 24, 2013 2:13 AM
    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:23 AM
    Tuesday, September 24, 2013 2:12 AM
  • Hi Vadim,

    Welcome to MSDN forums.

    For static variable, I would suggest you refer to the link below.

    http://msdn.microsoft.com/en-us/library/aa733578(v=vs.60).aspx

    http://en.wikipedia.org/wiki/Static_variable

    I agree with Ada, you can try that way.

    I will temporarily mark Ada’s last response as an answer. You can unmark it if they provide no help.

    If you fix it by yourself, I would suggest you share your solutions & experience here, it will be very beneficial for other community members who have similar questions.

    Thanks for your understanding.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:24 AM
    Tuesday, September 24, 2013 8:58 AM

All replies

  • Static just means that the variable is shared among instances of the class.  If you want a variable that does not change its value after one is assigned, you should use const.  

    public const string sMyVar = "Value";


    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:23 AM
    Saturday, September 14, 2013 1:42 AM
  • So you want to say that it's something to do with Garbage Collector? or AppDomain?

    Actually I can't use 'const' because I need to assign a value at runtime

    Thanks

    Saturday, September 14, 2013 1:47 AM
  • One way to accomplish your goal is to encapsulate a private variable within a public read method, so that the value can be read but not assigned through that method.  A value can be assigned to the private variable in the class constructor, or by another public method.
    Saturday, September 14, 2013 2:26 AM
  • Thank you for your quick reply

    but I have it already like that, and I have another function that is assigning a value to private field using out parameter.

    Thanks

    Saturday, September 14, 2013 2:36 AM
  • Hi vadimaus,

    Maybe you can try to create a static class and create a static property in it like the following code.

     public static class StaticClass
        {
           public static string GUID { get; set; }
        }

    You can use it anywhere you need like below.

    StaticClass.GUID=...

    Hopt it can help u!

    Regards


    If my posting is the answer that what you need, please mark as answer.Thanks.


    • Edited by Ada Semon Tuesday, September 24, 2013 2:13 AM
    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:23 AM
    Tuesday, September 24, 2013 2:12 AM
  • Hi Vadim,

    Welcome to MSDN forums.

    For static variable, I would suggest you refer to the link below.

    http://msdn.microsoft.com/en-us/library/aa733578(v=vs.60).aspx

    http://en.wikipedia.org/wiki/Static_variable

    I agree with Ada, you can try that way.

    I will temporarily mark Ada’s last response as an answer. You can unmark it if they provide no help.

    If you fix it by yourself, I would suggest you share your solutions & experience here, it will be very beneficial for other community members who have similar questions.

    Thanks for your understanding.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:24 AM
    Tuesday, September 24, 2013 8:58 AM