Solved

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'

Posted on 2006-07-21
11
231 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:louisiana_blues
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:MacNuttin
ID: 17155015
turn option strict off
0
 

Author Comment

by:louisiana_blues
ID: 17155034
I would rather not do this.  It brings into play all other kinds of potentially sloppy programming tactics.  Is there a better solution?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17155100
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17155170
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?
0
 

Author Comment

by:louisiana_blues
ID: 17155205
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?
0
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 150 total points
ID: 17155252
What about declaring your variable as a hashtable in the first place?
Dim htpagevalues as New Hashtable()

The cast it is having issues with is a cast from IDictionary -> HashTable (in the function parameter).

As another note, if you're passing ByRef, you could just use a Sub, no?
Protected Sub SaveHashItem (ByRef htpage As Hashtable, ByVal oobj As Object)

SaveHashItem (htpagevalues, osomeobject)
0
 

Author Comment

by:louisiana_blues
ID: 17155289
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...

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 350 total points
ID: 17155334
Can't you just declare the parameter as IDictionary rather than HashTable, and do the cast inside the routine:

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

Author Comment

by:louisiana_blues
ID: 17155488
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!
0
 

Author Comment

by:louisiana_blues
ID: 17155497
And also Chaosian for helping out too.  Thanks you both very much.  Hope you do not mind sharing...
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17155521
I hate sharing... just give Carl all the points :)

Seriously, though... glad we could help.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 62
VB.NET (2008) - Refactoring Question 2 23
Footer for each row on Gridview 2 32
vb.net 1 month apart 11 35
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question