Results 1 to 19 of 19

Thread: How to calculate formulas with variables?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    How to calculate formulas with variables?

    I used to learn VFP. There is a macro calculation (&) that can calculate formulas with variables, there such a function or other methods in vb.net(vs2012)?

    VFP code:
    a=4
    b=2
    v="a+b"

    ? &v
    =6

    VB.net code?

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: How to calculate formulas with variables?

    Please remember next time...elections matter!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: How to calculate formulas with variables?

    Thank you for your reply, but the link you gave is not related to this question.

    I want to be able to calculate formulas with variables

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to calculate formulas with variables?

    It won't do quite what you show. What you are showing is a string that happens to be an equation. You can't type an arbitrary string into a textbox and expect VB to be able to interpret that as an equation, identify the variables, identify the operators, substitute values in for the variables, and perform the operation. You can write something that does that, and there may be libraries that will parse and evaluate equations, but it isn't built into VB.

    Having said that, there IS a way to do some simple equations in datatables, or DGV, or something like that. I haven't used it, but it's there. I'm not sure how complex those equations can be, though.

    On the other hand, if you removed the quotation marks, then you could write virtually the same thing:

    a=4
    b=2
    v=a+b

    and v will equal 6, as you want.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: How to calculate formulas with variables?

    thank Shaggy Hiker!
    If you just calculate the equation after removing the quotation marks, the problem is too simple.
    That V is a character variable, not a numerical variable. Now I want to calculate the value of character "a +b" equal 6. This problem is very simple in VFP, but I don't know in VB.Net

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to calculate formulas with variables?

    Assuming a+b equals a one digit answer...

    Code:
    a=4
    b=2
    v=CChar(a+b)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to calculate formulas with variables?

    If a+b results in a number greater than 9…

    Code:
    Dim v as String = CStr(a+b)

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to calculate formulas with variables?

    It may be easy in VFP, but only because VFP included a means to parse and interpret the equation. What is being done behind the scenes to make that easy in VFP is not so easy in itself. There may be some library that adds that functionality to VB.NET, but it isn't built into the language, and it isn't particularly simple to implement directly.

    I wrote something like that for a VERY specialized project, but it wasn't an implementation that would be useful to the general public. After thinking about the problem a bit, I realized that what you are asking for has very few, very specialized, uses. When you need it, you REALLY need it, but you almost never need it. I've written a function evaluator just once in thirty years, and that was for a very specific problem. Most people who want to calculate something, are entering the numbers, but not the equation. Needing to enter BOTH the numbers and the equation is quite rare, in practice.

    Still, I feel like a few people on here have done just that at one point or another over the last decade, or two, so there is likely a library out there that would do what you need. I don't know of one, though, but somebody might.
    My usual boring signature: Nothing

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to calculate formulas with variables?

    Quote Originally Posted by Shaggy Hiker View Post
    I feel like a few people on here have done just that at one point or another over the last decade, or two, so there is likely a library out there that would do what you need. I don't know of one, though, but somebody might.
    You can use DataTable functionality...

    Code:
    Dim dt As New DataTable
    Dim v As String = dt.Compute("4+2", Nothing).ToString

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to calculate formulas with variables?

    I'd imagine you could use variables...

    Code:
    Dim dt As New DataTable
    Dim v As String = dt.Compute(a & "+" & b, Nothing).ToString

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to calculate formulas with variables?

    Quote Originally Posted by Shaggy Hiker View Post
    I've written a function evaluator just once in thirty years, and that was for a very specific problem. Most people who want to calculate something, are entering the numbers, but not the equation. Needing to enter BOTH the numbers and the equation is quite rare, in practice.
    I actually wrote a sample application that's capable of evaluating complex equations. It even has predefined functions like square root. This only problem, it's in VB6. I have an incomplete version in VB.Net that only builds the expression tree.

    Olaf's RichClient library also has a very powerful expression evaluator that could evaluate formulas. That's in VB6 too.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to calculate formulas with variables?

    There's a namespace in ,NET that you can use to build and execute .NET code, can be as simple or as complex as you want (as far as I know).



    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to calculate formulas with variables?

    Quote Originally Posted by techgnome View Post
    There's a namespace in ,NET that you can use to build and execute .NET code, can be as simple or as complex as you want (as far as I know).



    -tg
    CodeDomProvider Class

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to calculate formulas with variables?

    Actually if all you want to do is evaluate formulas, you don't have to use CodeDom. You can use .Net's built in expression tree support. However, you will still have to write the code to parse the text to convert it into an expression tree though. This is not really that difficult if you're familiar with this subject. You can write a simple Pratt parser to do this.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: How to calculate formulas with variables?

    Thank you for letting me learn more. This function is really rarely used. I use mscript OCX to calculate, VB Net code is as follows:

    dim a as short=4
    dim b as short=2
    dim v as string="a+b"
    dim RetuValue as single

    v=replace(v,"a",a)
    v=replace(v,"b",b)
    'by replacement, v = "4 + 2"

    Msgbox (calstringtonum (V)) 'this realizes the function of calculation formula
    'v=6

    Private Function CalStringToNum(ByVal cString As String) As Single
    Dim aa As Object = CreateObject("MSScriptControl.ScriptControl")
    aa. Language = "VBScript"
    Return aa. eval(v)
    End Function

    But, unfortunately, mscript OCX does not support 64 bits, so the CPU compilation target in vs2012 (VB. Net) can only be x86.

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to calculate formulas with variables?

    Ok. I have some code lying around. I'll see if I could put together a decent evaluator for you tonight that uses pure VB.Net code without depending on all that external COM stuff.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: How to calculate formulas with variables?

    Quote Originally Posted by Niya View Post
    Ok. I have some code lying around. I'll see if I could put together a decent evaluator for you tonight that uses pure VB.Net code without depending on all that external COM stuff.
    I'm looking forward to you. Come on!

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jun 2021
    Posts
    19

    Re: How to calculate formulas with variables?

    This method feels the best

  19. #19
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: How to calculate formulas with variables?

    I did this in a project of mine from scratch:
    Attachment 183639
    And I can tell you - to do it properly is not a trivial task... ~900 lines ... but allows the developer to add custom functions, operators, notations, comparators etc easily
    ... I am not willing to give it away, but am willing to help if you get stuck..
    Then again you may be better off downloading something already made.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width