I want to expose a property of Dictionary<stri ng, MyAbstractClass >.
I tried to do it this way:
private Dictionary<stri ng, MyChildClass> _dictionary;
public Dictionary<stri ng, MyAbstractClass >
{
get { return _dictionary; } //error: no implicit conversion.
}
How can I have a privay dictionay that uses an inheritied class, but
expose that dictionary as the base type?
--Brian 8 17762
No, and here is why.
Let's say that you could expose a dictionary of MyChildClass objects as
a dictionary of MyAbstractClass objects.
public class MyChildClass : MyAbstractClass { ... }
public class MyChildClass2 : MyAbstractClass { ... }
private Dictionary<stri ng, MyChildClass> _dictionary;
public Dictionary<stri ng, MyAbstractClass > Dict
{
get { return _dictionary; } //error: no implicit conversion.
}
Now, outside this class, you say
Dictionary<stri ng, MyAbstractClass > theDictionary = myObject.Dict;
theDictionary["Bruce"] = new MyChildClass2(. ..);
That last line seems perfectly legal to the compiler, but would have to
fail at runtime, because the actual dictionary returned by
myObject.Dict is not a dictionary of "anything that inherits from
MyAbstractClass ". It is, rather a dictionary of a specific subclass of
MyAbstractClass , called MyChildClass.
By allowing the return of a dictionary of a class farther up the
hierarchy, you've lost compile-time type safety, which is what generics
are trying to give you in the first place.
Brian P <no@email.com > wrote: I want to expose a property of Dictionary<stri ng, MyAbstractClass >.
I tried to do it this way:
private Dictionary<stri ng, MyChildClass> _dictionary;
public Dictionary<stri ng, MyAbstractClass > { get { return _dictionary; } //error: no implicit conversion. }
How can I have a privay dictionay that uses an inheritied class, but expose that dictionary as the base type?
You can't. There's no covariance like that on generic types. If you
could do that, consider the following code:
Dictionary<stri ng, MyAbstractClass > foo = YourProperty;
MyOtherChildCla ss bar = new MyOtherChildCla ss();
foo["hello"] = bar;
Now, the actual dictionary is only meant to contain MyChildClass values
- but I've just broken that!
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: You can't. There's no covariance like that on generic types. If you could do that, consider the following code:
Dictionary<stri ng, MyAbstractClass > foo = YourProperty; MyOtherChildCla ss bar = new MyOtherChildCla ss(); foo["hello"] = bar;
Now, the actual dictionary is only meant to contain MyChildClass values - but I've just broken that!
I must be doing something wrong in my design / thinking.
To the outside world, I want the Dictionary<stri ng, MyAbstractClass > to
be "read-only". So, I guess I'm not worried about the problem you point
out.
But, I realize that I'm surely doing something poor:
I have an interface that several classes implement. The interface
requires Dictionary <string, MyAbstractClass >. But the classes that
implement that interface, their interal workings are going to use
ChildClasses of MyAbstractClass . And so long as the only usage of
ChildClasses are interally, I can't see how exposing the dictionary as
MyAbstractClass is "bad".
--Brian
Jon Skeet [C# MVP] wrote: You can't. There's no covariance like that on generic types. If you could do that, consider the following code:
Dictionary<stri ng, MyAbstractClass > foo = YourProperty; MyOtherChildCla ss bar = new MyOtherChildCla ss(); foo["hello"] = bar;
Now, the actual dictionary is only meant to contain MyChildClass values - but I've just broken that!
I must be doing something wrong in my design / thinking.
To the outside world, I want the Dictionary<stri ng, MyAbstractClass > to
be "read-only". So, I guess I'm not worried about the problem you point
out.
But, I realize that I'm surely doing something poor:
I have an interface that several classes implement. The interface
requires Dictionary <string, MyAbstractClass >. But the classes that
implement that interface, their interal workings are going to use
ChildClasses of MyAbstractClass . And so long as the only usage of
ChildClasses are interally, I can't see how exposing the dictionary as
MyAbstractClass is "bad".
--Brian
Brian P <no@email.com > wrote: Now, the actual dictionary is only meant to contain MyChildClass values - but I've just broken that! I must be doing something wrong in my design / thinking.
To the outside world, I want the Dictionary<stri ng, MyAbstractClass > to be "read-only". So, I guess I'm not worried about the problem you point out.
Okay - but there's nothing to stop clients from *trying* to do that.
You're not exposing it in a read-only way (not that I can see any read-
only interfaces that are easily exposed in the generic collections
provided by the framework...)
But, I realize that I'm surely doing something poor:
I have an interface that several classes implement. The interface requires Dictionary <string, MyAbstractClass >. But the classes that implement that interface, their interal workings are going to use ChildClasses of MyAbstractClass . And so long as the only usage of ChildClasses are interally, I can't see how exposing the dictionary as MyAbstractClass is "bad".
I entirely understand where you're coming from, but I can't immediately
think of any way of doing it with generics.
Do the internal workings definitely need to know they they'll be using
ChildClass, other than when they're putting the entries in?
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Brian P <no@email.com > wrote:
<snip> I must be doing something wrong in my design / thinking.
To the outside world, I want the Dictionary<stri ng, MyAbstractClass > to be "read-only". So, I guess I'm not worried about the problem you point out.
I've thought of an alternative. Rather than exposing a whole
Dictionary<stri ng,MyAbstractCl ass> would it be possible to expose just
a property:
public MyAbstractClass this[string key];
? You could easily implement that in your class, just by returning
_dictionary[key]; and you'd be guaranteed that it would be exposing it
in a read-only way.
Alternatively, you could have:
class ReadOnlyDiction ary<K,V1,V2> : IDictionary<K,V 1>
where V2 : V1
{
Dictionary<K,V2 > dictionary;
public V1 this [K key]
{
get { return dictionary[key]; }
set { throw new UnsupportedOper ationException( ); }
}
// etc - implement the rest of the appropriate IDictionary
// properties
}
You could then use:
public IDictionary<str ing, MyAbstractClass >
{
get { return new
ReadOnlyDiction ary<string, MyAbstractClass , ChildClass>
(dictionary);
}
I *think* that would work - and would actually be quite neat (and very
reusable). You could do the same for List if you ever needed to.
My guess is that *someone* has actually already done this and published
it as open source... I can't be the first to think of it.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: I entirely understand where you're coming from, but I can't immediately think of any way of doing it with generics.
=) so I'm not completey dumb. Do the internal workings definitely need to know they they'll be using ChildClass, other than when they're putting the entries in?
Yes, they do. So, for now, I have a private dictionary of <string,
MyAbstractClass > and in the internal workings, I cast to MyChildClass:
((MyChildClass) _myDictionary["foo"]).ChildSpecific PropertyOrMetho d
This lets me expose the dictionary as MyAbstractClass but I still can
"work" with the ChildClass internally. Though, I admit this doesn't
seem ideal.
--Brian
Jon Skeet [C# MVP] wrote: I've thought of an alternative. Rather than exposing a whole Dictionary<stri ng,MyAbstractCl ass> would it be possible to expose just a property:
public MyAbstractClass this[string key];
? You could easily implement that in your class, just by returning _dictionary[key]; and you'd be guaranteed that it would be exposing it in a read-only way.
I *think* that would work - and would actually be quite neat (and very reusable). You could do the same for List if you ever needed to.
My guess is that *someone* has actually already done this and published it as open source... I can't be the first to think of it.
Yes! This sounds perfect! You are awesome ... I see you all over these
newsgroups and I always read your posts. Most of the time you are
talking about things over my head, but I usually learn something new
from you.
Thanks again,
--Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ESPNSTI |
last post by:
Hi,
I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable<> to find the key.
In the example below, accessing the dictionary by using the exact key object
that was used to add to the dictionary works.
(see code comment 1).
However, if I attempt to access the dictionary by using a key object that
|
by: Eran |
last post by:
Hi,
I have a huge data structure, which I previosly stored in a
Dictionary<int, MyObj>
MyObj is relatively small (2 int, 1 DateTime, 1 bool).
The dictionary I am using is quite large (25,000), and I have 500 such
dictionaries.
What I've noticed is that the total memory consumed became over 1 GB.
When I changed the implementation to List<MyObj>, or SortedList<int,
|
by: Andrew Robinson |
last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a...
|
by: Peter K |
last post by:
Hi
are there any benefits in using StringDictionary over Dictionary<string,
string?
It appears they achieve the same thing... (I could be wrong of course).
thanks,
Peter
|
by: Mark S. |
last post by:
Hello,
I have a series of changing string IDs that are loaded dynamically a couple
times a minute. I need to associate each ID with a different static class so
later on in the app's lifecycle it knows which static class to use for
processing.
I've tried:
static class MyStaticObject
{
| |
by: Peter Larsen [CPH] |
last post by:
Hi,
How do i concat two dictionaries aka the following sample:
Dictionary<int, stringa;
Dictionary<int, stringb;
b.Add(a);
What is the easiest way to concat two dictionaries ??
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |