-
Sep 27th, 2020, 08:13 AM
#1
Thread Starter
New Member
[RESOLVED] Random Numbers math
Last edited by redraider; Sep 27th, 2020 at 09:49 AM.
-
Sep 27th, 2020, 09:39 AM
#2
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
-
Sep 27th, 2020, 09:44 AM
#3
Thread Starter
New Member
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.
-
Sep 27th, 2020, 09:46 AM
#4
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?
-
Sep 27th, 2020, 09:47 AM
#5
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
-
Sep 27th, 2020, 09:48 AM
#6
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.
Last edited by jmcilhinney; Sep 27th, 2020 at 09:51 AM.
-
Sep 27th, 2020, 09:54 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|