473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Using a generic class as a property

Hi,

I have a class (say Company) which should expose a ReadOnlyCollection
property (say EmployeesCol) hosting userdefined types (Employee).

Since only the internals of the Company class should be allowed to
modify the EmployeesCol, I'd like to expose a read only collection as a
property, so that the users of my Company class can enumerate the
employes, but can't add or remove those.

I think this can be done using the ReadOnlyCollection<T> generic, but I
don't seem to understand the way of using this properly. Do I have to
derive from ReadOnlyCollection<T> or can I just use it in a property
declaration.

It would be nice if somebody could help me getting startet on this one.

Thanks in advance,

Matthias
Nov 16 '05 #1
2 1572
Matthias,

You can use the ReadOnlyCollection<T>. What I would do is keep a
List<T> (where T is Employee) internally in your Company class. When you
expose the ReadOnlyCollection, have it wrap the list you are holding
internally.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:O1****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class (say Company) which should expose a ReadOnlyCollection
property (say EmployeesCol) hosting userdefined types (Employee).

Since only the internals of the Company class should be allowed to modify
the EmployeesCol, I'd like to expose a read only collection as a property,
so that the users of my Company class can enumerate the employes, but
can't add or remove those.

I think this can be done using the ReadOnlyCollection<T> generic, but I
don't seem to understand the way of using this properly. Do I have to
derive from ReadOnlyCollection<T> or can I just use it in a property
declaration.

It would be nice if somebody could help me getting startet on this one.

Thanks in advance,

Matthias

Nov 16 '05 #2
Thanks Nicholas!

Ok, spend some time reading just about anything that deals with
generics, and acutally, they are pretty cool :). Got it done in no time.

Thanks for the fast response!

Matthias

Nicholas Paldino [.NET/C# MVP] wrote:
Matthias,

You can use the ReadOnlyCollection<T>. What I would do is keep a
List<T> (where T is Employee) internally in your Company class. When you
expose the ReadOnlyCollection, have it wrap the list you are holding
internally.

Hope this helps.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
1
by: Barry L. Camp | last post by:
Hi all, Wondering if someone can help with a nagging problem I am having using a GridView and an ObjectDataSource. I have a simple situation where I am trying to delete a row from a table, but...
11
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: ...
4
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
6
by: tshad | last post by:
I was looking at a page that showed how to set up a custom event and it seems to work ok. But I am not sure how I would use it. How would I subscribe to it. There is actual action (such as...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.