Solved

Inheriting from Hashtable

Posted on 2013-11-06
5
260 Views
Last Modified: 2013-11-06
I want to create a derived class SensingNodes from Hashtable for which the value is the custom type InitialTimePair. In my program I want to write
            SensingNodes sn = new SensingNodes();
            // ... code to add values
            InitialTimePair itp = sn[3];

Open in new window

When this happens I get the error "Cannot implicitly convert type 'object' to 'InitialTimePair'. An explicit conversion exists (are you missing a cast?)"

Here's my code for SensingNodes:
        internal class SensingNodes : Hashtable {
            internal void Add(int _nodeID, InitialTimePair _initialTimePair) {
                base.Add(_nodeID, _initialTimePair);
            }
            internal bool Contains(int _nodeID) {
                return base.Contains(_nodeID);
            }
            internal void Remove(int _nodeID) {
                base.Remove(_nodeID);
            }
            internal InitialTimePair this[InitialTimePair key] { get; set; }
        }

Open in new window

I had thought that line 11 might give this to me but it didn't. How can I fix it?

BTW, I'm using the Micro Framework version of C#, so my options for built-in collections are pretty limited.
0
Comment
Question by:BlearyEye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 39627626
Well, you didn't give us your InitialTimePair class implementation. Regardless, did you try simply casting the parameter in the call?

base.Add(_nodeID, (object)_initialTimePair);

Open in new window


I put something like this in my .NET 4.0 Visual Studio environment and didn't get any errors. Could be Micro Framework being a problem? Don't know.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 39627823
Here's the InitialTimePair class:
        internal class InitialTimePair {
            internal long baseTime { get; private set; }
            internal long sensingNodeTime { get; private set; }
            internal InitialTimePair(long _baseTime, long _sensingTime) {
                baseTime = _baseTime;
                sensingNodeTime = _sensingTime;
            }
        }

Open in new window

However, my problem isn't in the Add method (I don't think) but rather in the access
InitialTimePair itp = sn[3];

Open in new window

That's where I get the compiler error. If I do
InitialTimePair itp = (InitialTimePair)sn[3];

Open in new window

then there's no problem; but I want to avoid doing the cast by having my SensingNodes class just return the right type.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 39627831
You're passing an integer to the indexer, but you are showing the indexer as taking a InitialTimePair instance as the key. Which is correct?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39627863
Further, you're calling the base implementation of the index operator. This is defined to take an object as a parameter, and it returns an object as well, hence your error. You will need to define an overload for what you want to return; perform the casting within that overload:

e.g.

internal class SensingNodes : Hashtable
{
    internal void Add(int _nodeID, InitialTimePair _initialTimePair)
    {
        base.Add(_nodeID, _initialTimePair);
    }
    internal bool Contains(int _nodeID)
    {
        return base.Contains(_nodeID);
    }
    internal void Remove(int _nodeID)
    {
        base.Remove(_nodeID);
    }

    internal InitialTimePair this[int key]
    {
        get
        {
            return (InitialTimePair)base[key];
        }
        set
        {
            base[key] = value;
        }
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:BlearyEye
ID: 39628387
It should have been an integer. The get/set you provided did the trick. Thanks.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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