Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable initialized once with dynamic value

I have declared a constant variable which is being stored in cookie so its Ok because differnt browser will have a different copy.

private const string CookieName = "TempData";

Now I want to store it at third location where all user data will be stored.

So How can I declare something like

private const string CookieName = "TempData" + DataTime.Now.Tick.tostring(); So that each user will have differnet cookiename stored at third location.

Please advise.

like image 526
Md. Parvez Alam Avatar asked Dec 09 '25 17:12

Md. Parvez Alam


1 Answers

For that you cannot use a const field. Anything with const modifier must be able to be evaluated to a constant at compile time.

What you want is a static field initialised by a static constructor

public class YourSurroundingClass {
    private static readonly string CookieName;

    static YourSurroundingClass() {
        CookieName = "TempData" + DateTime.Now.Ticks
            .ToString();
    }
}
like image 107
Sweeper Avatar answered Dec 11 '25 07:12

Sweeper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!