Link to home
Start Free TrialLog in
Avatar of louisiana_blues
louisiana_blues

asked on

Why am I getting this error, and how do I properly fix it? Error Option Strict On disallows implicit conversions from 'System.Collections.IDictionary' to 'System.Collections.Hashtable'

Hello:

I am a former Java programmer that is getting back into .NET after some time off.  I had worked with C# before, and prefer it over VB.NET, but this is what we are using to build right now.  Isn't a Hashtable a subclass of an IDictionary interface?  If so, why is it not allowing me to convert it, and how do I do so?

Since there is a critical time component to this, I am awarding 500 pts to anyone who can specify exactly how to fix this issue...

Thanks for all your help.
Avatar of MacNuttin
MacNuttin
Flag of United States of America image

turn option strict off
Avatar of louisiana_blues
louisiana_blues

ASKER

I would rather not do this.  It brings into play all other kinds of potentially sloppy programming tactics.  Is there a better solution?
What code are you using ? Then following works fine for me with Option Strict On:

        Dim hsh As New Hashtable
        Dim id As IDictionary = hsh
Could you post the actual line of code that is throwing your error, along with the relevant variable declarations?

Also, are you using VS2003 or VS2005?
Dim htpagevalues as IDictionary = New Hashtable()

Is fine.  Sorry, I should have expanded on this.  I call numerous functions ByRef, such as (function stub)

Protected Function SaveHashItem (ByRef htpage As Hashtable, ByVal oobj As Object) As Hashtable

The above function must work with hashtables and return a hashtable.

But when I do:

htpagevalues = SaveHashItem (htpagevalues, osomeobject)

I get the errors.  I'm not sure whether I should be CTyping the values on the call to the function?  Or should I change the function stubs (either way is a LOT of stupid work!) to work with IDictionary and CTYpe to hashtable inside the functions?  Or is there a better way?

Does that make any more sense?
SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No, can't do that...or I already would've. ;-)  The engine I built uses multiple types of IDictionary items.  It just so happens in this case I am working with hashtables....

And yes, if I wanted to create a maintenance nightmare, I could just use a sub...which would not make it *clear* to another programmer from looking at the code that it is changed inside a function.  It's why I prefer C#, but this is what we are working with, so...

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, here's part of the problem with that (and I think you *meant* 'Protected Function SaveHashItem (ByRef htpage As IDictionary, ByVal oobj As Object) As IDICTIONARY') ).

I also have an arraylist (another IDictionary object), that contains a list of IDictionary items that are Hashtables.

So, when I try to keep all items of type IDictionary (which I agree would be the best way to do this) *outside* functions that specifically do Hashtable work, I still have issues like this:

Well, hold on.  I think I can do exactly that.

I am going to award points (and an A!) for the timeliness and feedback to you Carl.  Thank you so much for your help!
And also Chaosian for helping out too.  Thanks you both very much.  Hope you do not mind sharing...
I hate sharing... just give Carl all the points :)

Seriously, though... glad we could help.