473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Generics : breaking encapsulation?

Consider the following code:
>>>>>>>

// Define an empty class
public class ZorgleCollection : Dictionary<string, Zorgle>
{
}

// Somewhere outside ZorgleCollection:

// Print all the elements in this collection.
public void PrintAllMembers(ZorgleCollection collection)
{
// Method 1 -- foreach.
// Can't fill in the blank without specifying the types of the
// template parameters. But this breaks encapsulation!

foreach( ____(type name)____ entry in collection)
{
Console.WriteLine("Found entry: {0}", entry.Value);
}

// Method 2 -- use an enumerator.
// Still can't fill in the blank without specifying the types of
// the template parameters -- which also breaks encapsulation!

____(type name)____ enumerator = collection.GetEnumerator();

while (enumerator.MoveNext())
{
Console.WriteLine("Found entry: {0}", entry.Value);
}
}

// ...
<<<<<<<<<<<

We can't use an enumerator without first knowing the template's type
parameters. By extension, since foreach is really just syntactic sugar
around enumerators, we can't use foreach either. How can I walk through the
collection without specifying the type, which breaks encapsulation and
creates dependencies? Or is this a doomed effort?

Nov 16 '05 #1
3 1775
You should be able to use:
IEnumerator enumerator = collection.GetEnumerator();

You could also use contraints to specify an interface that Zorgle must
support.

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:uZ****************@TK2MSFTNGP10.phx.gbl...
Consider the following code:
>>>>>>>>

// Define an empty class
public class ZorgleCollection : Dictionary<string, Zorgle>
{
}

// Somewhere outside ZorgleCollection:

// Print all the elements in this collection.
public void PrintAllMembers(ZorgleCollection collection)
{
// Method 1 -- foreach.
// Can't fill in the blank without specifying the types of the
// template parameters. But this breaks encapsulation!

foreach( ____(type name)____ entry in collection)
{
Console.WriteLine("Found entry: {0}", entry.Value);
}

// Method 2 -- use an enumerator.
// Still can't fill in the blank without specifying the types of
// the template parameters -- which also breaks encapsulation!

____(type name)____ enumerator = collection.GetEnumerator();

while (enumerator.MoveNext())
{
Console.WriteLine("Found entry: {0}", entry.Value);
}
}

// ...
<<<<<<<<<<<

We can't use an enumerator without first knowing the template's type
parameters. By extension, since foreach is really just syntactic sugar
around enumerators, we can't use foreach either. How can I walk through
the
collection without specifying the type, which breaks encapsulation and
creates dependencies? Or is this a doomed effort?

Nov 16 '05 #2
I don't see what the issue is though. at this point you already know your
ZorgleCollection is a dictionary of string, Zorgle pair. it's no longer
generic.

"K.K." wrote:
Consider the following code:
>>>>>>>>

// Define an empty class
public class ZorgleCollection : Dictionary<string, Zorgle>
{
}

// Somewhere outside ZorgleCollection:

// Print all the elements in this collection.
public void PrintAllMembers(ZorgleCollection collection)
{
// Method 1 -- foreach.
// Can't fill in the blank without specifying the types of the
// template parameters. But this breaks encapsulation!

foreach( ____(type name)____ entry in collection)
{
Console.WriteLine("Found entry: {0}", entry.Value);
}

// Method 2 -- use an enumerator.
// Still can't fill in the blank without specifying the types of
// the template parameters -- which also breaks encapsulation!

____(type name)____ enumerator = collection.GetEnumerator();

while (enumerator.MoveNext())
{
Console.WriteLine("Found entry: {0}", entry.Value);
}
}

// ...
<<<<<<<<<<<

We can't use an enumerator without first knowing the template's type
parameters. By extension, since foreach is really just syntactic sugar
around enumerators, we can't use foreach either. How can I walk through the
collection without specifying the type, which breaks encapsulation and
creates dependencies? Or is this a doomed effort?

Nov 16 '05 #3
Let's try a simpler class:

public class ZorgleCollection : List<Zorgle>
{
}

Now print all members becomes:
public void PrintAllMembers(ZorgleCollection collection)
{
foreach( Zorgle entry in collection)
{
Console.WriteLine("Found entry: {0}", entry);
}
}
//-----------------------------------------------

Now let's try something about as complex as your original:
class ZorgleEntry
{
public string key;
public Zogle value;
}

class ZorgleCollection : List<ZorgleEntry>
{
}

public void PrintAllMembers(ZorgleCollection collection)
{
foreach( ZorgleEntry entry in collection)
{
Console.WriteLine("Found entry: {0}", entry.value);
}
}

All this is just to show that your problem has nothing to do with either
generics or encapsulation. The problem is that you are using an class which
you don't have a name for (specifically the type of the entry stored in a
ZorgleCollection)

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:uZ****************@TK2MSFTNGP10.phx.gbl...
Consider the following code:
>>>>>>>>
// Define an empty class
public class ZorgleCollection : Dictionary<string, Zorgle>
{
}

// Somewhere outside ZorgleCollection:

// Print all the elements in this collection.
public void PrintAllMembers(ZorgleCollection collection)
{
// Method 1 -- foreach.
// Can't fill in the blank without specifying the types of the
// template parameters. But this breaks encapsulation!

foreach( ____(type name)____ entry in collection)
{
Console.WriteLine("Found entry: {0}", entry.Value);
}

// Method 2 -- use an enumerator.
// Still can't fill in the blank without specifying the types of
// the template parameters -- which also breaks encapsulation!

____(type name)____ enumerator = collection.GetEnumerator();

while (enumerator.MoveNext())
{
Console.WriteLine("Found entry: {0}", entry.Value);
}
}

// ...
<<<<<<<<<<<

We can't use an enumerator without first knowing the template's type
parameters. By extension, since foreach is really just syntactic sugar
around enumerators, we can't use foreach either. How can I walk through

the collection without specifying the type, which breaks encapsulation and
creates dependencies? Or is this a doomed effort?

Nov 16 '05 #4

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

Similar topics

11
3969
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
3
41509
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
47
3290
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
19
1513
by: Barry Mossman | last post by:
Hi, if I have a generic class such as: public class MyGroup<T> : Collection<MyChildClass> and from one of it's methods I want to pass a reference to myself to the following method in another...
8
3157
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
5
1913
by: Stefano Peduzzi | last post by:
Hi, I'm building an application where I've defined a custom class Customer. Customer can have many phones (defined by phoneType and phoneNumber). I want to check that a phoneNumber is not already...
0
1446
by: Eliteisv | last post by:
I may be way off here but I like to philologize anyway. Recently I've been learning about Java Generics. Then I got to thinking about how object databases haven't cought on. Obviously one...
7
1279
by: Andy Bell | last post by:
Can this be done via .net generics? How? The % signs below are just to show how I want to do it, I realise they're not valid syntax. public abstract class BaseSelectionRequirement { ......
2
7612
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
0
7124
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
7326
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
7385
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...
1
7046
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...
0
7498
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...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.