Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Today I coded my first dictionary class, a ReadOnlyDictionary(TKey, TValue) which inherited from IDictionary(TKey, TValue). I implemented all the methods he asked for, all the properties that are needed and when I was done it all compiled nicely. (I followed the instructions from Blackwasp ReadOnlyDictionary[^].)

And then I wanted to use it... But all methods and properties show up as private and thus I cannot access them.

A little example from my dictionary code:
C#
bool ICollection<keyvaluepair><tkey,>>.Contains(KeyValuePair<tkey,> item)
{
    return _dictionary.Contains(item);
}
Posted
Updated 31-Aug-11 22:55pm
v2

If you would have followed that link, it wouldn't be a problem.
It has all the methods with public modifier.
C#
public bool Contains(KeyValuePair<TKey, TValue> item)
{
    return _dictionary.Contains(item);
}

You have not mentioned it in your method.
 
Share this answer
 
Comments
KenBonny 1-Sep-11 5:12am    
I see now what I did wrong. And I learned something here, let me share that with you:

If you implement an interface, apparently you can implement it two ways.

The first is like Prerak wrote, this will give you the function as a publicly available method.
The other way is like my way. If you use it without the public but with a reference to ICollection, then it is implemented as a private method and is hidden from the outside world. This way you can implement a part of an interface without getting compiler errors.

I let ReSharper generate the method signatures for me, and it implemented them as I did in my question. My very attentive mind skipped the part of the signatures and left them as private implementations.
Simon Bang Terkildsen 1-Sep-11 11:47am    
No it is NOT private even when you implement the method explicitly. If you cast your object to IDictionary<tkey,> then you can use that method anywhere thus the method is NOT private.
The default scope of a Class, Property, Field, Method is private, so you'll need to tell the compiler that the method is public by writing public before your method declaration e.g. public void MyMethod{ }

If you have implemented the .NET interface IDictionary<TKey, TValue>[^] then your code wont even compile if the methods and properties aren't public as the interface is public and thus so must the methods and properties.
 
Share this answer
 
v2
You are implementing an interface, so you need to implement all the methods.

Creating an instance of the custom dictionary should give you access to the public methods.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900