Results 1 to 12 of 12

Thread: Arithmetic and nested loops

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    5

    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.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Arithmetic and nested loops

    lstf2count should be lstf2.listcount

    That's the FIRST thing I see

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    5

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    5

    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.

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    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.

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    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.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Arithmetic and nested loops

    Quote Originally Posted by FrogFumes View Post
    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))
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    5

    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.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Arithmetic and nested loops

    Moderator Action: Merged duplicated threads together. Please stick to one thread per topic.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Arithmetic and nested loops

    Quote Originally Posted by FrogFumes View Post
    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

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    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).

Tags for this Thread

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