[TUT, VB6] Making a "Magic 8 Ball"

Discussion in 'Archives' started by Blupig, Aug 16, 2007.

[TUT, VB6] Making a "Magic 8 Ball"
  1. Unread #1 - Aug 16, 2007 at 4:17 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT, VB6] Making a "Magic 8 Ball"

    MAGIC 8 BALL TUT

    Let's begin with what control settings you need.

    -Label (1) = Default settings
    -TextBox (1) = Default settings
    -Command Button (1) = Default Settings

    -Standard .EXE

    Place your controls on the form in a comfortable sense.
    To do that I guess I should tell you what they are going to be used for:
    -The label is for the 8-Ball's answer
    -The textbox is for the question
    -The Command button is to answer the question

    Change the names of the controls or other settings to your likings before we start.
    ---------------------------------------------------------
    OK!!! Let's start the programming! This is a very simple application, and is *of course* designed for beginners at VB6. Please do not flame this tutorial, it is only to help out. If you're going to post anything at all, make it a Thanks or constructive comments. Nothing negative and no spam.

    CODE TIME.
    Ok, here we have the code for your command button, or answer button. This is the only code of the application.
    Code:
    Private Sub Command1_Click()
          Answer = Array("I'm afraid so.", "I really hope not.", "How the hell am I supposed to know?", "That's a hard one. Come back later.", "It could very well be so!")
    
          Randomize
          n = Int(5*Rnd)
          label1.caption = Answer(n)
    End Sub
    
    EXPLANATION
    Let's begin the explanation with the last line of code (forgive me for starting backwards ;)):
    Code:
    Text1.Text = Answer(n)
    
    This instruction is basically telling label1 to display the answer for the question you asked.

    Now a bit further up we start having the complicated stuff, with all the variables and such.
    Code:
          Answer = Array("I'm afraid so.", "I really hope not.", "How the hell am I supposed to know?", "That's a hard one. Come back later.", "It could very well be so!")
    
          Randomize
          n = Int(5*Rnd)
    
    I'll explain this all at once to make it more simple.
    At the top we have the "Answer =" function. What this is, is an instruction telling the program what the possible answers can be. To shorten it up when we tell label1 to display the answer, we an "abreviation" if you will, to make it shorter to type the second time. It also makes it look less like a mess.

    That next part, beginning with the "Randomize". That is well, the randomizing part, to tell the application to randomize the answer.

    Now we have probably the most complicated part: The variable.
    Each answer in this program corresponds to a number. In this case we have 0,1,2,3,4. Using the variable, we can identify and show one of these answers using number identification for them. The Rnd function helps us choose a number between 0.0 and 1.0 - This is a problem since we don't want decimals, we want full numbers. That's also to ease the process and render it less confusing. Using this, we can have our 5 answers. That's where the "Int" comes in. This useful function rounds off the random number chosen between 0.0 and 4.0 (now it's 4.0 due to the multiplication by 5 of the "Rnd function between the brackets) so it doesn't confuse the program, and the identity of our answers are nice and solid whole numbers. All that is packed in to choose the answer, therefore label1.caption = Answer(n).

    Thanks and I hope you enjoyed my tutorial.

    Credits
    This was made from my knowledge, which has been built up from many sources and many tutorials, so if I listed all of them I could be here for ages...
    So thanks so everyone on Sythe, Cruels and Fagex for their help.

    WTF SOURCE CODE!?
    I don't have a source code for you because I don't think giving the answers to you will help you learn. Read the tut and learn something.
    I do have a compiled .exe of this project though, download it here (.rar).
     
  3. Unread #2 - Aug 16, 2007 at 5:16 PM
  4. Monster Hack
    Joined:
    Nov 19, 2005
    Posts:
    59
    Referrals:
    2
    Sythe Gold:
    0

    Monster Hack Member

    [TUT, VB6] Making a "Magic 8 Ball"

    Um.
    Option Explicit isn't complicated. It makes perfect sense.
    The Option Explicit statement to force explicit declaration of all variables, in MS' words.

    -Mh
     
  5. Unread #3 - Aug 16, 2007 at 7:01 PM
  6. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT, VB6] Making a "Magic 8 Ball"

    Whoops. Sorry. My dad tought me a good bit of VB6 (he's awesome ownage in all languages, even assembly :eek:) and he said it was complicated and to not worry about it. Anyways, thanks for the comment I'm sure it will help others.
     
  7. Unread #4 - Aug 16, 2007 at 7:38 PM
  8. jdsfighter
    Joined:
    Jan 21, 2007
    Posts:
    603
    Referrals:
    0
    Sythe Gold:
    0

    jdsfighter Forum Addict
    Visual Basic Programmers

    [TUT, VB6] Making a "Magic 8 Ball"

    What I think is rather odd is you used option explicit, but you didn't define the variable Answer. This will present an error if you don't define it.
     
  9. Unread #5 - Aug 16, 2007 at 10:41 PM
  10. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT, VB6] Making a "Magic 8 Ball"

    Actually, it doesn't.
     
  11. Unread #6 - Aug 17, 2007 at 3:37 AM
  12. Jazz00006
    Joined:
    Aug 26, 2005
    Posts:
    807
    Referrals:
    0
    Sythe Gold:
    0

    Jazz00006 Apprentice
    Visual Basic Programmers

    [TUT, VB6] Making a "Magic 8 Ball"

    You fail.
    Answer nor n were declared. Me thinks you just pasted option explicit at the top of your code when you pasted it here otherwise you WOULD have got errors.
    [​IMG]
    Code:
    Option Explicit
    Private Sub Command1_Click()
          Dim Answer
          Dim n As Integer
          Answer = Array("I'm afraid so.", "I really hope not.", "How the hell am I supposed to know?", "That's a hard one. Come back later.", "It could very well be so!")
          Randomize
          Label1.Caption = Answer(Int((UBound(Answer) + 1) * Rnd))
    End Sub
     
  13. Unread #7 - Aug 17, 2007 at 9:35 AM
  14. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT, VB6] Making a "Magic 8 Ball"

    I should probably double check my work before posting it...Now I know why my .exe worked but not this. Stupid option explicit. My dad told me to always put it up, kinda like and <html> tag for html...I guess I won't be doing that anymore. I removed the option explicit from my tut and now things work.
     
  15. Unread #8 - Aug 19, 2007 at 7:45 AM
  16. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [TUT, VB6] Making a "Magic 8 Ball"

    Option Explicit is a must have for any smart programmer. .Net has it enabled by default so no code at the top to turn it on.

    If you don't declare your variables they automatically assume the type 'variant', and it would be pretty obvious that declaring an integer, or a string, would be more efficient.

    Java, C++, all of those languages I just love don't automatically assign un-declared variables, they tell you to do something about em ;)

    EDIT: ROFL - "Now we have probably the most complicated part: The variable.".
    what I just outlined isn't flaming or spam, because even an ameteur programmer in his/her right mind knows that a variable isn't complicated. Objects are a little more complicated, but definitely not primitive data-types/variables.
     
  17. Unread #9 - Aug 20, 2007 at 5:08 PM
  18. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT, VB6] Making a "Magic 8 Ball"

    See Swan, this is a Tut for noobs. People who don't know what the fuck they're doing.
     
  19. Unread #10 - Aug 29, 2007 at 2:56 AM
  20. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [TUT, VB6] Making a "Magic 8 Ball"

    Blupig - Even noobs should know the basics.
     
  21. Unread #11 - Aug 29, 2007 at 3:26 AM
  22. Cruel__Machine
    Referrals:
    100

    Cruel__Machine Guest

    [TUT, VB6] Making a "Magic 8 Ball"

    That's the worst part of VB. It's too loose... :( (like Swan)
    I used to use "Dim x, y As Long" not knowing that x would become a Variant. And I'd also have huge memory leaks using undeclared variables in loops.
    But at least it doesn't just execute a random function when you use an undefined function. Haha
     
  23. Unread #12 - Aug 29, 2007 at 4:50 AM
  24. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [TUT, VB6] Making a "Magic 8 Ball"

    define loose a little farther ;)

    Lolol, I admire your efforts for trying to teach ... but what is the use of something like this exactly? Teaching the use of the Rand Function? :D

    And even a noob can learn languages like C++, it just takes a little longer for them to comprehend the basics because they haven't been programming for a while.
     
< Fast money akein guide | Buying 100k yew logs! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site