Solved

Inheriting from Hashtable

Posted on 2013-11-06
5
254 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

947 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

24 Experts available now in Live!

Get 1:1 Help Now