Results 1 to 7 of 7

Thread: [RESOLVED] Random Numbers math

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    4

    Resolved [RESOLVED] Random Numbers math

    Jjjir
    Last edited by redraider; Sep 27th, 2020 at 09:49 AM.

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,189

    Re: Random Numbers math

    If someone were to ask you what the following line of code is doing and accomplishing, what would your response be?

    Code:
    If CBool((CInt(ListBox1.Items(i)) + sum)) Then

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    4

    Re: Random Numbers math

    I was just showing the concept of my code. But that line right there you posted is what I'm having trouble with.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,587

    Re: Random Numbers math

    ANY time you are doing a programming problem, you should be working out the logic first and then writing the code to implement that logic. It is very clear that you haven't done that here:
    Code:
                If CBool((CInt(ListBox1.Items(i)) + sum)) Then
                    sum = CInt(ListBox1.Items(1))
                End If
    because that code is complete nonsense. What exactly is that If statement supposed to be doing? If I gave you a list of numbers to add, what would be the logic you would follow to do it? Would you be doing something for a number in the list only if some condition was true? I would think not, so why would you write code that does that? Pick up a pen and paper and write down the logic first. Do the process manually, following the steps you wrote down. When it works, you know what the code has to do and you can write code to do it. If you just pluck code out of thin air with no real idea of what that code has to do - the steps, not just the result - then why would it work?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,332

    Re: Random Numbers math

    Yeah, and I think OB1 had a good point: What were you expecting that to do? If you can't explain it, then you need to step back and think about it a bit more. A sum is just adding all the numbers, so what is the condition supposed to be doing there? When you sum something, do you only add it if some condition is met? If so, then a condition is needed, but you made it sound like you add it in for all cases.

    EDIT: I was too slow.
    My usual boring signature: Nothing

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,587

    Re: Random Numbers math

    Also, you really ought to be using a For Each loop here. Unless you've been specifically instructed to do otherwise, do that.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,587

    Re: [RESOLVED] Random Numbers math

    Instead of deleting the question, how about you post your solution and maybe help someone else with a similar problem?

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