Arithmetic and nested loops
I've done a little bit of digging and haven't found any solutions that come close to what I am looking for.
The idea is that I will have 3 list boxes. The first labeled lstF1, the second labeled lstF2, the third labeled lstResult.
What I want to do is take a value from the first list and then with that value perform some arithmetic against *each* of the values in lstF2 and post the results in the lstresult. Then move to the second item in lstF1 and do the whole thing again.
I haven't declared variable types yet because I think that the arrays from the list boxes are pulled in as strings and I would prefer them to be integers. I will work through that later.
So far this is what I have.
Code:
'' inside of a command button
Dim f1
Dim f2
Dim item()
Dim items()
Dim IM3a
Dim IM3b
Dim IM3c
Dim IM3d
'Read through list''''''''''''''''''''''''''''''''''''''
For i = 0 To lstF1.ListCount - 1
ReDim Preserve item(i)
item(i) = lstF1.List(i)
f1 = item(i)
'Start nested loop'
For x = 0 To lstf2count - 1
ReDim Preserve items(x)
items(x) = lstF2.List(x)
f2 = items(x)
IM3a = 2 * f1 + f2
IM3b = 2 * f1 - f2
IM3c = 2 * f2 + f1
IM3d = 2 * f2 - f1
With lstResult
.AddItem IM3a
.AddItem IM3b
.AddItem IM3c
.AddItem IM3d
End With
Next'' should be next items(x)
Next'' should be next items(i)
End Sub
What I am seeing is that I never seem to enter the nested loop. If I put a break inside of the nested loop I never enter it.
Any help would be much appreciated.
Re: Arithmetic and nested loops
lstf2count should be lstf2.listcount
That's the FIRST thing I see
Re: Arithmetic and nested loops
Gaaah!!
Thanks Mate, Been looking at this for a couple of hours and can't believe I missed that. once I correct that everything seems to be coming through fine.
Re: Arithmetic and nested loops
Gaaah!!
Thanks Mate, Been looking at this for a couple of hours and can't believe I missed that. once I correct that everything seems to be coming through fine.
Re: Arithmetic and nested loops
Shouldn't this line :
For x = 0 To lstf2count - 1
be written as :
For x = 0 To lstf2.ListCount - 1
The way it is, your x loop loops from 0 to -1 so it does not execute.
Re: Arithmetic and nested loops
Shouldn't this line :
For x = 0 To lstf2count - 1
be written as :
For x = 0 To lstf2.ListCount - 1
The way it is, your x loop loops from 0 to -1 so it does not execute.
Re: Arithmetic and nested loops
Quote:
Originally Posted by
FrogFumes
I haven't declared variable types yet because I think that the arrays from the list boxes are pulled in as strings and I would prefer them to be integers. I will work through that later...
Convert them while adding them to your item() array:
Code:
...
Dim item() As Long
Dim items() As Long
...
item(i) = CLng(lstF1.List(i))
...
items(x) = CLng(lstF2.List(x))
Re: Arithmetic and nested loops
Thanks LaVolpe.
Hadn't seen this one before. I plan on doing this but can you help me understand the benefit other than the need for declared variables.
Re: Arithmetic and nested loops
Benefit? You mentioned you wanted to treat the list items as numeric? Always a superb idea to do arithmetic on numeric values vs text values
The benefit of converting the text to numeric is for calculations. VB will attempt to convert string/text to numeric if that string is used in a calculation, but there are 2 immediate downsides: 1) slower to convert some string to numeric several times over vs. just doing it once & 2) always a possibility that VB doesn't convert the string properly (depending on how string is formatted); whereas by using VB's conversion functions, you control that. Kinda hard to misinterpret a numeric value.
You mentioned integer also, but nowadays, never really know if someone says Integer, they are talking 16bit or 32bit. With VB6, Integers are 16bit and Longs are 32bit
Without declaring variable types, VB uses Variant as the variable type. Variants are slow (relatively speaking)
Re: Arithmetic and nested loops
Moderator Action: Merged duplicated threads together. Please stick to one thread per topic.
Re: Arithmetic and nested loops
Quote:
Originally Posted by
FrogFumes
Been looking at this for a couple of hours...
Using Option Explicit, VB would have alerted you to that error the first time you tried to run the code.
Also, instead of ReDimming the arrays inside the loop, populate them prior to your main loop.
Code:
Dim F1() As Long, F2() As Long
Dim N1 As Long, N2 As Long
With lstF1
ReDim F1(.ListCount - 1)
For N1 = 0 To .ListCount - 1
If IsNumeric(.List(N1)) Then F1(N1) = CLng(.Left(N1))
Next N1
End With
With lstF2
ReDim F2(.ListCount - 1)
For N2 = 0 To .ListCount - 1
If IsNumeric(.List(N2)) Then F2(N2) = CLng(.Left(N2))
Next N2
End With
With lstResult
For N1 = 0 To UBound(F1)
For N2 = 0 To UBound(F2)
.AddItem CStr(2 * F1(N1) + F2(N2)) ' IM3a
.AddItem CStr(2 * F1(N1) - F2(N2)) ' IM3b
.AddItem CStr(2 * F2(N2) + F1(N1)) ' IM3c
.AddItem CStr(2 * F2(N2) - F1(N1)) ' IM3d
Next N2
Next N1
End With
Re: Arithmetic and nested loops
I didn't look at your code extremely close, but from the text in your first post, it sounds like you're trying to do a bit of linear (matrix) algebra. If that's the case, let me know and I've got all kinds of linear algebra routines in VB6. Are you just trying to multiply two matrices? If so, that's an easy one.
My routines won't work from ListBoxes though. They'll be expecting two dimensional arrays of doubles, but it should be easy to unload and reload your textboxes into arrays. That'd be the way I'd tend to do it anyway.
Best of luck.
Elroy
p.s. If you're just trying to multiply two vectors, that's easy too (even easier). Heck, I've got routines to do canonical correlations, which is the generalization of multivariate multiple linear regression (including ANOVA with coded vectors).