-
Apr 16th, 2018, 02:03 PM
#1
Thread Starter
New Member
[RESOLVED] Sum of Double in Array
I have an array filled with numbers (double). I would like to sum all elements in the array.
Something isn't good because I get number 12 as result. Please give me your support to correct the code.
Code:
Private dFlangeX As String
Private dFlangeX_SUM As Double
dFlangeX = "48.6;48.6"
Dim i As Integer
For i = 0 To dFlangeX.Split(";").Count
dFlangeX_SUM = dFlangeX_SUM + CDbl(Val(dFlangeX(i)))
Next
-
Apr 16th, 2018, 02:13 PM
#2
Re: Sum of Double in Array
You have a (non-array) String and a (non-array) Double variable defined. So, when you say that you have an array filled with numbers, that is incorrect. If you think you are creating an accessible array in your For statement, you are incorrect.
If your true intention is to have a single String value that contains multiple numeric values separated by semi-colons, and then use Split to store those values into an array, then you need to declare an appropriate array variable and store the results of the Split command into that variable, and then access the elements of that new array variable inside your For statement.
-
Apr 16th, 2018, 02:18 PM
#3
Thread Starter
New Member
Re: Sum of Double in Array
Thanks for your prompt reply. As I am a very beginner in programming I would like to ask for your help to re-write me the code. I am really appreciate your any support.
-
Apr 16th, 2018, 02:30 PM
#4
Re: Sum of Double in Array
Given the goal of adding up the numeric values in an array, my code would probably look different than what you have written, so me writing it for you probably doesn't help much. I'll give you a suggestion, though. It is inefficient, and not the way I would write it, but it is consistent with the style of code you posted and should accomplish what you are looking for. Then, you can maybe do some more digging and figure out a better way to do it.
Since dFlangeX isn't an array, treating it like an array inside of your For loop isn't working the way you would expect it. But if you use the same Split statement like you do in your For statement, then it should give you the expected answer:
Code:
For i = 0 To dFlangeX.Split(";").Count
dFlangeX_SUM = dFlangeX_SUM + CDbl(Val(dFlangeX.Split(";")(i)))
Next
-
Apr 16th, 2018, 02:37 PM
#5
Thread Starter
New Member
Re: Sum of Double in Array
Thanks. That is what I need in the current situation.
-
Apr 16th, 2018, 06:59 PM
#6
Re: Sum of Double in Array
Originally Posted by ccsmith
Thanks. That is what I need in the current situation.
VB.Net Code:
Dim dFlangeX = "48.6;48.6"
Dim dFlangeX_SUM = dFlangeX_SUM = Split(dFlangeX, ";").Sum(Function(x) CDbl(x))
Kris
-
Apr 16th, 2018, 11:05 PM
#7
Re: Sum of Double in Array
Originally Posted by i00
VB.Net Code:
Dim dFlangeX = "48.6;48.6" Dim dFlangeX_SUM = dFlangeX_SUM = Split(dFlangeX, ";").Sum(Function(x) CDbl(x))
Kris
I think you mistyped there.
vb.net Code:
Dim dFlangeX_SUM = dFlangeX.Split(";"c).Sum(Function(x) CDbl(x))
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
|