什么是这个代码的VB9等价物 [英] What's the VB9 equivalent for this code

查看:116
本文介绍了什么是这个代码的VB9等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB6中,我可以轻松地从组合框中获取值并使用它生成一个

命令,这个:baudrate = cboBaud(listindex)。


使用VB9中新的虚线内容,我似乎无法做到这一点。这是我的问题的一个

的例子。


我有一个包含各种波特率的组合框。用户选择一个

并且我想使用它。


但是,使用它的命令格式为:

AxMbaxp1.BaudRate()= MBAXPLib.enumBaud.B9600

.B9600也可以只是一个.5。我可以从组合框中获取

listindex(selectedindex,我认为现在是这样)或者我可以使用selectitem.tostring中的B9600但是我无法想象

如何将这两个点缀格式命令结束。


我需要的是


AxMbaxp1.BaudRate()=" MBAXPLib.enumBaud。" &安培; _

cboBaud.selecteditem.tostring


这意味着我必须有一个Select Case并运行组合框索引和

选择正确的命令,从而增加代码膨胀大约是它应该是b $ b的10倍。


一般来说,有没有办法建立命令并避免使用

选择案例?


如果有人记得REXX,可以用这种语言轻松完成。

您只需使变量包含5,然后在结尾处发出带有

变量的命令。第一部分是固定的,最后附加了变量

。实际上,IBM Assembler宏的工作方式相同。

您可以使用LCLC并构建命令。 (对不起疏浚

历史,但似乎我们正在失去灵活性并获得代码臃肿)


Mike Morrow

MMSA00E62537

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

推荐答案

事情就是在.NET中编译代码的方式不允许你创建
代码以这种方式代码。你需要使用一个Case语句。

< Ju ******** @ home.netwrote in message

news:nj *** ***************************** @ 4ax.com ...
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.
<Ju********@home.netwrote in message
news:nj********************************@4ax.com...

在VB6中,我可以轻松地从组合框中获取值并使用它生成一个

命令,这个:baudrate = cboBaud(listindex)。


使用VB9中新的虚线内容,我似乎无法做到这一点。这是我的问题的一个

的例子。


我有一个包含各种波特率的组合框。用户选择一个

并且我想使用它。


但是,使用它的命令格式为:

AxMbaxp1.BaudRate()= MBAXPLib.enumBaud.B9600

.B9600也可以只是一个.5。我可以从组合框中获取

listindex(selectedindex,我认为现在是这样)或者我可以使用selectitem.tostring中的B9600但是我无法想象

如何将这两个点缀格式命令结束。


我需要的是


AxMbaxp1.BaudRate()=" MBAXPLib.enumBaud。" &安培; _

cboBaud.selecteditem.tostring


这意味着我必须有一个Select Case并运行组合框索引和

选择正确的命令,从而增加代码膨胀大约是它应该是b $ b的10倍。


一般来说,有没有办法建立命令并避免使用

选择案例?


如果有人记得REXX,可以用这种语言轻松完成。

您只需使变量包含5,然后在结尾处发出带有

变量的命令。第一部分是固定的,最后附加了变量

。实际上,IBM Assembler宏的工作方式相同。

您可以使用LCLC并构建命令。 (对不起疏浚

历史,但似乎我们正在失去灵活性并获得代码臃肿)


Mike Morrow

MMSA00E62537
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



Ju ***** ***@home.net 写道:

在VB6中,我可以轻松地从组合框中获取值并制作一个

命令用它,这个:baudrate = cboBaud(listindex)。


使用VB9中新的虚线内容,我似乎无法做到这一点。这是我的问题的一个

的例子。
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.



取决于你首先添加到组合框的内容。如果你只是添加

字符串,你将不得不再次使用它们。但是你可以直接将

对象添加到组合中。这是一个愚蠢的例子,其中枚举值是

,而不是他们的ToString。文本。当你得到枚举时,你可以得到

显示文本和它的整数值。


私人枚举测试

好的= 27

很棒= 35

惊人= 42

结束枚举


私人子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)处理ComboBox1.SelectedIndexChanged


如果ComboBox1.SelectedItem IsNot Nothing那么

Dim x As Test = CType(ComboBox1.SelectedItem,Test)

MsgBox("那将是< + x.ToString _

+"(value =" ; + CInt(x).ToString +")")

结束如果


End Sub

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

''新的正如你所说的那样,VB9中的虚线东西根本不是新的。自VB4(至少)以来已经有了b $ b。


最大的问题是,ComboBox是如何填充的。在

进一步信息的绝对值中,我将假设其中包含字符串,

" 4800"," 9600"," 14400"等等。


MBAXPLib.enumBaud.B9600只不过是积分值的别名

9600,因此,所有分配给AxMbaxp1.BaudRate的是一个

整数。 enum的使用仅仅为您提供了什么值可以接受的线索


另请注意,您要为属性分配值而不是函数所以

语法是AxMbaxp1.BaudRate =而不是AxMbaxp1.BaudRate()=。


如果AxMbaxp1.BaudRate实际上是函数那么所有你正在尝试的是

a比较,这将产生一个布尔值。


所以问题现在变成,如何将值从ComboBox转换为

一个整数。


ComboBox的Items集合是Object的集合,所以你需要使用

机制是一个将对象转换为整数的对象

(或Int32)。


显而易见的是Convert.ToInt32(cboBaud.SelectedItem)。 />

如果您确定它永远不会失败,那么您可以简单地使用:


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


其他的变种您可以使用的tions,例如:


AxMbaxp1.BaudRate = Convert.ToInt32(cboBaud.Items(cboBaud.SelectedInde x))

AxMbaxp1.BaudRate = Integer.Parse(cboBaud.SelectedItem.ToString)

AxMbaxp1.BaudRate =

Integer.Parse(cboBaud.Items(cboBaud.SelectedIndex).ToString)


但是,在我看来,这些更复杂,并且对手头的

任务没有任何价值。


当然,如果您没有Option Strict On,我不推荐也不支持
,您可以使用:


AxMbaxp1.BaudRate = cboBaud.SelectedItem <和b $ b一起生活并且编译器的后果选择了一个不合适的

强制方法,这与你在VB6中的例子没有什么不同。


baudrate = cboBaud(listindex)

< Ju ******** @ home.netwrote in message

news:nj ******************************** @ 4ax.com ...
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.SelectedInde x))
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)
<Ju********@home.netwrote in message
news:nj********************************@4ax.com...

在VB6中,我可以轻松地从组合框中获取值并使用它生成一个

命令,这个:baudrate = cboBaud(listindex)。


With VB9中的新点缀,我似乎无法做到这一点。这是我的问题的一个

的例子。


我有一个包含各种波特率的组合框。用户选择一个

并且我想使用它。


但是,使用它的命令格式为:

AxMbaxp1.BaudRate()= MBAXPLib.enumBaud.B9600

.B9600也可以只是一个.5。我可以从组合框中获取

listindex(selectedindex,我认为现在是这样)或者我可以使用selectitem.tostring中的B9600但是我无法想象

如何将这两个点缀格式命令结束。


我需要的是


AxMbaxp1.BaudRate()=" MBAXPLib.enumBaud。" &安培; _

cboBaud.selecteditem.tostring


这意味着我必须有一个Select Case并运行组合框索引和

选择正确的命令,从而增加代码膨胀大约是它应该是b $ b的10倍。


一般来说,有没有办法建立命令并避免使用

选择案例?


如果有人记得REXX,可以用这种语言轻松完成。

您只需使变量包含5,然后在结尾处发出带有

变量的命令。第一部分是固定的,最后附加了变量

。实际上,IBM Assembler宏的工作方式相同。

您可以使用LCLC并构建命令。 (对不起疏浚

历史,但似乎我们正在失去灵活性并获得代码臃肿)


Mike Morrow

MMSA00E62537
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


这篇关于什么是这个代码的VB9等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆