Help with correcting a loop function

alkc
02-14-2008, 01:20 PM
Hello all

I need some help with fixing a function that isn't working:confused:
I am an Engineer and not a Programmer but have been learning VB6 and will appreciate any help with this.

I have 3 listboxes that are labelled as follow
+ 1st "Number of items" - must be populated from 1 to 30
+ 2nd "H2" - A value that I start with and increase by 0.5 until I complete 30 trials
+ 3rd "P" - A value the needs calculating from the equation below


P = ((H1 * a2) - (d * H2)) / a1

I have the values of "a1", "a2", "H1" and "d" that are in textboxes
I need to assume a starting value for H2 then increase H2 incrementally by 0.5 untill I do 30 iterations

I tried to do write the few lines below but I only succeeded in populating is the first listbox from 1 to 30

The errors I am having:
- the second listbox only adds the first value and repeate the same value for 30 times but fail to add 0.5 to each H2 value
- the third listbox only calculate the P from the equation and repeat the same number for 30 times

Here is my code

Public Function el_us()
Dim P As Currency
Dim a1 As Currency
Dim a2 As Currency
Dim H1 As Currency
Dim H2 As Currency

Dim sa As Currency
Dim denliq As Currency
Dim denmix As Currency
Dim N As Long
Dim no As Long
Dim i As Long
Dim ni As Long
Dim No_of_iterations As Long
H3 = Text1.Text
sa = Text2.Text
denliq = Text3.Text
denmix = Text4.Text
a1 = sa * 14
a2 = denliq - denmix
H2 = Text5.Text
H1 = H3 - H2
N = 0.5 'This is the number that I want to increase H2 by
No_of_iterations = 30

'will need to use the starting value for H2 from Text5.Text to calculate P and increase H2 by 0.5 until it reaches the 30 trials.

For i = 0 To No_of_iterations
For ni = 0 To N
no = 0
dP = ((H1 * a2) - (denmix * (H2 + ni))) / a1
List1.AddItem (no + i)
List2.AddItem (H2 + ni)
List3.AddItem (dP)
Next ni
Next i

End Function


Could any one help me,

Thanks

Gruff
02-14-2008, 02:49 PM
Hi Alkc,

Off hand I see you have two loops. One nested inside the other.
You do not need the inner loop. You simply need to increase H2 by .05 on each iteration.
I also see several unnecessary variables.



' (Untested Code)

For i = 0 to No_Of_Iterations
dP = ((H1 * a2) - (denmix * H2)) / a1
List1.additem i
List2.additem H2
List3.additem dP
H2 = H2 + N
Next i


Should do it.

alkc
02-14-2008, 03:43 PM
Gruff

Thanks for the response,

I tried to replace the code with your recommendation but for some reason the H2 increment isn't working.

The code code just repeat the same figure in the second and third listboxes

loquin
02-14-2008, 04:12 PM
Also.

You've Defined N as long (an integer type) but you initially set it to 0.5 (a floating point type)

Offhand, I believe that VB will coerce the folating point value to an integer type, which will probably make it equal to 0. Then, as you're adding N on each iteration to a value that is intially set to 0, ...

Change the definition of N to

Dim N as Double

(or Currency)

BTW. You might want to take a look at the *** when you get a chance.

Gruff
02-14-2008, 04:15 PM
You are defining N as a 'long' data type. This can only hold whole numbers.
define it as a 'Double' instead. Then it will be able to hold a floating point number (0.50). I imagine it is holding a value of 0.
Also I do not see where you are defining the datatype for dP.

If you use 'Option Explicit' at the top of your forms and modules VB will make sure you define all your variables.

Also. You should learn how to use your debug mode and tools.
If you step through your program in debug mode you can see the variable values as they are assigned simply by hovering the mouse cursor over the variable names. Very powerful tools.

Finally I am not sure why you are using currancy as the data types for some of your variables.

~T

alkc
02-14-2008, 04:26 PM
It's working :D
I changed the definition of N to double,
Thank you Gruff and loquin.

I will try to clean up the code and go over more tutorials ;)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum