423,850 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.