Solved

Inheriting from Hashtable

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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