Need Hashtable and Arraylist

18.7k views Asked by At

I am trying to use someone else's C# classes in my Windows 7 Phone app. The classes use objects of type Hashtable.

The file in question has

using System.Collections;

at the top, so I'm assuming that's the Hashtable object it wants.

When I try to build my solution, I get errors that the type or namespace name 'Hashtable' could not be found, are you missing a using directive or assembly reference.

In Microsoft's documentation of Hashtable, I see it says Assembly: mscorlib

But if I try to add mscorlib via Project>Add Reference, VS says it can't add it because it is automatically referenced by the build system.

What am I missing?

5

There are 5 answers

0
SLaks On BEST ANSWER

The non-generic collections, including ArrayList and HashTable, are not included in Silverlight.
These classes are hold-overs from .Net 1.0 (which didn't have generics) and should not be used in new code.

Instead, you should use the generic collections—List<T> and Dictionary<TKey, TValue>.

0
Matt Greer On

There are different mscorlibs depending on which .NET framework you are using. If you look in the "Other Versions" dropdown on the MSDN page, you will see Hashtable is not a part of Silverlight. You will need to use a Dictionary<Object, Object> (or ideally more strongly typed keys and value types)

Hashtable is not in Silverlight

But Dictionary is

0
Lukasz Madon On

System.Collection is a legacy of first version of .Net - no generic types.

To fix your code use Dictorany class which is a hashtable at heart, and List insted of ArrayList.

6
Gabe On

You have a few options:

  1. Change your import to using System.Collections.Generic; and change every use of a HashTable to Dictionary<> and ArrayList to List<>.

  2. You might be able to get away with:

    using HashTable = System.Collections.Generic.Dictionary<object, object>;
    using ArrayList = System.Collections.Generic.List<object>;
    Note that any future maintainer will hate you for doing this.

  3. But it's better to refactor the code to use the generic collections properly.

  4. Create a class Hashtable in a namespace System.Collections, implement IDictionary<object, object> by forwarding everything to an inner Dictionary<object, object> and implement the necessary changes in behavior (locking, missing keys, etc.); Create an ArrayList by encapsulation List<object>. (suggested by henon)

0
Alfonso Leon On

It worked for me changing:

Hashtable for Dictionary<object, object>

NameValueCollection for Dictionary<object, object>

Other problem I encoutered is Encoding.ASCII is not defined either, I sorted that with a function a stackoverflow lad wrote:

public static byte[] StringToAscii(string s) {
    byte[] retval = new byte[s.Length];
    for (int ix = 0; ix < s.Length; ++ix) {
        char ch = s[ix];
        if (ch <= 0x7f) retval[ix] = (byte)ch;
        else retval[ix] = (byte)'?';
    }
    return retval;
}

credits here:

ASCIIEncoding In Windows Phone 7

So finally to return the ASCII this is what to do:

return StringToAscii(Encoding.Unicode.GetString(result.ToArray()));