-
Jan 6th, 2022, 04:12 AM
#1
Thread Starter
Junior Member
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?
-
Jan 6th, 2022, 05:11 AM
#2
Re: How to calculate formulas with variables?
Please remember next time...elections matter!
-
Jan 6th, 2022, 06:33 AM
#3
Thread Starter
Junior Member
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
-
Jan 6th, 2022, 09:57 AM
#4
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
-
Jan 6th, 2022, 08:19 PM
#5
Thread Starter
Junior Member
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
-
Jan 7th, 2022, 02:43 AM
#6
Re: How to calculate formulas with variables?
Assuming a+b equals a one digit answer...
Code:
a=4
b=2
v=CChar(a+b)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 7th, 2022, 03:44 AM
#7
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 7th, 2022, 11:57 AM
#8
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
-
Jan 7th, 2022, 12:30 PM
#9
Re: How to calculate formulas with variables?
Originally Posted by Shaggy Hiker
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 7th, 2022, 12:33 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2022, 04:28 AM
#11
Re: How to calculate formulas with variables?
Originally Posted by Shaggy Hiker
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.
-
Jan 12th, 2022, 09:15 AM
#12
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
-
Jan 12th, 2022, 09:30 AM
#13
Re: How to calculate formulas with variables?
Originally Posted by techgnome
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2022, 02:03 PM
#14
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.
-
Jan 12th, 2022, 09:28 PM
#15
Thread Starter
Junior Member
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.
-
Jan 12th, 2022, 10:05 PM
#16
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.
-
Jan 13th, 2022, 07:18 PM
#17
Thread Starter
Junior Member
Re: How to calculate formulas with variables?
Originally Posted by Niya
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!
-
Jan 18th, 2022, 08:58 AM
#18
Thread Starter
Junior Member
Re: How to calculate formulas with variables?
This method feels the best
-
Jan 18th, 2022, 10:56 PM
#19
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.
Last edited by i00; Jan 18th, 2022 at 10:59 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|