SomethingToChatWith Posted April 22, 2009 Share Posted April 22, 2009 I have an application that well have a simple calculator built in. I'd like to make it as easy as possible for myself here by storing the problem the user is calculating in a read only textbox. When the user hits equal, it should take the user's mathematic expression from the textbox and actually calculate it to return an answer. Example User puts in 2 + 2, Textbox control reads "2+2" (Appends on each character as the user types it like in Windows calculator, User clicks the equal button and the text property from textbox control is calculated and returns an answer. The long way I can think of solving this is going character by character and converting the values, but this would require much more code and time to develop. So is there an easy way to calculate mathematically whats in a textbox? I use vb.net but c# developers feel free to comment since it should be similar... Quote Link to comment Share on other sites More sharing options...
Cthobs Posted April 22, 2009 Share Posted April 22, 2009 If you want the textbox update to happen in the browser real time without a forms submission, you're gonna need to do it with javascript. Are you planning on a post submission? Writing that in JS is fairly straightforward. And using eval() on the final string will let you execute the expression as a JS command. If you're planning on vb executing it, see if it supports an eval() style string evaluator. Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted April 23, 2009 Author Share Posted April 23, 2009 Ok to clarify here it is a vb.net windows forms application. Its not only a calculator, but a calculator integrated into it would make things nice for the user without having to hunt down windows calculator in the start menu or open more windows in thier taskbar than what they need. Would this work?, taking the text property, having it convert to a decimal if it calculated a mathematic expression passed in, and assigning to a variable for output? decAnswer = CDec(TextBox1.Text) If its only a number than thats not an issue but i'm not sure if vb would calculate the text as an expression like in the case 2+2 to make 4 and convert it to a decimal :( Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted April 23, 2009 Author Share Posted April 23, 2009 Well the answers no. In the coming days I'll put a function together doing everything the hard way :( ... But for those interested I'll post it here when I'm done with it :) Quote Link to comment Share on other sites More sharing options...
Cthobs Posted April 24, 2009 Share Posted April 24, 2009 I hate VB. Never really spent time with it. But doing a little looking I can see that there is no function to evaluate a string like that. I did find this though, unless you really are into doing it yourself. http://www.vb-helper.com/howto_evaluate_expressions.html Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted April 24, 2009 Author Share Posted April 24, 2009 Looks very interesting, a little tweaking here and there but it looks almost like the "rough draft" I was working on earlier today. I think for the purpose of getting it done I'm going to have it calculate as the user types stuff in, but having a function like this well be very useful for other projects or for my app anyway when I'm done with it. Thanks for posting that link. Would it make any difference if I learned C#? I guess I need to stay away from .net all together but what languages would provide flexibility like this that VB can't? I'm stuck on VB at the moment but in the next two years I should know C# as well and maybe Java with the classes I'm taking, so by all means I'm more than willing to learn a new language if it provides additional benefits. Quote Link to comment Share on other sites More sharing options...
Cthobs Posted April 24, 2009 Share Posted April 24, 2009 Would it make any difference if I learned C#? I guess I need to stay away from .net all together but what languages would provide flexibility like this that VB can't? I'm stuck on VB at the moment but in the next two years I should know C# as well and maybe Java with the classes I'm taking, so by all means I'm more than willing to learn a new language if it provides additional benefits. Don't let me turn you off to anything. I hate VB because it was Microsoft's lame and late attempt of getting in on the web's server side. Biggest flaw, no dynamic includes. And then sticking it into .net was like putting a Model T's engine in a new Vette. I do C# cause it pays. But then, I do a lot of things for money I wouldn't do on my own. From the grand scheme of code management and deployment, C# is not a bad way to do things. It's major flaw is the lack of data persistence. Having to re-instantiate every object needed for every request creates unnecessary overhead on the web and db machines and load on your network. It's biggest strength is being able to maintain a business rules layer of compiled objects which your devs can access in their web layer development. This provides code lock down and protection for complex environments. There are a lot of C# jobs out there. Not a bad way to make a living. If you learn C# and Java, PHP's not that far off for you. And if you're looking to make a living at web or internet app development, the broader your skills, the better off you will be. Might be worth looking into Ruby and ColdFusion as well. You could learn CF in a weekend, it's cake to work with. Look through Monster or Dice and you will see lots of dev jobs where knowledge of multiple platforms is required. Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted May 3, 2009 Author Share Posted May 3, 2009 Ok update for those that may be interested. Its not done because I've been really busy. For my app for at least the moment anyway I ended up putting in a button that simply launches windows calculator. But once it is it'll be here... Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted May 13, 2009 Author Share Posted May 13, 2009 Ok with the way I was writing it I would've had a gigantic function or 5-6 functions and no support for ()'s. I think I'll stick with the code from the link than. Now to convert it to C# since I've gotten into that as well lately... Update: somethings up with the function. I had a whole bunch of not delcared errors until I imported System.Math but I still got one more error for the single line using "m_Primatives". Its not declared ovbiously. I wonder what its there for or if theres another namespace I need imported. C# definetly does have an advantage over VB, paticuarly that being of size of the compiled assembly. On a few test apps I've thrown together the C# versions were only half the size of the vb ones. Anyone know why that is if its all supposed to compile to MSIL anyway? Quote Link to comment Share on other sites More sharing options...
shawty Posted May 25, 2009 Share Posted May 25, 2009 Something.... Cthobs is bang on the money with eval, there is an eval function built into VB and it works near enough the same was as the javascript one. However, that's not why my post ;) If you use the ASP>NET ajax toolkit, there's 2 components in there that will A) Allow you to do your real time in page update and B) Have a Calulator connected to your text box. You'll find everything you need to know in both VB and C# here. http://www.asp.net/ajax/ Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted June 2, 2009 Author Share Posted June 2, 2009 Its not an ASP.Net application (just a normal windows forms app), but I'll take a look at it. Thanks... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.