[RESOLVED] Sum of Double in Array-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Sum of Double in Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    7

    Resolved [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

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    294

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    7

    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.

  4. #4
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    294

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    7

    Re: Sum of Double in Array

    Thanks. That is what I need in the current situation.

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,248

    Re: Sum of Double in Array

    Quote Originally Posted by ccsmith View Post
    Thanks. That is what I need in the current situation.
    VB.Net Code:
    1. Dim dFlangeX = "48.6;48.6"
    2. Dim dFlangeX_SUM = dFlangeX_SUM = Split(dFlangeX, ";").Sum(Function(x) CDbl(x))

    Kris

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,276

    Re: Sum of Double in Array

    Quote Originally Posted by i00 View Post
    VB.Net Code:
    1. Dim dFlangeX = "48.6;48.6"
    2. Dim dFlangeX_SUM = dFlangeX_SUM = Split(dFlangeX, ";").Sum(Function(x) CDbl(x))

    Kris
    I think you mistyped there.
    vb.net Code:
    1. Dim dFlangeX_SUM = dFlangeX.Split(";"c).Sum(Function(x) CDbl(x))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width