472,358 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

Homework help for If Then Else statements Could someone check my work?

Amy
Hi,
I have 6 If Then Else statements I was supposed to write. I did so but I
know that they have to be wrong because they all look the same. Could
someone take a look at them and point me in the right direction about what I
am not doing correctly?
1.. Write an If Then Else statement that displays the string "Pontiac" in
the CarMakeLabel control if the CarTextBox control contains the string
"Grand Am" (in any case).
(In this one I am trying to make "Gand Am" be true in any case so I used the
UCase to just change all of the letters to uppercase before comparing it. Is
that right?)



Dim strCar as String

strCar = UCase(Me.CarTextBox.Text)

If strCar = "Grand Am" Then

Me.CarMakelabel.Text = "Pontiac"

Else

Me.CarMakeLabel.Text = "Input Error"

End If

2.. Write an If. Then. Else statement that displays the string "Entry
Error" in the MessageLabel control if the intUnits variable contains a
number that is less than 0; otherwise, display the string "Valid Number".
(this one seems ok to me)

Dim intUnits as Integer

intUnits = Val(InputBox("Enter a number equal to or greater than 0",
"Number"))

If intUnits < 0 Then

Me.MessageLabel.Text = "Entry Error"

Else

Me.Messagelabel.Text = "Valid Number"

End If

3.. Write an If. Then. Else statement that displays the string "Reorder"
in the MessageLabel control if the sngPrice variable contains a number that
is less than 10; otherwise, display the string "OK".
(I would say that this one seems ok but the sng worries me a bit, isn't
that a floating point number? and the number 10 is a whole number. Do I need
to do some kind of calulation to change it?)

Dim sngPrice as Single

sngPrice = Val(InputBox("How many items are left on the shelf?", "Number"))

If sngPrice < 10 Then

Me.MessageLabel.Text = "Reorder"

Else

Me.MessageLabel.Text = "OK"

End If

4.. Write an If Then Else Statement that assigns the number 10 to the
sngBonus variable if the sngSales variable contains a number that is less
than or equal to $250; otherwise, assign the number 50.
(This looks ok??? Does the currency sign make a difference?)

Dim sngSales, sngBonus as Single

If sngSales <= 250 Then

sngBonus = 10

Else

sngBonus = 50

End If



5.. Write an If. Then.. Else statement that displays the number 25 in the
ShippingLabel control if the strState variable contains the string "Hawaii"
(in any case); otherwise display the number 50.
(I am a little confused about the comapring a string inany case. Does the
Ucase make that happen. So it does not matter how someone enters it. I just
make all of the letters uppercase and then see if they match?)

Dim strState as String

strState = Me.ShippingTextBoxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabel.Text = "25"

Else

Me.ShippingLabel.Text = "50"

End If


Nov 20 '05 #1
3 3807
"Amy" <ac*********@hotmail.com> wrote...
I have 6 If Then Else statements I was supposed to write. I did so but I
know that they have to be wrong because they all look the same. Could
someone take a look at them and point me in the right direction about what I am not doing correctly?
Hi Amy... you've only posted five... maybe that was part of the test :-)

Two things you need to keep in mind "form" and "function." You don't
actually need the strCar variable unless the test suggests you assign a
variable and the "me." isn't required either. UCase() should be avoided in
favor of .ToUpper as a general rule. And while UCase is capitalizing the
text you are testing against a mixed case version of the string so it will
never match.
Dim strCar as String

strCar = UCase(Me.CarTextBox.Text)

If strCar = "Grand Am" Then

Me.CarMakelabel.Text = "Pontiac"

Else

Me.CarMakeLabel.Text = "Input Error"

End If
Try:

Dim strCar As String
strCar = CarTextBox.Text.ToUpper

If strCar = "GRAND AM" Then
CarMakelabel = "Pontiac"
Else
CarMakelabel = "Input Error"
End If
The first two lines of which can be further reduced to one:

Dim strCar As String = CarTextBox.Text.ToUpper
You should immediately set Option Strict on. This one should have given you
an error about implict conversion. Val() has some side-effects which you
should be aware of. You can type "123ABCDE" into the InputBox for instance
and it will work. So would "ABC" which your code will report is a valid
number.
Dim intUnits as Integer

intUnits = Val(InputBox("Enter a number equal to or greater than 0",
"Number"))

If intUnits < 0 Then

Me.MessageLabel.Text = "Entry Error"

Else

Me.Messagelabel.Text = "Valid Number"

End If
I've left Val() in but I explicitly cast the result to an Integer

Dim intUnits As Integer = CInt(Val(InputBox("Enter a number equal to
or greater than 0", "Number")))

If intUnits < 0 Then
MessageLabel.Text = "Entry Error"
Else
MessageLabel.Text = "Valid Number"
End If
Yes Single is a floating point value. Same problem with Val() and the
constant value 10 is actually an Integer.
Dim sngPrice as Single

sngPrice = Val(InputBox("How many items are left on the shelf?", "Number"))
If sngPrice < 10 Then

Me.MessageLabel.Text = "Reorder"

Else

Me.MessageLabel.Text = "OK"

End If
Try:

Dim sngPrice As Single = CSng(Val(InputBox("How many items are left
on the shelf?", "Number")))

If sngPrice < 10.0F Then
MessageLabel.Text = "Reorder"
Else
MessageLabel.Text = "OK"
End If
Nothing is setting sngSales on this one and while you are assigning an
Integer constant to sngBonus it won't display anywhere.
Dim sngSales, sngBonus as Single

If sngSales <= 250 Then

sngBonus = 10

Else

sngBonus = 50

End If
Try:

Dim sngBonus As Single
Dim sngSales As Single = CSng(Val(InputBox("Sales", "Number")))

If sngSales <= 250.0F Then
sngBonus = 10.0F
Else
sngBonus = 50.0F
End If

MessageLabel.Text = sngBonus.ToString
Similar to the first example. The use of UCase is discouraged but you have
to test against "HAWAII" not "Hawaii" if you want it to match.
Dim strState as String

strState = Me.ShippingTextBoxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabel.Text = "25"

Else

Me.ShippingLabel.Text = "50"

End If


Try:

Dim strState As String = ShippingTextBoxl.Text.ToUpper

If strState = "HAWAII" Then
ShippingLabel.Text = "25"
Else
ShippingLabel.Text = "50"
End If
Nov 20 '05 #2
Amy
Hi Tom,
Thanks for taking a moment to help me! I am such a beginner that I am not
sure what the "option explicit" thing is on the second question.
Who knew If then Else statements could be so confusing!! :) Amy
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
"Amy" <ac*********@hotmail.com> wrote...
I have 6 If Then Else statements I was supposed to write. I did so but I
know that they have to be wrong because they all look the same. Could
someone take a look at them and point me in the right direction about
what I
am not doing correctly?
Hi Amy... you've only posted five... maybe that was part of the test :-)

Two things you need to keep in mind "form" and "function." You don't
actually need the strCar variable unless the test suggests you assign a
variable and the "me." isn't required either. UCase() should be avoided

in favor of .ToUpper as a general rule. And while UCase is capitalizing the
text you are testing against a mixed case version of the string so it will
never match.
Dim strCar as String

strCar = UCase(Me.CarTextBox.Text)

If strCar = "Grand Am" Then

Me.CarMakelabel.Text = "Pontiac"

Else

Me.CarMakeLabel.Text = "Input Error"

End If
Try:

Dim strCar As String
strCar = CarTextBox.Text.ToUpper

If strCar = "GRAND AM" Then
CarMakelabel = "Pontiac"
Else
CarMakelabel = "Input Error"
End If
The first two lines of which can be further reduced to one:

Dim strCar As String = CarTextBox.Text.ToUpper
You should immediately set Option Strict on. This one should have given

you an error about implict conversion. Val() has some side-effects which you
should be aware of. You can type "123ABCDE" into the InputBox for instance and it will work. So would "ABC" which your code will report is a valid
number.
Dim intUnits as Integer

intUnits = Val(InputBox("Enter a number equal to or greater than 0",
"Number"))

If intUnits < 0 Then

Me.MessageLabel.Text = "Entry Error"

Else

Me.Messagelabel.Text = "Valid Number"

End If
I've left Val() in but I explicitly cast the result to an Integer

Dim intUnits As Integer = CInt(Val(InputBox("Enter a number equal

to or greater than 0", "Number")))

If intUnits < 0 Then
MessageLabel.Text = "Entry Error"
Else
MessageLabel.Text = "Valid Number"
End If
Yes Single is a floating point value. Same problem with Val() and the
constant value 10 is actually an Integer.
Dim sngPrice as Single

sngPrice = Val(InputBox("How many items are left on the shelf?", "Number"))

If sngPrice < 10 Then

Me.MessageLabel.Text = "Reorder"

Else

Me.MessageLabel.Text = "OK"

End If


Try:

Dim sngPrice As Single = CSng(Val(InputBox("How many items are

left on the shelf?", "Number")))

If sngPrice < 10.0F Then
MessageLabel.Text = "Reorder"
Else
MessageLabel.Text = "OK"
End If
Nothing is setting sngSales on this one and while you are assigning an
Integer constant to sngBonus it won't display anywhere.
Dim sngSales, sngBonus as Single

If sngSales <= 250 Then

sngBonus = 10

Else

sngBonus = 50

End If
Try:

Dim sngBonus As Single
Dim sngSales As Single = CSng(Val(InputBox("Sales", "Number")))

If sngSales <= 250.0F Then
sngBonus = 10.0F
Else
sngBonus = 50.0F
End If

MessageLabel.Text = sngBonus.ToString
Similar to the first example. The use of UCase is discouraged but you

have to test against "HAWAII" not "Hawaii" if you want it to match.
Dim strState as String

strState = Me.ShippingTextBoxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabel.Text = "25"

Else

Me.ShippingLabel.Text = "50"

End If


Try:

Dim strState As String = ShippingTextBoxl.Text.ToUpper

If strState = "HAWAII" Then
ShippingLabel.Text = "25"
Else
ShippingLabel.Text = "50"
End If

Nov 20 '05 #3
"Amy" <ac*********@hotmail.com> wrote...
Hi Tom,
Thanks for taking a moment to help me! I am such a beginner that I am not
sure what the "option explicit" thing is on the second question.
Who knew If then Else statements could be so confusing!! :) Amy


Option Strict ... but Option Explicit should be set too of course. You want
to let the compiler and IDE help you as much as possible to reveal/prevent
ambiguous situations from turning into a source of trouble.

Are you using Visual Studio .Net? Check the help file... you can set it as
a default in the IDE, in the project or type the following at the top of
every file...

Option Explict On


Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
19
by: GMKS | last post by:
Hello all, I have 13 check boxes on a form. I am trying to check all the check boxes to determine if they are true or false when I close the form. At present only the first IF...Then...Else...
8
by: jane | last post by:
like, my homework is due at 4:30 pm central time Nov 19, and this stupid professor has no respect for my need to use my time elsewhere. He thinks that if the braces are off, then there's something...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
36
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): ...
22
by: MLH | last post by:
I would like to test some of this code in the debug window... Option Compare Database Option Explicit Private Sub Command0_Click() #If Win32 Then MsgBox "Hey! It's WIN32." #End If End Sub
12
by: Jerim79 | last post by:
I have created a verification script to verify information and redirect the customer to the appropriate error page. For example: if ($FName=""){ header('Location:/verify_fname.htm'); } else{...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.