Question Basic generics question

ergolargo

New member
Joined
May 28, 2010
Messages
1
Programming Experience
1-3
Hi there

Can anybody point me in the right direction with a question that has been annoying me - in the example code below, would it be possible to write it without the repitition of the If TypeOf ... ElseIf TypeOf condition, thereby using a single condition to process the ImageUrl for any number of Image and ImageButton references - in this example, the references are in a hashtable with multiple dictionary entries of either control type - i.e., how would it be possible to handle more than one type of control in the same routine. I have a feeling this might involve generics, but is this even possible if I have to use early binding?

Here is just a simplified example, for which I would like to remove the If TypeOf ... ElseIf TypeOf condition :

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

Dim absimagepath As String = [expression to determine an absolute path]
Dim imagecontrols As Hashtable = New Hashtable()
Dim imagefilenames As Hashtable = NewHashtable()
....
[processing to create 'imagecontrols' hashtable from references to various Image and Imagebutton controls]
[processing to create 'imagefilenames' hashtable from various pathnames]
....
VB.NET:
For Each de As DictionaryEntry In imagecontrols

     Dim sb As StringBuilder = New StringBuilder(absimagepath)
     sb.Append("\")
     sb.Append(imagefilenames(de.Key))

     If TypeOf de.Value Is ImageButton Then
          Dim ctrl As ImageButton = _
           DirectCast(imagecontrols.Item(de.Key), ImageButton)
          ctrl.ImageUrl = sb.ToString()
     ElseIf TypeOf de.Value Is Image Then
          Dim ctrl As Image = _
           DirectCast(imagecontrols.Item(de.Key), Image)
          ctrl.ImageUrl = sb.ToString()
     End If

Next
--------------------------------------------

It's just a simplified representation of my real code, but I'm sure there must be a more elegant way to populate the ImageUrl properties of the two different control types from two separate hashtables, which is what my real code is doing.

Thanks very much for any advice on this!

Regards
 
Last edited by a moderator:
You could write the code differently but it's not really going to be any shorter. You could use TryCast but then you have to test for Not Nothing anyway, so you still end up with conditional statements.

If you were using .NET 3.5 or later then you would have LINQ at your disposal and you could use the OfType method, which would filter a collection and only return the items of a particular type. That wouldn't really help you though, because all your items are DictionaryEntry objects. You would have to use the Where method to filter, which would require you to use a lambda expression that included using TypeOf anyway. It would be a bit more elegant than your current solution but it's still not going to be a one-liner.
 
Since these controls appear to have a common property, ImageUrl, could (or should) there be some inheritance relationship? In that case you can cast to base type that have the common property. If not the best idea is perhaps to implement a common Interface and use that type as cast. If for some reason neither is possible that leaves Reflection as an option.

Related to this, Hashtable is a Dictionary(Of Object, Object), you should use the Dictionary type regardless, and the inheritance/interface relationship can be used to narrow down the allowed types. In this case a cast may not be needed at all.
 
Back
Top