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]
....
--------------------------------------------
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
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: