Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > IEnumerable
 

IEnumerable


I always implemented an IEnumerable object like this:

-----------------------

Class CollectionItem
.
.
.
End Class


Class Collection : Implements IEnumerable
Private InternalCollectionContent as Whatever(of CollectionItem)
.
.
.
Private Class CollectionEnumerator : Implements IEnumerator
Private InternalCollectionContent as Whatever(of CollectionItem)
.
.
.
End Class
End Class

------------------------

And it works just perfect !


But I noticed that on MSDN there is an exemple that is made to demonstrate how
to do the implementation. It goes like this:

-----------------------

Public Class CollectionItem
.
.
.
End Class


Public Class Collection : Implements IEnumerable
Private InternalCollectionContent as Whatever(of CollectionItem)
.
.
.
End Class

Public Class CollectionEnumerator : Implements IEnumerator
Public InternalCollectionContent as Whatever(of CollectionItem)
.
.
.
End Class

-----------------------

I do not understand why the Microsoft implementation method expose the IEnumerator class
as Public and I do not understand neither why the the internal content of the collection
is exposed (Public) from the IEnumerator class

What is the reason for this

Crazypennie  Tuesday, December 01, 2009 1:34 AM
CrazyPenny,

If you ask yourself why Microsoft did something, than start asking yourself why your collegue did something.

Not everything has a reason or is just because somebody forgot something.

Does it harm for you and where?

All those things are done by simple developers, they too do things they latter on don't understand why they did it like that, like I do as well.


Cor
Cor Ligthert  23 hours 54 minutes ago

When I read the code of somebody that is a better developer than I am, I always look at how he does thing. This is a very good source of knowledge.


But here you are right,MSDN code don't even compile. The compiler will not let you expose the content of a private variable from a public reference to it in an another class

------
you asked, Does it harm me how they did something ?

If I cant trust the way Microsoft tells you how to implement an interface, what good is this library for a developer?
Crazypennie  19 hours 30 minutes ago

Crazypennie,

Good call on finding a bug in the library.
Go to the page again, and use the "Report Bug" link.

As far as best coding practices in the general samples, there are disclaimers scattered around the library that tell you samples do not conform to the best or safest coding practices. I understand and agree with this position. The *intent* of the examples is demonstrate how a given type or method is designed to be used, and not confuse the learner with 'best practice' coding patterns. There is a separate section of the library dedicated to that topic.

Rudy =8^D

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  16 hours 22 minutes ago
Hi Crazypennie,

Which example are you talking about? It would be good if you could provide the link to it.

Private modifieraremeant to be defined inside another type, not as aindependant type itself.

but not sure what you meant.

Thanks anyway


Arjun Paudel
Arjun Paudel  15 hours 8 minutes ago


Arjun

I am talking about this

http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx


If you refer to my first post, You will see how I use to implement the IEnumerator class inside the IEnumerable one.

I Use to do this so the enumerator and the intern content of the collection never get exposed.

I will agree with you if you say that the reference to the enumerator exist only in the GetEnumerator function and is not anyway accessible to the rest of the application and the internal data stay innacessible. Still, the class itself is accessible. To me the class has no reason to be exposed.

Also, the way it is showed in the exemple, the Ienumerator class using a public variable to hold the private content of the Ienumerable class, this is not something that the compiler will accept


So there is a bug in the exemple, but now this does not tells me if my way to implement this is good, regardless to the fact that it works good.

We have to considere the fact that the Ienumerator class is mainly there for some "Under The Hood" processing.
Crazypennie  13 hours 55 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• retrieve the name of a "NEW" control
• Disable MDI childs maximize button.
• delete all record in a table
• Executing Macros from the command window
• Hello need help
• help me in vb?
• How to Read/Write from Excel File to Sql DataTable
• using visual basic in access
• Getting a structure from memory
• How to get the selected text box? (On screen keyboard)