Results 1 to 11 of 11

Thread: [RESOLVED] Adding array items

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Resolved [RESOLVED] Adding array items

    This is a really dumb issue and im very confused.. But here it is..

    I have a program thats adding the answers to questions. It takes each of the answers and throws them into an array, then goes through the array and adds them up with whatever multipliers they chose earlier. But, alot of the questions are dealing with money, so there's decimals.

    When i add the values to the array, it rounds them off every time! I dont know how to make it stop. Here's the code that actually puts the value of each answer into the array:

    VB Code:
    1. For MyCount = 1 To Len(Formula)
    2.             Select Case MyCount
    3.                 Case Is = 1, 3, 5, 7, 9, 11, 13, 15 'This is the numbers
    4.                     Cost = Cost + 1
    5.                     MultNum(MyCount) = Left(Formula, 1)
    6.                     MultNum(MyCount) = frmNewProp.GridJobSpecCustom.TextMatrix(QRow, (MultNum(MyCount) * 2) - 1)
    7.                     Formula = Right(Formula, Len(Formula) - 1)
    8.                 Case Is = 2, 4, 6, 8, 10, 12, 14 'This is the multipliers
    9.                     Multiplier(MyCount) = Left(Formula, 1)
    10.                     Formula = Right(Formula, Len(Formula) - 1)
    11.             End Select
    12.         Next MyCount

    Formula for example will have a value like this: "1*2*3"
    That just means First question x Second question x 3rd question.

    Basically what that code is doing is going through the forumla, and seperating out each of the multipliers, and answers. It first makes MultNum the value of the answer on the grid(ie question 1, 2 or 3 etc..). Then right after it makes it the actual value of the answer on the grid, but this is where it either truncates the value after the decimal, or rounds it up depending on my declarations. I tried making MultNum integer, long, and currency... And it does these things each time.. Anybody know how to stop this?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Adding array items

    what is the array declared as?
    Integer?

    Declare it as Currency, or Single
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Adding array items

    ive declared the array as Integer, Long, and Currency and none of them worked.. It rounded or truncated it each time...

    this is how i have it now:

    VB Code:
    1. Dim MultNum(15) As Currency
    Last edited by PMad; Jan 29th, 2007 at 02:39 PM.

  4. #4

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Adding array items

    Here's all my declarations:

    VB Code:
    1. Dim QRow, FormulaCol, QuestionCount As Long
    2. Dim Cost As Single
    3. Dim MultNum(15) As Currency
    4. Dim Formula, Multiplier(14) As String
    5. Dim MyCount, QRemoveNum2, Counts2 As Integer

    Here's the code that calculates the formula, this part isnt really the problem but here it is anyway, incase it helps:

    VB Code:
    1. For MyCount = 2 To ((QuestionCount * 2) - 1)
    2.             Select Case MyCount
    3.                 Case Is = 2
    4.                     Select Case Multiplier(MyCount)
    5.                         Case Is = "*"
    6.                             Cost = MultNum(1) * MultNum(3)
    7.                         Case Is = "+"
    8.                             Cost = MultNum(1) + MultNum(3)
    9.                         Case Is = "-"
    10.                             Cost = MultNum(1) - MultNum(3)
    11.                         Case Is = "/"
    12.                             Cost = MultNum(1) / MultNum(3)
    13.                     End Select
    14.                 Case Else
    15.                     If MyCount < 15 Then
    16.                     Select Case Multiplier(MyCount)
    17.                         Case Is = "*"
    18.                             Cost = Cost * MultNum(MyCount + 1)
    19.                         Case Is = "+"
    20.                             Cost = Cost + MultNum(MyCount + 1)
    21.                         Case Is = "-"
    22.                             Cost = Cost - MultNum(MyCount + 1)
    23.                         Case Is = "/"
    24.                             Cost = Cost / MultNum(MyCount + 1)
    25.                     End Select
    26.                     End If
    27.             End Select
    28.         Next MyCount

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Adding array items

    Here are some potential problems
    VB Code:
    1. Dim QRow, FormulaCol, QuestionCount As Long
    2. Dim Cost As Single
    3. Dim MultNum(15) As Currency
    4. Dim Formula, Multiplier(14) As String
    5. Dim MyCount, QRemoveNum2, Counts2 As Integer
    In the first line, only QuestionCount is a Long. All the rest are Variants.
    The second and third lines are fine.
    In the fourth line, Multiplier is a string array and Forumla is a Variant
    In the last line, only Counts2 is an Integer, all the rest are Variants. See if this helps any
    VB Code:
    1. Dim QRow As Long, FormulaCol As Long, QuestionCount As Long
    2. Dim Cost As Single
    3. Dim MultNum(15) As Currency
    4. Dim Formula As String, Multiplier(14) As String
    5. Dim MyCount As Integer, QRemoveNum2 As Integer, Counts2 As Integer
    If you dont explicitly declare each, individual variable, as a type, it will default to Variant which does have the potential of causing hard to find issues.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Adding array items

    i thought if you seperate each variable by a comma and put "As (type)" at the end, it made them all the same type, i guess i was wrong

    I tried what you said hack, but it still has the same effect... Round's everything off, or truncates it at the decimal

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Adding array items

    VB Code:
    1. MultNum(MyCount) = Left(Formula, 1)
    2.                     MultNum(MyCount) = frmNewProp.GridJobSpecCustom.TextMatrix(QRow, (MultNum(MyCount) * 2) - 1)
    another issue.. question...
    why set MultNum = Left(forumla,1)
    then set it to something else?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Adding array items

    The formula usually starts off looking like this: "§1*2*3"

    Thats what the variable 'Formula' will equal at the start, but then i use the Right command to get rid of my formula marker "§" so that its just "1*2*3"

    each of the numbers is representing the Answer # that is going to be used. Thats where the first line comes in:
    VB Code:
    1. MultNum(MyCount) = Left(Formula, 1)

    Then i use it, and change it to the actual value of that answer, instead of the number of the question/answer that it is with:
    VB Code:
    1. MultNum(MyCount) = frmNewProp.GridJobSpecCustom.TextMatrix(QRow, (MultNum(MyCount) * 2) - 1)

    That can easily be changed to 1 line, but it was a quick fix and i just never changed it.

    After it gets the first answer, it truncates the formula and goes through again and gets the multiplier, then truncates it again and gets the next answer, and so on until the formula is empty.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Adding array items

    When i remake this as a new project, using only what i need to try to recreate this it works just fine... But in my program its not.. This is confusing

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Thumbs up Re: Adding array items

    I figured it out!

    I have a global variable for each section of the program that holds the total cost for that specific section. Then for this section there's another global array that holds the total cost for all the questions in each row, and at the end of the section of code we've been working on, it goes through that array and adds them together. Then the total of that is what the total for that section equals. But i had those set as Long. I changed both of those to Single, and it works!

    If it wasnt for Static and Hack, i'd probably still be sitting here staring at it wondering whats going on! hehe Thanks you guys!

Posting Permissions

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



Click Here to Expand Forum to Full Width