What's the VB9 equivalent for this code

J

Just_a_fan

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
 
S

Scott M.

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.
 
S

Stephany Young

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)
 
L

Lloyd Sheen

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
 
C

Cor Ligthert[MVP]

Mike,

Just a sample to show you how you can do it using a class (in this case the
datatable) which contains a lot of sub classes which you intance with the
New keyword.

I have not made exatly a sample of your problem, but I have included all
kind of index methods of the DataRow to use the DataColumn, I hope the
latter scares you not to much.

For this you need to drag a combobox on a form.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim dc1 As New DataColumn("Rate")
dt.Columns.Add(dc1)
Dim dc2 As New DataColumn("RateLong")
dt.Columns.Add(dc2)
Dim firstText = 9600
For i As Integer = 0 To 3
Dim dr = dt.NewRow
dr.Item(0) = firstText.ToString
dr.Item(1) = firstText.ToString & "Long"
dt.Rows.Add(dr)
firstText = firstText + firstText
Next
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = dc1.ColumnName
ComboBox1.ValueMember = dc2.ColumnName
End Sub
///

Cor
 
J

Just_a_fan

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

WOW! What an exquisite level of pain this .NET stuff puts on us to do
what we used to do quite easily!

Thanks. I will have to chew on this a while. Seems quite complex and
difficult. It used to be so easy.

Now I really miss the help I used to be able to get on stuff. That
makes it hurt even more.

Mike
 
J

Just_a_fan

Yeah, see that's the problem. In good ole VB, you could take that 9600
from the combo box and just use it. Now one has to write a dozen or so
lines of very complex code, debug it, maintain it, etc.

Without being able to symbolically create commands and then issue them,
things get very difficult. It almost seems like the VB developers are
working to assure that we all have jobs forever maintaining about 12
times the code we used to have. Just seems that way...

Code bloat RULES!

Thanks for the code. I will try to digest it some day. But, by then, I
could have done it another way much faster, I think. We will see when I
have time to get into it. Maybe I am just tired and my brain has gone
offline. This is the 7th day running, after all. Time to stop!

Mike
 
A

Armin Zingler

Yeah, see that's the problem. In good ole VB, you could take that
9600 from the combo box and just use it.

In good ole VB, you had to maintain an additional array if you want to store
objects associated with the items in the combobox because you weren't able
to store every type of object in a combo box. Glad that this is simpler now.


Armin
 
J

Just_a_fan

In good ole' VB I DID NOT have to save anything extra. I just used the
9600 right out of the selected item. I could put it in a command string
and go.

Now, it takes databases and lots of lines of messy code.

I can't see this as simpler but if you think it is, good for you.

That's all for me. A HUGE difference of opinion on what is simpler, a
single combo box or dozens of lines of code + a combo box with a
database that has to be created and attached to it.

Mike
 
C

Cor Ligthert[MVP]

Dozens? messy? Show us how you did it in VB, now you are only telling
trash?

However, I see now what you want,

Drag a Combobox on your screen
Go to the propertiews fill the Items Collection with your boudrated
Add an Event Combobox1 Selected index changed
Do in that
AxMbaxp1.BaudRate() = "MBAXPLib.enumBaud." & Combobox1.selecteditem.tostring

However that is so simple, I think that nobody would believe that there was
somebody who would ask this in 2008.

Cor
 
C

Chris Dunaway

In good ole' VB I DID NOT have to save anything extra. I just used the
9600 right out of the selected item. I could put it in a command string
and go.

What's stopping you? How is the following code not simple?

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource =
System.Enum.GetValues(GetType(MBAXPLib.enumBaud))
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
AxMbaxp1.BaudRate = ComboBox1.SelectedItem
End Sub

One line of code to set up all the values in the ComboBox and one line
to get the value out when selected. How is the VB6 solution more
simple than that?

Chris
 
S

Scott M.

Your reaction is not uncommon for folks that are just coming to VB .NET from
VB 6.0.

What you begin to realize though, after a short time, is that while some
things involved less code in VB 6.0, the qualtiy of that code was garbage.
It wasn't OO and it wasn't type-safe. It was full of late-binding calls that
degrade performance and open your app up to runtime errors.

Once you realize how VB .NET coding is meant to be different than VB 6.0,
you begin to appreciate the much more bullet proof, scalable, and better
performing code you get in the end.

-Scott
 
J

J.B. Moreno

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

That's not making a command, that's using the listindex to get the
value of a particular item in a combo box.
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

No, you don't -- it's actually possible to do something like that (NOT
in a couple of lines of code), but that's not what you want to do.

I can't give you the exact code, because I couldn't find anything about
the type MBAXPLIB (only reference I could find to it was your own
post).

You want something like:

AxMbaxp1.BaudRate = integer.parse(cboBaud.SelectedItem.ToString())


(Although I suspect this is some code converted by the wizard, and
cboBaud isn't actually a combo box at all, in which case the above will
need to be changed so that it does refer to the combobox).


Post the code that fills the combo box, and a bit about the variable
AxMbaxp1 and it's BaudRate property and maybe someone could give you a
precise answer.
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.

When comparing VB6 to VB.NET code bloat is generally a consequences of
not knowing how to take the differences between the two into account
when writing your code.
In general, is there any way to build up a command and avoid using the
Select Case?

Like I said, yes, but even so that's not what you want to do.

Even in a language that would let you do that easily, you wouldn't want
to do that -- there's almost always a penalty for going from executing
the code to executing a piece of data.
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)

This can be done in a lot of current languages, perl, php, ruby to name
just 3. But like I said, you wouldn't want to do it in them either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top