From: Just_a_fan on
In VB6, I could easily take the value from a combo box and make a
command with it, this: baudrate = cboBaud(listindex).

With the new dotted stuff in VB9, I can't seem to do that. Here's an
example of my problem.

I have a combo box with various baud rates in it. The user selects one
and I want to use it.

However, the command to use it is in the format:
AxMbaxp1.BaudRate() = MBAXPLib.enumBaud.B9600

The ".B9600" could also be just a ".5" which I could get from the
listindex (selectedindex I think it is now) from the combo box or I
could use the B9600 from the selecteditem.tostring but I cannot figure
out how to put either on the end of such a dotted format command.

What I need is

AxMbaxp1.BaudRate() = "MBAXPLib.enumBaud." & _
cboBaud.selecteditem.tostring

This means I have to have a Select Case and run the combo box index and
pick the right command thereby increasing code bloat about 10 times what
it should be.

In general, is there any way to build up a command and avoid using the
Select Case?

If anyone remembers "REXX", this could easily be done in that language.
You just make a variable contain 5 and then issue the command with the
variable at the end. The first part was fixed and the variable was
appended at the end. In fact, IBM Assembler macro works the same way.
You can just use a LCLC and build the command up. (Sorry for dredging up
history but it seems we are losing flexibility and gaining code bloat)

Mike Morrow
MMSA00E62537

From: Scott M. on
The thing is that in .NET the way code is compiled does not allow you to
create code from code in this way. You'll need to use a Case statement.


<Just_a_fan(a)home.net> wrote in message
news:njid64t72illhldq5j1t2tipn9kt9t2fgn(a)4ax.com...
> In VB6, I could easily take the value from a combo box and make a
> command with it, this: baudrate = cboBaud(listindex).
>
> With the new dotted stuff in VB9, I can't seem to do that. Here's an
> example of my problem.
>
> I have a combo box with various baud rates in it. The user selects one
> and I want to use it.
>
> However, the command to use it is in the format:
> AxMbaxp1.BaudRate() = MBAXPLib.enumBaud.B9600
>
> The ".B9600" could also be just a ".5" which I could get from the
> listindex (selectedindex I think it is now) from the combo box or I
> could use the B9600 from the selecteditem.tostring but I cannot figure
> out how to put either on the end of such a dotted format command.
>
> What I need is
>
> AxMbaxp1.BaudRate() = "MBAXPLib.enumBaud." & _
> cboBaud.selecteditem.tostring
>
> This means I have to have a Select Case and run the combo box index and
> pick the right command thereby increasing code bloat about 10 times what
> it should be.
>
> In general, is there any way to build up a command and avoid using the
> Select Case?
>
> If anyone remembers "REXX", this could easily be done in that language.
> You just make a variable contain 5 and then issue the command with the
> variable at the end. The first part was fixed and the variable was
> appended at the end. In fact, IBM Assembler macro works the same way.
> You can just use a LCLC and build the command up. (Sorry for dredging up
> history but it seems we are losing flexibility and gaining code bloat)
>
> Mike Morrow
> MMSA00E62537
>


From: Stephany Young on
The 'new dotted stuff in VB9', as you put it, is not new at at all. It has
been around since VB4 (at least).

The big question is, how is the ComboBox populated. In the abscence of
further information, I will assume that it is popluated with the strings,
"4800", "9600", "14400", etc.

MBAXPLib.enumBaud.B9600 is nothing more than an alias for the integral value
9600, therefore, all that is being assigned to AxMbaxp1.BaudRate is an
integer. The use of the enum merely gives you a clue as to what values are
acceptable.

Also note that you are assigning a value to a property not a function so the
syntax is AxMbaxp1.BaudRate = rather than AxMbaxp1.BaudRate() =.

If AxMbaxp1.BaudRate is, in fact, a function then all you are attempting is
a comparison that will result in a Boolean value.

So the question now becomes, how do I turn the value from the ComboBox into
an integer.

The Items collection for a ComboBox is a collection of Object, so the
mechanism you need to use is one that will convert an Object to an Integer
(or Int32).

The obvious one is Convert.ToInt32(cboBaud.SelectedItem).

If you are sure that it can never fail then you can simply use:

AxMbaxp1.BaudRate = Convert.ToInt32(cboBaud.SelectedItem)

The are other variations that you could use, such as:

AxMbaxp1.BaudRate = Convert.ToInt32(cboBaud.Items(cboBaud.SelectedIndex))
AxMbaxp1.BaudRate = Integer.Parse(cboBaud.SelectedItem.ToString)
AxMbaxp1.BaudRate =
Integer.Parse(cboBaud.Items(cboBaud.SelectedIndex).ToString)

however, in my opinion, these are more convoluted and add no value to the
task at hand.

Of course, if you do not have Option Strict On, which I DO NOT recommend nor
endorse, you can use:

AxMbaxp1.BaudRate = cboBaud.SelectedItem

and live with the consequences of the compiler selects an inappropriate
coercion method, which is no different to what you had in you VB6 example.

baudrate = cboBaud(listindex)


<Just_a_fan(a)home.net> wrote in message
news:njid64t72illhldq5j1t2tipn9kt9t2fgn(a)4ax.com...
> In VB6, I could easily take the value from a combo box and make a
> command with it, this: baudrate = cboBaud(listindex).
>
> With the new dotted stuff in VB9, I can't seem to do that. Here's an
> example of my problem.
>
> I have a combo box with various baud rates in it. The user selects one
> and I want to use it.
>
> However, the command to use it is in the format:
> AxMbaxp1.BaudRate() = MBAXPLib.enumBaud.B9600
>
> The ".B9600" could also be just a ".5" which I could get from the
> listindex (selectedindex I think it is now) from the combo box or I
> could use the B9600 from the selecteditem.tostring but I cannot figure
> out how to put either on the end of such a dotted format command.
>
> What I need is
>
> AxMbaxp1.BaudRate() = "MBAXPLib.enumBaud." & _
> cboBaud.selecteditem.tostring
>
> This means I have to have a Select Case and run the combo box index and
> pick the right command thereby increasing code bloat about 10 times what
> it should be.
>
> In general, is there any way to build up a command and avoid using the
> Select Case?
>
> If anyone remembers "REXX", this could easily be done in that language.
> You just make a variable contain 5 and then issue the command with the
> variable at the end. The first part was fixed and the variable was
> appended at the end. In fact, IBM Assembler macro works the same way.
> You can just use a LCLC and build the command up. (Sorry for dredging up
> history but it seems we are losing flexibility and gaining code bloat)
>
> Mike Morrow
> MMSA00E62537
>

From: Steve Gerrard on
Just_a_fan(a)home.net wrote:
> In VB6, I could easily take the value from a combo box and make a
> command with it, this: baudrate = cboBaud(listindex).
>
> With the new dotted stuff in VB9, I can't seem to do that. Here's an
> example of my problem.
>

Depends on what you add to the combo box in the first place. If you just add
strings, you will have to match them up again with something. But you can add
objects directly to the combo. Here is a dumb example, where the enum values are
added, not their "ToString" text. When you get the enum back, you can get the
display text and the integer value from it.

Private Enum Test
Okay = 27
Great = 35
Amazing = 42
End Enum

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange( _
New Object() {Test.Okay, Test.Great, Test.Amazing})
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If ComboBox1.SelectedItem IsNot Nothing Then
Dim x As Test = CType(ComboBox1.SelectedItem, Test)
MsgBox("That would be " + x.ToString _
+ " (value = " + CInt(x).ToString + ")")
End If

End Sub


From: Lloyd Sheen on

<Just_a_fan(a)home.net> wrote in message
news:njid64t72illhldq5j1t2tipn9kt9t2fgn(a)4ax.com...
> In VB6, I could easily take the value from a combo box and make a
> command with it, this: baudrate = cboBaud(listindex).
>
> With the new dotted stuff in VB9, I can't seem to do that. Here's an
> example of my problem.
>
> I have a combo box with various baud rates in it. The user selects one
> and I want to use it.
>
> However, the command to use it is in the format:
> AxMbaxp1.BaudRate() = MBAXPLib.enumBaud.B9600
>
> The ".B9600" could also be just a ".5" which I could get from the
> listindex (selectedindex I think it is now) from the combo box or I
> could use the B9600 from the selecteditem.tostring but I cannot figure
> out how to put either on the end of such a dotted format command.
>
> What I need is
>
> AxMbaxp1.BaudRate() = "MBAXPLib.enumBaud." & _
> cboBaud.selecteditem.tostring
>
> This means I have to have a Select Case and run the combo box index and
> pick the right command thereby increasing code bloat about 10 times what
> it should be.
>
> In general, is there any way to build up a command and avoid using the
> Select Case?
>
> If anyone remembers "REXX", this could easily be done in that language.
> You just make a variable contain 5 and then issue the command with the
> variable at the end. The first part was fixed and the variable was
> appended at the end. In fact, IBM Assembler macro works the same way.
> You can just use a LCLC and build the command up. (Sorry for dredging up
> history but it seems we are losing flexibility and gaining code bloat)
>
> Mike Morrow
> MMSA00E62537
>

Each item in the listbox can be an object. So what you can use is an
array/list of object where the object has a property which will display and
a property which you will use as the baudrate.

When an item is selected there is a selecteditem property for the combobox
which can get whatever info you want.

This is a pattern used by listboxes/comboboxes. If you create an array of
your new object you can then use binding to get the items into the items
collection of the combox. You will use the DisplayMember to tell the
combobox what to display.

This while it may seem lots to do is a pattern to learn and will / can be
used for every instance of a listbox/combobox.

Hope this helps
LS