Vous êtes sur la page 1sur 7

Log in or Sign up

Forums Site Archives Archives

Sign in to Post Thread Tools

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


#1 - Aug 16, 2007 at 4:17 PM

Joined: Nov 23, 2006


Posts: 6,988
Referrals: 16
Sythe Gold: 1,524
BEEF TOILET

[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'

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'

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).
Share

#2 - Aug 16, 2007 at 5:16 PM

Monster Hack
Member
Joined: Nov 19, 2005
Posts: 59
Referrals: 2
Sythe Gold: 0

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

Um.

it's pretty complicated so don't worry about it right now.

Option Explicit isn't complicated. It makes perfect sense.


The Option Explicit statement to force explicit declaration of all variables, in MS' words.

-Mh
_____________________________
[IMG]
Share

#3 - Aug 16, 2007 at 7:01 PM

Joined: Nov 23, 2006


Posts: 6,988
Referrals: 16
Sythe Gold: 1,524
BEEF TOILET

[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
) and he said it was complicated and to not worry about it. Anyways, thanks for the comment I'm sure it will
help others.
Share

#4 - Aug 16, 2007 at 7:38 PM

Joined: Jan 21, 2007


jdsfighter Posts: 603
Forum Addict Referrals: 0
Sythe Gold: 0

[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.
_____________________________
Share

#5 - Aug 16, 2007 at 10:41 PM

Joined: Nov 23, 2006


Posts: 6,988
Referrals: 16
Sythe Gold: 1,524
BEEF TOILET

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

Actually, it doesn't.
Share

#6 - Aug 17, 2007 at 3:37 AM

Joined: Aug 26, 2005


Jazz00006 Posts: 807
Apprentice Referrals: 0
Sythe Gold: 0

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

blupig said: ↑
Actually, it doesn't.

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'
Randomize
Label1.Caption = Answer(Int((UBound(Answer) + 1) * Rnd))
End Sub

Share
#7 - Aug 17, 2007 at 9:35 AM

Joined: Nov 23, 2006


Posts: 6,988
Referrals: 16
Sythe Gold: 1,524
BEEF TOILET

[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.
Share

#8 - Aug 19, 2007 at 7:45 AM

Joined: Jan 23, 2007


Swan Posts: 4,957
When They Cry... Referrals: 0
Sythe Gold: 0

[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.
_____________________________

[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT

Share

#9 - Aug 20, 2007 at 5:08 PM

Joined: Nov 23, 2006


Posts: 6,988
Referrals: 16
Sythe Gold: 1,524
BEEF TOILET

[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.
Share

#10 - Aug 29, 2007 at 2:56 AM

Swan
When They Cry...
Joined: Jan 23, 2007
Posts: 4,957
Referrals: 0
Sythe Gold: 0

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

Blupig - Even noobs should know the basics.


_____________________________

[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT

Share

#11 - Aug 29, 2007 at 3:26 AM

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
Share

#12 - Aug 29, 2007 at 4:50 AM

Joined: Jan 23, 2007


Swan Posts: 4,957
When They Cry... Referrals: 0
Sythe Gold: 0

[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?

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.
_____________________________

[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT

Share

(You must log in or sign up to reply here.)

< Fast money akein guide | Buying 100k yew logs! >
Users viewing this thread

1 guest

SytheXenModern Sythe Discord Follow us r/SytheMarket Subscribe to us Follow us Like us C ontact Us Help

Forum software by XenForo™ ©2010-2016 XenForo Ltd. Terms and Rules

Vous aimerez peut-être aussi