2010-09-17 79 views
4

我想用 “*” 或 “\” 作为数学运算这样:如何将字符串“*”或“”用作数学运算符?

“我要澄清”

dim tbox as textbox 
tbox.text = "*" 

dim i as integer = 8 tbox.text 3 

“结束澄清”

dim [mult] as string = "*" 
dim [div] as string = "\" 


dim i as integer = 8 [mult] 3 

和结果将是我等于24

dim i as integer = 8 [div] 2 

,其结果将是我等于4

谁能解决这个问题的一个行没有建立一个长期的,复杂的功能?我也想这是已经是VB.NET结构的一部分,并且不需要导入。

如果这样的解决方案不存在,我该如何处理函数或.dll导入?

+0

你想改变VB.NET的语法?什么是错的代码编写'8 * 3'和'8 \ 2'这是完全合法的VB.NET? – 2010-09-17 15:23:46

+0

没有什么错写8 * 3,如果我没有试图从一个文本框读取操作数,我已经editted我的问题作出澄清的目的。 – 2010-09-17 15:26:39

+1

“没有建立一个长期的,复杂的函数” => **无**为了记录在案,这是不合理的要求。你想要什么,如果使用得当,是一个极其复杂的位逻辑 – 2010-09-17 15:35:41

回答

6

将此插入到您想要评估的位置。

dim [mult] as string = "*" 
dim [div] as string = "\" 


'dim i as integer = 8 [mult] 3 

op = mult 

dim i As Integer = Eval("8" & op & "3") 

'i is your result 


    Private Function Eval(ByVal command As String) As Object 
    Dim MyProvider As New VBCodeProvider 'Create a new VB Code Compiler 
    Dim cp As New CodeDom.Compiler.CompilerParameters  'Create a new Compiler parameter object. 

    cp.GenerateExecutable = False  'Don't create an object on disk 
    cp.GenerateInMemory = True   'But do create one in memory. 

    'If cp.OutputAssembly is used with a VBCodeProvider, it seems to want to read before it is executed. 

    'See C# CodeBank example for explanation of why it was used. 



    'the below is an empty VB.NET Project with a function that simply returns the value of our command parameter. 

    Dim ClassName As String = "class" & Now.Ticks 

    Dim TempModuleSource As String = "Imports System" & Environment.NewLine & _ 
            "Namespace ns " & Environment.NewLine & _ 
            " Public Class " & ClassName & Environment.NewLine & _ 
            "  Public Shared Function Evaluate()" & Environment.NewLine & _ 
            "   Return (" & command & ")" & Environment.NewLine & _ 
            "  End Function" & Environment.NewLine & _ 
            " End Class" & Environment.NewLine & _ 
            "End Namespace" 

    'Create a compiler output results object and compile the source code. 

    Dim cr As CodeDom.Compiler.CompilerResults = MyProvider.CompileAssemblyFromSource(cp, TempModuleSource) 

    If cr.Errors.Count > 0 Then 

     'If the expression passed is invalid or "", the compiler will generate errors. 

     'Throw New ArgumentOutOfRangeException("Invalid Expression - please use something VB could evaluate") 
     Return Nothing 
    Else 

     'Find our Evaluate method. 
     Dim methInfo As Reflection.MethodInfo = cr.CompiledAssembly.GetType("ns." & ClassName).GetMethod("Evaluate") 

     'Invoke it on nothing, so that we can get the return value 
     Return methInfo.Invoke(methInfo, New Object() {}) 
    End If 
    End Function 
2

你可以像EVAL“8·3”整个字符串使用ScriptControl的:

http://www.devx.com/vb2themax/Tip/18773

相当危险,不过......你需要throughly净化任何用户输入,以确保它是唯一数学他们正在尝试。

5

您可以使用CodeDom来评估表达式。您示例中的表达式应为"8 * 3""8" + tbox.Text + "3"

这是关于如何在VB.NET中做到的example。该功能当然不仅仅是一个班轮,而是将其称为简单。

5

任何人都可以在一行中解决这个问题,而不需要构建一个长而复杂的函数吗?

也许,是的,但我觉得一个小功能要好得多:

Function Compute(a as integer, oper as String, b as integer) as integer 
    If oper = "*" Then 
    return a*b 
    ElseIf oper = "\" Then 
    Return a\b 
    Else 
    Throw New InvalidOperationException() 
    End If 
End Function 

如果你真的想要一个班轮你可以使用If()功能:

Dim i As Integer = If(oper = "*", a*b, a\b) 

这不会检查无效的操作员。

3

这是混乱的,你说你想用乘法和除法在VB.NET运营商?

的可以用暗淡来完成我作为整数= 8 * 9

0

我需要知道做了类似的事情,然后,一个朋友找到了正确的方式做到这一点使用“Microsoft脚本控制1。0" ,要知道如何使用它,请访问此链接:

http://www.devx.com/vb2themax/Tip/18773

我看到它比任何其他更好的方式:-)