tags:

views:

119

answers:

8

I want to use "*" or "\" as mathematical operators as such:

"I am going to clarify"

dim tbox as textbox
tbox.text = "*"

dim i as integer = 8 tbox.text 3

"End Clarify"

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


dim i as integer = 8 [mult] 3

and the result would be i is equal to 24

or

dim i as integer = 8 [div] 2 

and the result would be i is equal to 4

Can anyone solve this in one line without building a long, complex function? I would also like for this to be something that is already a part of the VB.NET structure and doesn't require an import.

If such a solution does not exist how do I do it with a function or .dll import?

+3  A: 

OK I'm a bit lost, why wouldn't you just use the operators directly?

i.e.

Dim i as integer = 8 * 3
Dean
This operand is coming from a text box as a string.
Michael Eakins
Then you should be using some sort of case/switch statement to select the real operator based on the input.
Fosco
That isn't very efficient to simply write a statement that isn't intuitive, the statement would then need updated for additional operands, such as "+ or - or mod or > or <", you can see this would become lengthy.
Michael Eakins
Can you give any context as to why you are doing this as we may be giving you advice based on misunderstanding your requirement as it seems you are trying to re-invent the wheel here
Dean
@Meakins: It wouldn't be *that* lengthy. The alternative is to sanitize user input anyway; is that better?
Dan Tao
I am building a Grammar engine and its result has the possibility of being "*" or "\" I want to parse this efficiently.
Michael Eakins
+2  A: 

You could eval an entire string like "8*3" using a ScriptControl:

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

Pretty dangerous, though...you would need to throughly sanitize any user input to make sure it's only math they are attempting.

pjabbott
+5  A: 

You can use CodeDom to evaluate expressions. The expression in your example would be "8 * 3", or "8" + tbox.Text + "3".

Here's an example of how to do this in VB.NET. The function is certainly more than a one liner, but calling it will be simple.

Jon B
+4  A: 

Can anyone solve this in one line without building a long, complex function?

Perhaps, yes, but I think a small function is much better:

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 you really want a one-liner you can use the If() function:

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

This will not check for invalid operators.

Martin Liversage
+6  A: 

Insert This into where you want to evaluate.

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
Jimmie Clark
+4  A: 

This is not worth even trying. It will eat up so much CPU just to do the operation you could have manually typed it.

TheAssassin83
+3  A: 

This is confusing, are you saying you want to use multiply and divide as operators in VB.NET?

The can be accomplished with dim i as integer = 8*9

Stack Guy
+3  A: 

I believe you need some very complex logic to accommodate this omission from Microsoft. Here's how to do it in C#

http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx

Anonomouse