Q1. Why should one go for this implementation where
viewmember, datamember and datasource can easily solve the issue?
Ans: Yes, viewmember, datamember and
datasource could have solved this issue in an easier way if .Net would be bug
free on this aspect. We can use viewmember, datamember and datasource in the
following manner.
Listing 4
With cmbEmployee
.ValueMember = ""
.DisplayMember = "asdasd"
.DataSource = ""
End With
However, there is one flaw in this scenario. While
assigning the datasource to the combo box, the comboBoxName_
SelectedIndexChanged() gets fired. That means the code written in this section
will be executed again and again. (You can test this by putting a Message Box
inside comboBoxName_ SelectedIndexChanged.)
Q2. Is this the only way for achieving the mentioned goal?
Ans: There might be other ways, but the
idea behind all the logic is the same. Basically, the logic behind all of it is
that we are creating objects for each entry in the combo box and each object
has a pair (Data and Description).