Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to type the numbers in a textbox:
and then display the result.
so how can i split the positive numbers and the negatives?
or is there any other way to do it ?
Updated 19-Feb-13 11:59am
Richard C Bishop 19-Feb-13 16:51pm    
You can use the .Split() that returns an array and then do what you want with them.
Sergey Alexandrovich Kryukov 19-Feb-13 17:10pm    
I suggested a pretty unusual but quite practical solution, please see...
Maciej Los 19-Feb-13 16:53pm    
I think, you mean "operators" ("+", "-")...
Sergey Alexandrovich Kryukov 19-Feb-13 17:09pm    
I have that very funny solution I just promised. It's quite reasonable, depending on the purpose of all that, at least much more interesting than the boring parsing...
Zoltán Zörgő 19-Feb-13 17:00pm    
Note, that this is an expression, there are no negative numbers, only substraction operators.

Of course you can parse, interpret it, and show the result. But this is kind of boring.

I can advise one "exotic" alternative: wrap the input in C# code, compile it using CodeDOM, execute (you will have to use a separate Application Domain, to avoid memory leak! see below), and show the result, if it was successful, otherwise show compilation error.

Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

Not a very simple solution, but you can do a lot more with that.

If something is not quite clear, I'll gladly explain. It works.

Share this answer
Maciej Los 19-Feb-13 17:14pm    
This is not funny answer, but full professional answer! 5^5!
Sergey Alexandrovich Kryukov 19-Feb-13 17:19pm    
Nor "full" neither "professional" is a barrier for being funny. :-)
Thank you, Maciej.
appleduardo 19-Feb-13 18:19pm    
would you help me to do it by the "boring" way ? please! i dont understand much the way you said is a good way 2do it.
Sergey Alexandrovich Kryukov 19-Feb-13 18:31pm    
I say, boring... :-)

Help with what? Just use logic. This is just work, no secrets to share. Don't use string.Split, loop by all charters, have 3 "modes": digits, blank spaces, sign, invalid (for other characters; throw exception when you face them). This way, split. Remove blank spaces why splitting, just don't add them to the list. Find numeric values using, say uint.Parse. Do not consider negative values, treat '-' as operators, not a part of the number. Summarize taking sign into consideration. Simple, boring... The biggest thing: this is one-time work. Should you add operators, brackets, and you will need to do it all again.
OK, I described the solution, but I should have asked: what did you try so far?..

Better use my approach.

You better use or write an expression evaluator.
You can use the classical approach as described here: Building Expression Evaluator with Expression Trees in C# – Improving the API[^]
Use a third party library as this one:[^]
Or use the reflection to emit code:[^]
Or use CodeDom to run dynamic code at runtime, like described here:[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900