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

Application Object Stores the Same Key Multiple Times

P: n/a
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.
--
Alan Foxmore
May 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
thats by design. if you add an exisiting key, it replaces the old value.

-- bruce (sqlwork.com)
"Alan Foxmore" <AF******@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
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.
--
Alan Foxmore

May 25 '06 #2

P: n/a


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.



"Alan Foxmore" <AF******@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
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.
--
Alan Foxmore


May 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.