• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Operation with datatable values in one textbox with C#

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My objective is to type an operation (operands and operators) in a single expressionTextBox and if I click on a button the result will be displayed.
For example, after inserting 3 variables of type float var1, var2 and var3 (in a table of data with their content values ) I want to write in the expressionTextBox var1 = var2 + var3 (or '*' or '-'...) and the result of var1 will be displayed in ResultTextbox.
Is this the right way to do that? The textbox has to be able to understand more than one variable from datatable and has to understand operands for the operation. Are there other methods using listBox! Help me please !
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Are you using Windows Workflow Foundation? If so, why?

In my opinion, the best way to do this is to write your own expression parser. What symbols is the user allowed to type in? How complex can the expression be?
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No i'm using Windows Presentation Foundation (WPF) in Visual Studio , and the expression may contain functions (cos , sin, ..) and operands(*,+,/,...) like ( a*cos(b)=? )
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I asked is because you mentioned ExpressionTextBox as if it were a type, and it's part of WWF and not appropriate for your use-case. You probably used it as an identifier for your TextBox though.

Forget the user interface. The challenge here is to parse a string as a mathematical expression and then evaluating it. There are probably libraries out there that can do this for you, but if your expressions only contain the basic arithmetic operators and trigonometric functions, with some guidance you can easily write your own parser.

First you need to create a model that can represent a mathematical expression. You need classes that represent the following concepts: binary operations, unary operations, function calls, floating point literals. Let all of them implement an interface that represents a mathematical expression. Here is an example:

Now, if you want to go down this road, try to create classes that represent functions and unary operators, and then I will go on to explain how you can parse them from strings.
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you didn't understand my problem , i'm searching how can i read with an only textbox 2 variables separated with an operand , how can a textbox get two variables picked from the same column of datatable?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I'm afraid that your requirements are not all that clear, although I do believe you're meaning to parse an expression.

Please give us a real example of what your table looks like, a real example of an expression you would enter in the textbox and what result you expect it to show.
 
Robertina Pene
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example :
In the begining i have a table of data which contain two columns (column1 of names of variables (named `Variable`)and column2 of contents with type float (named `Contents`) , i inserted 3 variables `var1` , `var2` and `var3` in that table with their contents successively  0  , 6.9 and 8.3 . Now i have a textbox1 in the gui i want to be able to write in it  my expression var1=var2*cos(var3)  and when i click on a button the result (-2.9765002303)  will be showed in another textbox2 , and the value 0 (initial value of var1) will be changed to  -2.9765002303 in the table of data (after calcul), i hope that it's more clear now ! thank you
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that does make it clear. It also makes me more certain that the solution I proposed will work for you, IF you want to write your own solution. Otherwise, you need to Google for "parse math .NET" to find libraries that will do this for you.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic