Application Object Stores the Same Key Multiple Times

A

Alan Foxmore

Hi Everyone,

I'm new to ASP.NET so maybe this is easy. I'm using ASP.NET 2.0.

I'm finding that the Application object (the HttpApplicationState
object) is allowing me to add the same key multiple times. The easiest
way to describe the problem is to just show you.

In my Page_Load() I have this code:

Application.Add("Fred", "Pebbles");
Application.Add("Fred", "Wilma");
Application.Add("Fred", "Dino");

Application.Set("Fred", "Barney");

foreach (string key in Application.AllKeys) {
object val = Application.Get(key);
Trace.Write("Fred's value: " + val.ToString());
}

for (int i = 0; i < Application.Count; ++i) {
object val = Application.Get(i);
Trace.Write("val " + i + ": " + val);
}
Notice I am using the same key -- "Fred" -- multiple times.

When I look at the Trace output here's what I see:

Fred's value: Barney
Fred's value: Barney
Fred's value: Barney
val 0: Barney
val 1: Wilma
val 2: Dino



I really don't know what to make of this except that it's totally
unexpected and inconsistent with a regular hashtable's behavior. I've
read the Microsoft documentation and I didn't see any mention of this
kind of behavior. My understanding was that Application should behave
like a hashtable.

Can anyone comment on this behavior? Does it seem right?

Thanks.
 
B

bruce barker \(sqlwork.com\)

thats by design. if you add an exisiting key, it replaces the old value.

-- bruce (sqlwork.com)
 
A

Alan Foxmore

thats by design. if you add an exisiting key, it replaces the old
value.

-- bruce (sqlwork.com)


Hi Bruce,

By design? Adding an existing key is NOT replacing the old value. Did
you even look at the example I gave?

I am still able to access 'Wilma' and 'Dino'. They are *not* replaced.
If I remove the call to "Application.Set("Fred", "Barney");" then
"Application.Get("Fred")" returns "Pebbles". According to what you say
(and what I would expect), it should return 'Dino'.

Take a look at the calls to Application.Add() and Application.Set() and
then look at the results.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top