473,320 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
2 1530
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fred Newton | last post by:
Just wondering if there are any strong arguments one way or the other for storing a small value (6 bytes) in the ViewState vs. Session state. The value is needed in only one page in a large ASP.NET...
2
by: Frank | last post by:
Can I do this? I add a session var in C# and ultimatly want to pass it into a vbscript client side activeX control. This is what I have so far but get " Object Required:'name2' " error. Can...
2
by: Alani | last post by:
Hello All, I'm a new ASP.NET programmer and I want to create a custom control consist of two properties (Number1) and (Number2) and both of them are integers and default value = 0, now I'm...
6
by: Kermit Piper | last post by:
Hello, I thought this should be easy, but... all I want to do is set the value of this state drop-down based on a session var I'm getting back from a redirect (from the processing page): <%...
1
by: irfangani | last post by:
Hi, I have two web applications one in VB.Net and other in C#. I am using Out-Process session state using SQL server. I create a session in VB.Net application. If I access the session in the...
1
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
0
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
3
by: whitey | last post by:
this code is producing the message BUT it is entering the data. What should i do? <?php if (!$_POST) { //haven't seen the form, so show it $display_block = " <form...
2
by: Cramer | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.