Solved

Inheriting from Hashtable

Posted on 2013-11-06
5
248 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
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
It should have been an integer. The get/set you provided did the trick. Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now