|
-
Apr 28th, 2011, 03:00 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Calculating numbers
In the TextBox1 I have the following numbers:5 4.1 6.23 0 0 7 7.924

Possible to calculate all the numbers in this way?
like replace all the "space" in "+" and get the answer or something?
-
Apr 28th, 2011, 03:28 AM
#2
Hyperactive Member
Re: Calculating numbers
I like your thinking.. maybe in the next version of VS 
For now though, I would split the values into an array. Then you can loop through the results and add them up.
VB Code:
Dim TextboxText As String = "5 4.1 6.23 0 0 7 7.924"
Dim ArrayOfValues() As String = TextboxText.Split(" ")
Dim Sum As Double = 0
For Each Value As String In ArrayOfValues
If Double.TryParse(Value, Nothing) Then Sum += CDbl(Value)
Next
MsgBox(Sum)
-
Apr 28th, 2011, 03:56 AM
#3
Member
Re: Calculating numbers
 Originally Posted by jay20aiii
I like your thinking.. maybe in the next version of VS 
It's actually possible in 2005, and may be possible in other versions as well :-
Code:
Dim TextBoxText As String = "5 4.1 6.23 0 0 7 7.924"
Dim Formula as String = TextBoxText.Replace(" ", "+")
Dim Evaluator As New DataTable
Dim Result As Double = 0.0
Result = Convert.ToDouble(Evaluator.Compute(Formula, ""))
MessageBox.Show(Result.ToString)
It will also cope with brackets, for example :-
Code:
Dim Formula As String = "5+(3*6)"
-
Apr 28th, 2011, 03:58 AM
#4
Thread Starter
Addicted Member
Re: Calculating numbers
 Originally Posted by jay20aiii
I like your thinking.. maybe in the next version of VS
For now though, I would split the values into an array. Then you can loop through the results and add them up.
VB Code:
Dim TextboxText As String = "5 4.1 6.23 0 0 7 7.924"
Dim ArrayOfValues() As String = TextboxText.Split(" ")
Dim Sum As Double = 0
For Each Value As String In ArrayOfValues
If Double.TryParse(Value, Nothing) Then Sum += CDbl(Value)
Next
MsgBox(Sum)
Thank you very much
-
Apr 28th, 2011, 04:38 AM
#5
Hyperactive Member
Re: [RESOLVED] Calculating numbers
Cool, I'm liking that DataTable function InetiaM. I bet I will be able to find a use for it at some point in the future.
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
|