![]() |
![]() |
![]() |
|
|
||
|
EXTRA INFO Object Orientated programming (OOPS)
|
WELL DONE !! lesson 4 - AIM - To Understand How to Use MsgBox, Indent and REMs |
|
|
The answer is Sub Main() Dim Choice as Byte Choice = msgbox ("Hello I'm a message box are you?",vbYesNo) End Sub Close any existing work and open a new Visual Basic (VB) Programming Environment. Start >> Programs>> Visual Basic 6. Open a new Standard EXE file and remove the form. Now add a module. If your unsure how to do this click on the 'Getting Started with VB Link' on the left. You can open up your work from last lesson but it is good practice to start again. If you open your work from last lesson it should look like the window below. If not close and open a new VB6 program. Change the type of variable in the declaration to integer. This is just to get you changing things around in VB. Byte is actually more correct as it uses less storage space and we know our answer will be below 8 however, integer will suffice.
If your window doesn't look like the one above you may need to open the Module folder and then double click on the module. See below
We have a message box that offers the user 2 buttons to click - Yes and No. Each one of these 'message box buttons' returns an integer (whole number) value between 1 and 7 see table below.
Using the table above, our message box can return a value of 6 or 7. If the user chooses the Yes button a value of 6 will be stored in our variable Choice and If the user chooses No the value sent to Choice will be 7. We can use this in our program to determine what the user wants to do. For this purpose we will need a decision or 'IF' statement. Option Explicit Sub Main() Dim Choice As Integer Choice = MsgBox("Would you like to play a game?", vbYesNo + 32 ) If Choice = 6 Then The code in bold is the existing code if you are working with your opened project from yesterday. Just copy and paste the non-bold code into the same place in your program. If starting from scratch paste all the code into an empty module. Make sure you don't have OPTION EXPLICIT twice at the top! Run the program. If it doesn't work or there are errors delete the whole code and copy and paste all of the above. Run again. Note that we have used the 'default' msgBox for the second message. This does not require a variable since it only has one button - OK. The code is now becoming more complicated and looks quite messy. You need to develop the habit of indenting code so it is easily readable. Make your code look like the picture below. Use the tab key to indent.
There are many reasons for indenting code. It makes code easier to read, less spaghetti like and it is easier to see errors. I can clearly see the beginning of the program and the end statement. Also the decision statement If...then....else....end if is clearly correct and has all the needed parts. Note that in the decision (if) statement I do not need to specify that Choice =7, because there are only 2 buttons. If the user doesn't click Yes by defaut they must click No. If they close the msgbox by clicking the cross the program ends anyway. There is one more thing needed and these are REMs or comments through the code in PLAIN English that tell what the code is doing. You must indent your code correctly and add comments or REMs. In a real programming environment there are often many programmers working on the same code so you must explain what the code does. Even if it is only you working on the code when you come back to modify the program, in a few weeks or months, you will not remember what the code does so it is essential. Also, it is practically impossible for a teacher to fully understand what you are trying to do without comments of REMs. All REMs must begin with the apostrophe key on the keyboard next to the Enter key ( ' ) anything that follows this will be ignored by the compiler i.e. the program knows it isn't code and wont convert it to machine code. If you go onto a new line then you need to type a new apostrophe. Make your code look like the picture below by further indenting and adding the REM comments.
Now save your work. The code now looks neater and makes a lot more sense. Read through the REMs and try to understand what is happening in each line of code. Your not expected to fully understand just get an idea of what the code is doing. Now lets make something happen when the user clicks Yes or Nol. Copy the code below and then indent and place the REMs through the code. Again the code you already have is shown in bold. Add the new lighter code to what you already have in the correct place OR delete the code you have and copy all the code. You will need to indent all the code again. NOTE: Code should not 'run' on to 2 lines. Each line of code below should appear on ONE line in the VB module window. Option Explicit 'begin program 'declare variable choice to hold message box return number 'Use If statement to find out which button selected and display an appropriate message Else 'No button clicked 'end the program Your final indented and REM commented code should look like the picture below.
Run your program. Don't worry if your code didn't work or you don't 'get it'. Programming takes time to become comfortable with. It's the 'panic' that sets in that stops you learning. It is very different, there is sooo much to understand and it just doesn't make sense! All you should be able to do right now is
We are now going to look at how to get Input from a user. If you understand the concepts above click the Quiz Me button - if not go back and re-read the last couple of lessons. |
||