如何在vb.net中以字符串格式执行一行代码 [英] How to execute a line of code in string format in vb.net

查看:216
本文介绍了如何在vb.net中以字符串格式执行一行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个调用方法的代码如下所示

Hi
i have a code to call a method is like below

dim r as integer
 r = Add(a,b)


我需要更改方法加法而不是减法或乘法等.以便我已将所有方法名称存储在数据库中,并在运行时将该方法名称加载到组合框
然后我有字符串格式的表单代码,如下所示


i need to change the method Add instead subtract or multiplication etc.. so that i have store all method names in database and load that method names to Combobox at runtime
then i have form code in string format like below

dim str as string
str="r=" & Combobox1.selectedItem & "(a,b)"
// combobox1.selectedtext is Add 
//and str is Add(a,b)  


现在我需要执行/评估上面的行
请给建议.....

在此先感谢


now i need execute/evalute the above line
plz give suggestions.....

thanks in advance

推荐答案



尝试关注
hey

try following
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
       MessageBox.Show(Calculate(ComboBox1.SelectedItem, 10, 5).ToString())
   End Sub

   Private Function Calculate(ByVal functionName As String, ByVal a As Integer, ByVal b As Integer) As Integer

       If functionName = "Add" Then
           Return a + b
       ElseIf functionName = "Minus" Then
           Return a - b
       Else
           Return a / b
       End If

   End Function


祝你好运


Best Luck



一种有效的方法是使用CodeDOM.看看这些帖子,了解有关CodeDOM的信息:
使用CodeDOM进行动态代码生成 [使用CodeDom编译 [
Hi,
One powerful way is to use CodeDOM. Take a look at these post to find out about CodeDOM:
Dynamic Code Generation Using CodeDOM[^]
Compiling with CodeDom[^]

I hope it helps,
Cheers


这篇关于如何在vb.net中以字符串格式执行一行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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