Variable initialized once with dynamic value

Md. Parvez Alam

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.

Sweeper

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();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create a dynamic variable and assign value to it?

Is float value guaranteed to be 0 when a float variable is initialized to 0.0?

'Value never used' changes to 'variable not initialized'

Value of a non-initialized variable in C++

Why my variable hasn't its initialized value?

inline variable is initialized more than once

Rounding a decimal value with dynamic variable

c++ : When is a declared variable is initialized without assigning it to any value?

Accessing variable value initialized in one function from another function in Solidity

C++ program giving garbage value to an initialized variable

Variable initialized with garbage value in copy constructor

Flutter/Dart - Dynamic value in a variable

Field variable reverts to initialized value when printed by another field variable

getting the dynamic ID value from variable

Unable to access value of global variable in functions other than it is initialized

Get dynamic scope variable value angularjs

CGPoint variable initialized in @interface of .m file can't have value

Ignoring the initialized value in a variable

creating a variable within tf.variable_scope(name), initialized from another variable's initialized_value

Creating global variable with Python, changes value after initialized

How to increment the value of initialized variable in mongodb query and that initialized value variable I am using to fetch data from object

Dynamic variable value on HTML with Django

modifying the value of a non static variable which was initialized with a static variable modifies the static variable as well

Variable is not initialized

Is it true that if the address of an uninitialized variable is taken, it is initialized to default value?

Is it possible within a function to get the memory address of the variable initialized by the return value?

Why the variable 'i' is returning a value of 0 when even not initialized?

Store a fixed value in the variable only once

is const variable in javascript function initialized every time when it called or just once?