By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Retrieve Value as it's Strong Type from Application or Session State

P: n/a
Using ASP.NET 3.5...

As far as I know, any time we store a value in application or session state,
it is stored as a humble 'object' type rather than it's "real" type.

For example, if we want to store an integer value in Application state:
Application["MyInt"] = 93;

Then to retrieve that value, we would use something like this:
Convert.ToInt32(Application["MyInt"]);
My question:
Is there any way we can retrieve the value of "MyInt" as an integer -
without having to convert it first from 'object'?

I'm aware of extension methods - but what about "extension properties"? Or
am I missing something that should be obvious?

Thanks.

Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Usually I make a "manager" class when using the session or application
state. Use properties in this class to retrieve the values in their
strongly typed forms. This way, not only will the types come through, but
you will also be able to access your stored values using intellisense.

For example (I use VB)

Private Shared _MyInt As String = "MyInt"

Shared Property MyInt() As Integer
Get
Return Current.Session(_MyInt)
End Get
Set(ByVal value As Integer)
Current.Session(_MyInt) = value
End Set
End Property

Hope this helps

Matt

"Cramer" <A@B.comwrote in message
news:eT**************@TK2MSFTNGP05.phx.gbl...
Using ASP.NET 3.5...

As far as I know, any time we store a value in application or session
state, it is stored as a humble 'object' type rather than it's "real"
type.

For example, if we want to store an integer value in Application state:
Application["MyInt"] = 93;

Then to retrieve that value, we would use something like this:
Convert.ToInt32(Application["MyInt"]);
My question:
Is there any way we can retrieve the value of "MyInt" as an integer -
without having to convert it first from 'object'?

I'm aware of extension methods - but what about "extension properties"? Or
am I missing something that should be obvious?

Thanks.


Jun 27 '08 #2

P: n/a
Cramer wrote:
Using ASP.NET 3.5...

As far as I know, any time we store a value in application or session state,
it is stored as a humble 'object' type rather than it's "real" type.

For example, if we want to store an integer value in Application state:
Application["MyInt"] = 93;

Then to retrieve that value, we would use something like this:
Convert.ToInt32(Application["MyInt"]);

My question:
Is there any way we can retrieve the value of "MyInt" as an integer -
without having to convert it first from 'object'?
You do not need to convert it.

A cast should be fine.

int ival = (int)Application["MyInt"];

If you want type safeness, then I would recommend storing a
single object with multiple properties in Application.

Arne
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.