Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inheriting from Hashtable

Posted on 2013-11-06
5
Medium Priority
?
264 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 2000 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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
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.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

916 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