468,107 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Dictionary data structure

Hello,

I need a kind of lightweight data structure known as "associative array". It
will store a few values that I need to access using textual keys. The
Hashtable is too heavy for me.
I also need to serialize data to Session state or View State (in ASP.NET).

I think there must be a class in .NET I can use. What collection should I
use?

Thanks
Leszek
Nov 16 '05 #1
7 2880
Leszek,

Unfortunately, the Hashtable is going to be your best bet. Unless you
want to code all that searching for indexes and whatnot. What makes it too
"heavy"?

If you are that concerned, then you can use a ListDictionary instance
(in the System.Collections.Specialized namespace), but you will pay for it
in terms of lookup times (because it will iterate through the list, instead
of hashing the keys).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hello,

I need a kind of lightweight data structure known as "associative array".
It
will store a few values that I need to access using textual keys. The
Hashtable is too heavy for me.
I also need to serialize data to Session state or View State (in ASP.NET).

I think there must be a class in .NET I can use. What collection should I
use?

Thanks
Leszek

Nov 16 '05 #2
Thank you very much.

Regarding Hashtable I found it cound not be serialized into ASP.NET Session
State. It was a case when I used the State Server process. I did not
investigate this issue, just created a kind of workaroud assuming that
Hashtables are somewhat too complex ("too heavy") for .NET to keep them is
State Server Session. For example strings are kept without any problems
(they are "light").
Now I need to use "associative array" again that's why I am concerned with
"surprises" of Hashtable.

Leszek

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oj**************@TK2MSFTNGP09.phx.gbl...
Leszek,

Unfortunately, the Hashtable is going to be your best bet. Unless you
want to code all that searching for indexes and whatnot. What makes it too "heavy"?

If you are that concerned, then you can use a ListDictionary instance
(in the System.Collections.Specialized namespace), but you will pay for it
in terms of lookup times (because it will iterate through the list, instead of hashing the keys).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hello,

I need a kind of lightweight data structure known as "associative array". It
will store a few values that I need to access using textual keys. The
Hashtable is too heavy for me.
I also need to serialize data to Session state or View State (in ASP.NET).
I think there must be a class in .NET I can use. What collection should I use?

Thanks
Leszek


Nov 16 '05 #3
Leszek Taratuta wrote:
assuming that
Hashtables are somewhat too complex ("too heavy") for .NET to keep them is
State Server Session. For example strings are kept without any problems
(they are "light").


Perhaps you should use the word "unstorable in ASP sessions" to describe
the problem you have with Hashtable, calling it "heavy" mislead (at
least me) to thinking of performance of lookup/insert/remove.

I know nothing of ASP, but couldn't you just inherit from Hashtable and
implement serialization/deserialization yourself?

--
Helge
Nov 16 '05 #4
As a workaround I created my own serializer Hashtable-->String and vice
versa. It works fine for me.
Regarding my original question about "assotiative array" collection in .NET,
I can see that Hashtable is the only viable choice. I mean it is built-in
and ready to use with minimal coding.

So I used Hashtable. It is kept without any problems in APS.NET View State
(I've tested this).

Thanks,
Leszek Taratuta

"Helge Jensen" <he**********@slog.dk> wrote in message
news:#a**************@TK2MSFTNGP09.phx.gbl...
Leszek Taratuta wrote:
assuming that
Hashtables are somewhat too complex ("too heavy") for .NET to keep them is State Server Session. For example strings are kept without any problems
(they are "light").


Perhaps you should use the word "unstorable in ASP sessions" to describe
the problem you have with Hashtable, calling it "heavy" mislead (at
least me) to thinking of performance of lookup/insert/remove.

I know nothing of ASP, but couldn't you just inherit from Hashtable and
implement serialization/deserialization yourself?

--
Helge

Nov 16 '05 #5
Leszek,

You are going to have this problem no matter what data structure you
use. The HashTable is indeed serializable, which means that the error that
you are getting referrs to the fact that something you are placing in the
hashtable is not serializable, not the hashtable itself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thank you very much.

Regarding Hashtable I found it cound not be serialized into ASP.NET
Session
State. It was a case when I used the State Server process. I did not
investigate this issue, just created a kind of workaroud assuming that
Hashtables are somewhat too complex ("too heavy") for .NET to keep them is
State Server Session. For example strings are kept without any problems
(they are "light").
Now I need to use "associative array" again that's why I am concerned with
"surprises" of Hashtable.

Leszek

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Oj**************@TK2MSFTNGP09.phx.gbl...
Leszek,

Unfortunately, the Hashtable is going to be your best bet. Unless
you
want to code all that searching for indexes and whatnot. What makes it

too
"heavy"?

If you are that concerned, then you can use a ListDictionary instance
(in the System.Collections.Specialized namespace), but you will pay for
it
in terms of lookup times (because it will iterate through the list,

instead
of hashing the keys).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hello,
>
> I need a kind of lightweight data structure known as "associative array". > It
> will store a few values that I need to access using textual keys. The
> Hashtable is too heavy for me.
> I also need to serialize data to Session state or View State (in ASP.NET). >
> I think there must be a class in .NET I can use. What collection should I > use?
>
> Thanks
> Leszek
>
>



Nov 16 '05 #6
That's valuable information.
I am going to check the elements of my Hashtable and find what is not
serializable.

Thanks a lot!

Leszek

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#u**************@TK2MSFTNGP15.phx.gbl...
Leszek,

You are going to have this problem no matter what data structure you
use. The HashTable is indeed serializable, which means that the error that you are getting referrs to the fact that something you are placing in the
hashtable is not serializable, not the hashtable itself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thank you very much.

Regarding Hashtable I found it cound not be serialized into ASP.NET
Session
State. It was a case when I used the State Server process. I did not
investigate this issue, just created a kind of workaroud assuming that
Hashtables are somewhat too complex ("too heavy") for .NET to keep them is State Server Session. For example strings are kept without any problems
(they are "light").
Now I need to use "associative array" again that's why I am concerned with "surprises" of Hashtable.

Leszek

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Oj**************@TK2MSFTNGP09.phx.gbl...
Leszek,

Unfortunately, the Hashtable is going to be your best bet. Unless
you
want to code all that searching for indexes and whatnot. What makes it

too
"heavy"?

If you are that concerned, then you can use a ListDictionary instance (in the System.Collections.Specialized namespace), but you will pay for
it
in terms of lookup times (because it will iterate through the list,

instead
of hashing the keys).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leszek Taratuta" <ad*@taratuta.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hello,
>
> I need a kind of lightweight data structure known as "associative

array".
> It
> will store a few values that I need to access using textual keys. The
> Hashtable is too heavy for me.
> I also need to serialize data to Session state or View State (in

ASP.NET).
>
> I think there must be a class in .NET I can use. What collection
should I
> use?
>
> Thanks
> Leszek
>
>



Nov 16 '05 #7
Nicholas Paldino [.NET/C# MVP] wrote:
The HashTable is indeed serializable

! That surprized me.
Ok, to give some context I am assuming:

==MyClass.cs:
using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;

namespace TestHashtableSerialization
{
[XmlRoot(ElementName="MyClass")]
public class MyClass
{
[XmlElement(ElementName="Table")]
public Hashtable table = new Hashtable();

public MyClass()
{ }
}
}
==End of MyClass.cs

==Start.cs (I am skipping the usual code that comes with forms)
using System;
using System.Collections;

using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TestHashtableSerialization
{
public class Start : System.Windows.Forms.Form
{
public Start()
{
// Required for Windows Form Designer support
InitializeComponent();
}

[STAThread]
static void Main()
{
Application.Run(new Start());
}

private void btn_Test_Click(object sender, System.EventArgs e)
{
try
{
MyClass c = new MyClass();
c.table[0]="zeroth";
c.table[1]="first";

XmlSerializer serializer = new
XmlSerializer(typeof(TestHashtableSerialization.My Class));
TextWriter writer = new StreamWriter("SerializedMyClass.xml");
serializer.Serialize(writer, c);
writer.Close();
MessageBox.Show(this, "MyClass serialized.");
}
catch(Exception e2)
{
MessageBox.Show(this,
"Exception:\n"+e2.ToString());
}
}

}
}

==End of Start.cs

Running this and clicking on the btn_Test button I get an exception:

System.InvalidOperationException: There was an error reflecting type
'TestHashtableSerialization.MyClass'.
---> System.NotSupportedException: The type
System.Collections.Hashtable is not supported because it implements
IDictionary.

So, assuming we were talking about Serialization to Xml files via
XmlSerializer...
....if, as you say, Hashtables ARE Serializable 'out-of-the-box', what
am I missing/doing wrong?

I am *Very Interested*,

thanks,
F.O.R.

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

57 posts views Thread by Egor Bolonev | last post: by
7 posts views Thread by rickle | last post: by
reply views Thread by Spur | last post: by
70 posts views Thread by jojoba | last post: by
2 posts views Thread by joe.kimbler | last post: by
3 posts views Thread by Jordan | last post: by
16 posts views Thread by ravi | last post: by
14 posts views Thread by lee | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.