简体   繁体   English

如何在Visual Basic(VB.NET)中将字符串作为命令运行

[英]How to run a string as a command in Visual Basic (VB.NET)

i am running a Sub which takes as arguments a Datatable and a String. 我正在运行一个Sub,它以Datatable和String作为参数。

Private Sub Populate(ByVal dttemp As DataTable, ByVal strWhichPart As String)

At one point there is a combobox which is populated with some of the data in the datatable. 某一时刻有一个组合框,其中装有数据表中的某些数据。 The name of the combobox is cmd plus the name of the string for example when the string is Person_FirstName the name of the combobox is cmbPerson_FirstName. 组合框的名称为cmd加上字符串的名称,例如,当字符串为Person_FirstName时,组合框的名称为cmbPerson_FirstName。 Then i add items to the combobox like this: 然后我将项目添加到组合框中,如下所示:

 cmbPerson_FirstName.Items.Add(strHold(rr))

My question is this can i make a String into a command? 我的问题是我可以将String变成命令吗? Because i have many comboboxes which have the same name as the string argument of the sub how can i do something like this to work 因为我有很多组合框与子字符串参数具有相同的名称,我该如何做这样的工作

strWhichPart.Items.Add(strHold(rr))

in which strWhichPart is a string. 其中strWhichPart是字符串。 Is there a command that i can execute a string as a command? 有没有可以将字符串作为命令执行的命令? Thank you. 谢谢。

如果我理解正确,只需使用字符串作为键从Form的Controls集合中获取正确的Controls

CType(Controls(strWhichPart), ComboBox).Items.Add(strHold(rr))

You can use reflection to achieve this by creating an assembly with the code in a method, but its really not recommended. 您可以通过使用方法中的代码创建一个程序集来使用反射来实现此目的,但实际上不建议这样做。 As in, it's insane. 就像疯了一样。 I suspect there are no problems in .NET development that need this approach. 我怀疑.NET开发中没有需要这种方法的问题。

Rather than using this approach, actually pass the relevant combo box as an argument - not an arbitrary string. 与其使用这种方法,不如将相关的组合框作为参数传递-而不是任意字符串。 Combo boxes are objects like anything else. 组合框是其他任何对象。 You could create a dictionary which allows you to lookup combo boxes by a string. 您可以创建一个字典,使您可以通过字符串查找组合框。 Something like: 就像是:

Dictionary(Of String, ComboBox) MyDictionary = new Dictionary(Of String, ComboBox)()
MyDictionary.Add("ComboBoxA", objComboBoxA)
ComboBox objTheComboBox = MyDictionary("ComboBoxA")

The name you give your objects should not be semantically relevant in your code. 您为对象指定的名称在代码中不应与语义相关。 If I name an object "lstMyObjectNamedThisWayForAReason1" I should NOT be using that to reference it. 如果我将对象命名为“ lstMyObjectNamedThisWayForAReason1”,则不应使用该对象来引用它。 Instead, there should be a separation between what constitutes a GUI element and how it is referenced. 取而代之的是,在组成GUI元素和如何引用GUI元素之间应该有分隔。

For example, if I create a WinForms GUI and reference all the items directly, then later have to write another front-end using a different framework I have to rewrite every reference. 例如,如果我创建一个WinForms GUI并直接引用所有项目,则以后必须使用不同的框架编写另一个前端,我必须重写每个引用。 This isn't a problem if you don't tie your logic directly into your controls. 如果您不将逻辑直接绑定到控件中,那么这不是问题。

The only reason to tie them together is laziness and lack of respect for co-workers who might have to improve on your code in future. 将他们联系在一起的唯一原因是懒惰和对同事的尊重,因为他们将来可能需要改进您的代码。 Why should they care what the GUI looks like? 他们为什么要关心GUI的外观? They might not even be using Visual Studio! 他们甚至可能没有使用Visual Studio! They certainly can't take your code and use it elsewhere without ripping out your GUI dependency. 他们当然不能不删除您的GUI依赖关系而将您的代码用于其他地方。

With a minor modification of ho1 it worked. 经过对ho1的较小修改,它可以工作。 Thanks a lot 非常感谢

 CType(Controls("cmb" + strWhichPart), ComboBox).Items.Add(strHold(rr))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM