|
-
Jan 29th, 2007, 02:13 PM
#1
Thread Starter
Fanatic Member
[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:
For MyCount = 1 To Len(Formula)
Select Case MyCount
Case Is = 1, 3, 5, 7, 9, 11, 13, 15 'This is the numbers
Cost = Cost + 1
MultNum(MyCount) = Left(Formula, 1)
MultNum(MyCount) = frmNewProp.GridJobSpecCustom.TextMatrix(QRow, (MultNum(MyCount) * 2) - 1)
Formula = Right(Formula, Len(Formula) - 1)
Case Is = 2, 4, 6, 8, 10, 12, 14 'This is the multipliers
Multiplier(MyCount) = Left(Formula, 1)
Formula = Right(Formula, Len(Formula) - 1)
End Select
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?
-
Jan 29th, 2007, 02:26 PM
#2
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"
-
Jan 29th, 2007, 02:35 PM
#3
Thread Starter
Fanatic Member
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:
Dim MultNum(15) As Currency
Last edited by PMad; Jan 29th, 2007 at 02:39 PM.
-
Jan 29th, 2007, 02:39 PM
#4
Re: Adding array items
It's hard to solve your problem without being able to see how you have defined your variables, but some variable involved with the calc must be Integer or Long.
-
Jan 29th, 2007, 02:47 PM
#5
Thread Starter
Fanatic Member
Re: Adding array items
Here's all my declarations:
VB Code:
Dim QRow, FormulaCol, QuestionCount As Long
Dim Cost As Single
Dim MultNum(15) As Currency
Dim Formula, Multiplier(14) As String
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:
For MyCount = 2 To ((QuestionCount * 2) - 1)
Select Case MyCount
Case Is = 2
Select Case Multiplier(MyCount)
Case Is = "*"
Cost = MultNum(1) * MultNum(3)
Case Is = "+"
Cost = MultNum(1) + MultNum(3)
Case Is = "-"
Cost = MultNum(1) - MultNum(3)
Case Is = "/"
Cost = MultNum(1) / MultNum(3)
End Select
Case Else
If MyCount < 15 Then
Select Case Multiplier(MyCount)
Case Is = "*"
Cost = Cost * MultNum(MyCount + 1)
Case Is = "+"
Cost = Cost + MultNum(MyCount + 1)
Case Is = "-"
Cost = Cost - MultNum(MyCount + 1)
Case Is = "/"
Cost = Cost / MultNum(MyCount + 1)
End Select
End If
End Select
Next MyCount
-
Jan 29th, 2007, 02:51 PM
#6
Re: Adding array items
Here are some potential problems
VB Code:
Dim QRow, FormulaCol, QuestionCount As Long
Dim Cost As Single
Dim MultNum(15) As Currency
Dim Formula, Multiplier(14) As String
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:
Dim QRow As Long, FormulaCol As Long, QuestionCount As Long
Dim Cost As Single
Dim MultNum(15) As Currency
Dim Formula As String, Multiplier(14) As String
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.
-
Jan 29th, 2007, 02:57 PM
#7
Thread Starter
Fanatic Member
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
-
Jan 29th, 2007, 03:10 PM
#8
Re: Adding array items
VB Code:
MultNum(MyCount) = Left(Formula, 1)
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"
-
Jan 29th, 2007, 03:17 PM
#9
Thread Starter
Fanatic Member
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:
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:
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.
-
Jan 29th, 2007, 03:59 PM
#10
Thread Starter
Fanatic Member
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
-
Jan 29th, 2007, 04:06 PM
#11
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|