473,385 Members | 1,353 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,385 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 889
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...
4
by: kang jia | last post by:
hi i am doing mailinglist currently. the code in my first page is like this : : <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css" /> <script language="JavaScript"> ...
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...
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.