By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,908 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,908 IT Pros & Developers. It's quick & easy.

C# Generics : breaking encapsulation?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.