Alex Rivera | Logout

is it a good idea to create an enum for the key names of session values?

Asked 2009-01-21T14:31:08.407
9

instead of doing

 session("myvar1") = something
 session("myvar2") = something
 session("myvar3") = something
 session("myvar4") = something

is doing

enum sessionVar
   myvar1
   myvar2
   myvar3
   myvar4
end enum


 session(sessionVar.myvar1.tostring) = something
 session(sessionVar.myvar2.tostring) = something
 session(sessionVar.myvar3.tostring) = something
 session(sessionVar.myvar4.tostring) = something

would be better?

Edit
Report

1 Answer

0

For the simple thing of removing all of the string key references, I would go with global static/shared constants visible at application scope.

Otherwise, the strongly typed simple wrapper for session variables is a superior alternative, and it's far more OOP-friendly considering that you get intellisense and Object Browser compatibility.

The only way I can see the enum being of much value is to be used to index into an array or a similar list. But even then you have to cast the enum to an int.

So, you could have an array that you load at application startup with all of your session variable keys and then an enum for the indexes. However, given that the Session object derives from HttpSessionState, which derives from IEnumerable, you should be able to just do a foreach loop over the session variables, if you need to.

answered 2009-01-21T14:47:30.403

Your Answer