Groups | Blog | Home
all groups > vb.net upgrade > march 2006 >

vb.net upgrade : Dynamic casting of Listbox and Combobox


Stanley Mok
3/8/2006 10:26:00 AM
In VB6, I can do something like:

Private Sub (c as object) ' c can be a listbox or a combo box
c.additem "aaa"
End Sub

Stanley Mok
3/8/2006 3:23:27 PM
[quoted text, click to view]

Interesting, both Items and AddItem are not a member of ListControl, what
can I do now? I am trying to populate the Listbox/ComboBox from a text file.
Herfried K. Wagner [MVP]
3/8/2006 8:33:16 PM
"Stanley Mok" <Stanley Mok@discussions.microsoft.com> schrieb:
[quoted text, click to view]

\\\
Private Sub Bla(ByVal c As ListControl)
c.Items.Add(...)
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
Herfried K. Wagner [MVP]
3/9/2006 12:45:03 AM
"Stanley Mok" <StanleyMok@discussions.microsoft.com> schrieb:
[quoted text, click to view]

Sorry, I have overlooked that.

\\\
Private Sub Bla(ByVal c As ListControl)
If TypeOf c Is ListBox Then
DirectCast(c, ListBox).Items.Add(...)
ElseIf TypeOf c Is ComboBox Then
DirectCast(c, ComboBox).Items.Add(...)
Else
Throw New ArgumentException(...)
End If
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Stanley Mok
3/9/2006 8:20:31 AM
[quoted text, click to view]

This is exactly what I try to avoid since the code for both the TypeOf test
are the same, it would be better to maintenance one section of code than two
Stanley Mok
3/9/2006 9:53:27 PM
[quoted text, click to view]

Thanks for the answer. I didn't expect this would be so complicated in
vb.net, I think I will use the If-else construct or use separate Subs.
Herfried K. Wagner [MVP]
3/10/2006 12:16:00 AM
"Stanley Mok" <StanleyMok@discussions.microsoft.com> schrieb:
[quoted text, click to view]

There are other ways (late binding, 'CallByName', reflection), but I doubt
they are better.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
AddThis Social Bookmark Button