-1

Visual Basic, Visual Studio 2013

My question is how change this code to read any function that was wrote in textbox that the program did not pay conversion error.

Public Function f(Byval x As Double) As Double

        Return x * x + 2 * x    <- how read this function from textbox ?

   End Function

I try other one and did not work:

Public Function f(x) As Double

   f = TextBox1.Text

End Function
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Pit998
  • 49
  • 8
  • Which is it? VBA or visual studio (i.e. .NET, which is not VBA). – rory.ap Apr 17 '14 at 17:22
  • Also -- if this is VB.NET (not VBA) --, looking more closely, I don't think this is possible. I was going to suggest a delegate or lambda expression, but you can't provide that dynamically at runtime (as far as I know). – rory.ap Apr 17 '14 at 17:25
  • VBA using Visual Studio 2013 ; Windows Forms Application – Pit998 Apr 17 '14 at 17:26
  • 1
    You can use VBA in Visual Studio 2013? That's news to me. Is that a new feature in 2013? – rory.ap Apr 17 '14 at 17:28
  • @simoco seems to disagree. – rory.ap Apr 17 '14 at 17:28
  • `Return x * x + 2 * x` is definatly not a syntax of VBA – Dmitry Pavliv Apr 17 '14 at 17:29
  • yes i can use vba in VS2013 – Pit998 Apr 17 '14 at 17:50
  • error - "conversion to type 'Double' is not valid." – Pit998 Apr 17 '14 at 17:54
  • If your function name is "f" and you do this "f = TextBox1.Text" then it's the same as doing "Return TextBox1.Text". Your function is set to return a double, not a string. – the_lotus Apr 17 '14 at 18:14
  • http://social.msdn.microsoft.com/Forums/vstudio/en-US/7f62b87d-a35c-4074-a0f0-84a9dd7ff0a5/convert-string-to-formula maybe? Or http://ncalc.codeplex.com/ – John Bustos Apr 17 '14 at 18:26
  • yes i know it, but is there any way to change it ? or to change TextBox.Text to double? – Pit998 Apr 17 '14 at 18:27
  • 2
    I think it really depends on how complex your function is - The quickest way would be to use soemthing like NCalc from what I see - http://ncalc.codeplex.com/wikipage?title=parameters&referringTitle=Home – John Bustos Apr 17 '14 at 18:32
  • So you are basically asking the program to read in a string and then break the string apart separating each piece into its correct data type and then perform the correct operations using the operators that were in the string? – John Apr 17 '14 at 20:30
  • possible duplicate of [Is there a string math evaluator in .NET?](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) – John Alexiou Apr 17 '14 at 23:10
  • @ja72 but it isn't work with function f(x): `Dim sc As New MSScriptControl.ScriptControl()`
    `sc.Language = "VBScript"` `Dim expression As String = "1 + 2 * 7"` `Dim result As Double = sc.Eval(expression)` `f = result`
    – Pit998 Apr 18 '14 at 05:16
  • @Pit998 - I do not understand your comment. What doesn't work with function `f(x)`? – John Alexiou Apr 18 '14 at 17:16
  • What is "..program did not pay conversion error"? – John Alexiou Apr 18 '14 at 17:18

1 Answers1

0

You have to read the value from the textbox, check to see if it is numeric, and then pass that value to the calculation.

' in some code somewhere, like maybe a click event
Dim value As Double = 0
If IsNumeric(Textbox1.Text) Then
    value = f(CDbl(Textbox1.Text))
End If
' Do something useful with value

' the rest of the code . . .

Public Function f(Byval x As Double) As Double
    Return x * x + 2 * x
End Function
Adam Zuckerman
  • 1,633
  • 1
  • 14
  • 20