Dictionary data structure

L

Leszek Taratuta

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
L

Leszek Taratuta

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 said:
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]
- (e-mail address removed)

Leszek Taratuta said:
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
 
H

Helge Jensen

Leszek said:
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?
 
L

Leszek Taratuta

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Leszek Taratuta said:
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

in
message news:[email protected]...
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]
- (e-mail address removed)

Leszek Taratuta said:
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
 
L

Leszek Taratuta

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 said:
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]
- (e-mail address removed)

Leszek Taratuta said:
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

in
message news:[email protected]...
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]
- (e-mail address removed)

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
 
O

Olorin

Nicholas said:
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.MyClass));
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.
 

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

Top