Posts tagged: vb.net

Jun 23 2009

How to fake a TreeNodeCollection subclass in .NET

If you’ve ever had reason to try to extend the standard Microsoft web TreeView control, you will have no doubt noticed that MS was quite unkind to you and sealed (or declared NotInheritable for you VB.NET types) the System.Web.UI.WebControls.TreeNodeCollection class.

The problem arises when you want to overload the default behavior that is implemented by the TreeNodeCollection class. For example, when a node was added to my TreeView class (via the TreeView.Nodes.add method), I needed to be able to analyze it for the ultimate purpose of my subclass.

However, this was not possible because the TreeNodeCollection class is sealed, so I wasn’t able to inherit from it and overload the add method behavior as I should have been able to do.

There are always possibilities.

Anyone who knows me knows I don’t give up easily (if ever), and I eventually plowed through many false starts but hit upon a solution.

I decided to wrap the underlying TreeNodeCollection class with my own, and overload the Nodes property on the TreeView class, and the ChildNodes property on the TreeNode class.

The Wrapper.

Here’s what the wrapper looks like:
[vb language=".net"]

Public Class MyTreeNodeCollection

Private mtvwChildNodes As System.Web.UI.WebControls.TreeNodeCollection
Private mMyTreeViewOwner As IMyNodeContainer

Public Sub New(ByVal Owner As IMyNodeContainer, _
ByVal TreeViewChildren As System.Web.UI.WebControls.TreeNodeCollection)
mtvwChildNodes = TreeViewChildren
mMyTreeViewOwner = Owner
End Sub

Public Sub Add(ByVal child As MyTreeNode)
mtvwChildNodes.Add(CType(child, System.Web.UI.WebControls.TreeNode))
mMyTreeViewOwner.RegisterNodeForLookup(child)
End Sub
End Class
[/vb]

As you can see, the constructor takes the real, underlying TreeNodeCollection to pass all node to prior to being “registered” (analyzed). It also takes something that implements the IMyNodeContainer interface.

The IMyNodeContainer interface.

I had to implement this because I wanted to be able to use this wrapper class for both TreeView and TreeNode objects, but the signature of their properties is different - TreeView.Nodes and TreeNode.ChildNodes, respectively. So I opted for the interface to keep things clean.

Here’s what the interface looks like:

[vb language=".net"]
Public Interface IMyNodeContainer

Sub RegisterNodeForLookup(ByVal node As MyTreeNode)
ReadOnly Property Nodes() As MyTreeNodeCollection

End Interface
[/vb]

Wrapping things up.

This is where the magic happens.

To put the wrapper (MyTreeNodeCollection) in place, I overload the collection getting properties in the base class - TreeView.Nodes and TreeNode.ChildNodes, respectively, like so:

[vb language=".net"]
Public Overloads ReadOnly Property Nodes() As MyTreeNodeCollection Implements IMyNodeContainer.Nodes
Get
Return mNodeCollection
End Get
End Property
[/vb]

So, for those of you playing along at home, when the developer calls .Nodes on an instance of MyTreeView class, an instance of MyTreeNodeCollection is returned and the resulting add method in performed on the underlying TreeNodeCollection, thusly:

[vb language=".net"]
Public Sub Add(ByVal child As MyTreeNode)
mtvwChildNodes.Add(CType(child, System.Web.UI.WebControls.TreeNode))
mMyTreeViewOwner.RegisterNodeForLookup(child)
End Sub
[/vb]

You can see why I had to use the IMyNodeContainer interface here. The RegisterNodeForLookup performs the functionality I was originally trying to subclass the TreeNodeCollection for.

Blog Traffic Exchange Related Posts
Jun 09 2009

Arraylist and generics don’t mix with IEnumerable(Of T).GetEnumerator.

The other day I was writing an in-house tool to assist in some upgrades we were performing on client installations. This tool was supposed to perform its operations on a batch of items, and display the results upon completion.

Since processing this batch of items was a lengthy endeavor, I wanted the failure to process one of the items to simply be recorded and allow the processing of the others to continue. Part of the processing of each item was a call to multiple web services, so I would need a way to handle the collection of errors along the way and make them available for their eventual display.

I had what I thought was a clever idea: a private Arraylist of exceptions that occurred during processing.

[vbnet]
Public Class BatchExceptions
Implements ICollection(Of System.Exception)

Private mExceptionlist As ArrayList
.
.
.
End Class
[/vbnet]

That way, I could simply iterate over the list and perform the standard exception handling, like so:

[vbnet]
Dim exc As Exception
For Each exc In BatchExceptions
HandleError(exc)
Next
[/vbnet]

Of course, in order to make use of the “For Each” construct, I had to implement the GetEnumerator of the ICollection interface.

[vbnet]
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Exception) _
Implements System.Collections.Generic.IEnumerable(Of System.Exception).GetEnumerator
Return (mExceptionlist.GetEnumerator)
End Function
[/vbnet]

Cool. Only one problem:

arraylist-and-generics-dont-mix-when-enumerating-generics_generics-enumerator

That was OK though, because I was using generics after all. The compiler was being helpful and reminding me that I had to specify ‘IEnumerator(Of Exception)’:

arraylist-and-generics-dont-mix-when-enumerating-generics_generics-enumerator2

[vbnet]
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Exception) _
Implements System.Collections.Generic.IEnumerable(Of System.Exception).GetEnumerator
Return (DirectCast(mExceptionlist.GetEnumerator, IEnumerator(Of Exception)))
End Function
[/vbnet]

Everything compiled fine, but at run-time I got the following RTE:

Unable to cast object of type ‘ArrayListEnumeratorSimple’ to type ‘System.Collections.Generic.IEnumerator`1[System.Exception]‘.

arraylist-and-generics-dont-mix-when-enumerating-generics_generics-enumerator-rte

This was frustrating. It seemed like the compiler failed to warn me of this incompatibility, and simply kicked the can on down the road to the run-time to deal with.

Solution:

Well, I did a little poking around and finally ended up replacing my Arraylist with a list like so:

[vbnet]
private mExceptionlist as System.Collections.Generic.list
[/vbnet]

Well, that did the trick but I can’t say it was as intuitive as it would seem. It makes sense, in hindsight, but why did I have to get some cryptic RTE? Why couldn’t the compiler have picked up on my use of an ArrayList and say, “Hey dummy - use a generic list!”? Still, I have a new trick to toss in my bag for the time I want to implement an enumerator on an Arraylist!

Blog Traffic Exchange Related Posts Blog Traffic Exchange Related Websites
  • Israel eases Gaza import limits [/caption] JERUSALEM — Israel on Monday announced a major change in the way it will manage the country's controversial blockade of the Gaza Strip , a move Israeli officials hope will ease tensions with the Obama administration on the eve of a visit to Washington by Prime Minister Benjamin Netanyahu......
  • Organizing Collectible Memorabilia The first step when you are dealing with collectible memorabilia is to figure out how you want to collect, organize, store, display and preserve your memorabilia. What keepsakes are you intending to hang onto for the long haul, and which memorabilia keepsakes would you rather sell or give up? Once......
  • How Lacquered Door The process of varnishing a door is to do a little maintenance at the door by creating a smoother surface, perfect and smooth while giving protection to the wood against water and other harmful agents. The process is long, but is not complicated, and I've made a video in Spanish......
  • Things To Do Today: Make Ta-da Lists I've been making to-do lists over the years on scraps of paper, on backs of envelopes, on shopping receipts, on those free insurance and real estate notepads you get in the mail all the time, etc. Sticking them on the refrigerator, under my car keys, in my purse, wrapped around......
  • Free Success Ebook: “How to Turn Your Desires and Ideals Into Reality” by Brown Landone – Free Download! FIRST, AN ideal to come true must be an ideal; an idea will not do.  Second, an ideal to become a reality must have a heart of desire, – and a good strong heart.  Third, an ideal to come into manifestation must be a body of real etheric substance. ......