 |
| Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 24th, 2007, 06:48 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Very Simple code
I am a lecturer in ICT and have been roped into teaching VB - only I don't know VB. I can design etc. Anyway I am trying to code a program - the problem statement is this:
Design a program to read ten numbers from a keyboard and display their sum (addition) on the monitor screen.
This is the code I have developed so far - this is the first time ive ever touche a programming language - i know this code is very simple but please dont laugh at me lol.
Private Sub Command1_Click()
Dim total As Double
Static count As Double
Number = Text1.Text
total = 0
count = 1
If count <= 10 Then
total = total + Number
count = count + 1
ElseIf count > 10 Then
Form1.Print "The sum of the 10 numbers is: "; total
End If
End Sub
Can any body please tell me where I'm going wrong as I truly do not have a clue.
T
|
|

January 24th, 2007, 08:21 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dang you're in a tight spot!
Here is a little working example based on your example.
Good luck to you
Code:
Private Sub Command1_Click()
Static dblTotal As Double
Static intCount As Integer
Dim dblNumber As Double
dblNumber = Val(Text1.Text)
If intCount < 9 Then
dblTotal = dblTotal + dblNumber
intCount = intCount + 1
ElseIf intCount = 9 Then
MsgBox "The sum of the 10 numbers is: " & dblTotal + dblNumber
' reset things...
dblTotal = 0
intCount = 0
End If
End Sub
Woody Z
http://www.learntoprogramnow.com
|
|

January 25th, 2007, 09:03 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
mmm.. I see one problem with your code woody, you are adding the same number 10 times.. maybe you need to add 10 diferents numbers??
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
|
|

January 25th, 2007, 11:28 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by gbianchi
mmm.. I see one problem with your code woody, you are adding the same number 10 times.. maybe you need to add 10 diferents numbers??
Gonzalo
|
It only adds one number with each click. It is not a loop, it is just handling a single click of the button. With each click it takse the "val" of whatever is in the textbox and "accumulates" it. Once you have made 10 clicks it displays the accumulated value, displays it in a message box, and resets the static members to start over again.
The user must change the number in the text box after each click.
Drop the code into a form, add a command button and a text box and run it - it works. It certainly isn't a great implementation - my only purpose was to give the OP a chance to get his/her code working so he/she could move forward a little.
But thanks for the review. I try to test everything I post, and this is one that I did test.
Woody Z
http://www.learntoprogramnow.com
|
|

January 25th, 2007, 01:02 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
.....
let me get this straight: you do NOT know vb (and never programmed before)... and you are teaching VB? What kind of school is that and in which country?
Sorry man, I am *very* perplexed....
"There are two ways to write error-free programs. Only the third one works."
Unknown
|
|

January 25th, 2007, 02:06 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by marcostraf
.....
let me get this straight: you do NOT know vb (and never programmed before)... and you are teaching VB? What kind of school is that and in which country?
Sorry man, I am *very* perplexed....
|
This isn't uncommon - not just with programming, but with all subjects. It is sad. Remembering back a long time I had a geometry class in high school where the teacher did not understand the subject, and could not teach it. Luckily for me it was a subject I was very interested in and had already read the book before the class began, but most of the students floundered. I got a grade of about 45% on my final, and I had to go in and step the teacher through everything line-by-line - after which I got 100%. So what about all the other students who also found the correct solutions but were graded incorrectly? I still feel very "abused" by that situation.
On the other side, I have taught programming at the university level. A Perl class was scheduled and had a full roster but the teacher ended up getting very ill and couldn't teach it. There was no qualified replacement and I was assigned to teach the class with only 2 weeks notice. Fortunately I am a programmer, and the class was a beginning level class so I was able to prepare sufficiently - I would not have done it otherwise. But this sort of thing happens all the time, and if a qualified replacement is not found, it isn't unusual to just get someone who is willing to take it on.
Quote:
quote:
"There are two ways to write error-free programs. Only the third one works."
Unknown
|
Like we say: The only error-free code is the code we don't put in.
Woody Z
http://www.learntoprogramnow.com
|
|

January 25th, 2007, 02:43 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sad, you said? I would say it is worrisome
sorry for this OT guys, I do not mean to offend anyone here, and I am always willing to help everyone. But I am also a strong advocate for good education, this is why I am curious about what tal1481 wrote.
In Italy (where I studied) it is also common to find BAD teachers... but not teachers that do not know the subject at all.
Marco (who is sorry to have brought this up)
"There are two ways to write error-free programs. Only the third one works."
Unknown
|
|

January 25th, 2007, 03:11 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I agree, worrisome as well.
Woody Z
http://www.learntoprogramnow.com
|
|

January 29th, 2007, 09:00 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
sorry Woody about my comment about your code.. my mistake, your code was perfect...
And here (Argentina) we have the same problems with teachers.. I has a OOP class in the uni with a professor that never write a line of code and she also teach java, c++ and vb without using a computer... not even for the final exam!
unfortunately S@@@ happends :(
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
|
|

January 29th, 2007, 11:12 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gonzalo: Don't worry about your comment - if I had made a mistake I certainly would have wanted it corrected.
In defense of teachers, and the system of teaching in general: Most of the schooling I had was by competent teachers who knew the subject they were teaching and took a lot of care to do a good job.
A class like OOP should be taught by an expert who understands not only the mechanics of OOP, but also the motivations and principles. Trying to present this topic "out of the book" isn't very helpful and is a dis-service to the students in the course.
Woody Z
http://www.learntoprogramnow.com
|
|
 |