473,715 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CarTex tBox.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("E nter 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("H ow 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.ShippingText Boxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabe l.Text = "25"

Else

Me.ShippingLabe l.Text = "50"

End If


Nov 20 '05 #1
3 3934
"Amy" <ac*********@ho tmail.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.CarTex tBox.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("E nter 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(InputB ox("Enter a number equal to
or greater than 0", "Number")))

If intUnits < 0 Then
MessageLabel.Te xt = "Entry Error"
Else
MessageLabel.Te xt = "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("H ow 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(InputB ox("How many items are left
on the shelf?", "Number")))

If sngPrice < 10.0F Then
MessageLabel.Te xt = "Reorder"
Else
MessageLabel.Te xt = "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(InputB ox("Sales", "Number")))

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

MessageLabel.Te xt = sngBonus.ToStri ng
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.ShippingText Boxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabe l.Text = "25"

Else

Me.ShippingLabe l.Text = "50"

End If


Try:

Dim strState As String = ShippingTextBox l.Text.ToUpper

If strState = "HAWAII" Then
ShippingLabel.T ext = "25"
Else
ShippingLabel.T ext = "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*@iamtiredof spam.com> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
"Amy" <ac*********@ho tmail.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.CarTex tBox.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("E nter 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(InputB ox("Enter a number equal

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

If intUnits < 0 Then
MessageLabel.Te xt = "Entry Error"
Else
MessageLabel.Te xt = "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("H ow 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(InputB ox("How many items are

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

If sngPrice < 10.0F Then
MessageLabel.Te xt = "Reorder"
Else
MessageLabel.Te xt = "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(InputB ox("Sales", "Number")))

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

MessageLabel.Te xt = sngBonus.ToStri ng
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.ShippingText Boxl.Text

strState = UCase(strState)

If strState = "Hawaii" Then

Me.ShippingLabe l.Text = "25"

Else

Me.ShippingLabe l.Text = "50"

End If


Try:

Dim strState As String = ShippingTextBox l.Text.ToUpper

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

Nov 20 '05 #3
"Amy" <ac*********@ho tmail.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
2279
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 class for awhile and don't know what I'm doing. I have included my (probably wrong) answers for the first few questions. It would be great if someone could tell me what is missing or what I need to work on at least just for the first few. I...
19
8234
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 works and then Exits the Sub. Example:
8
1530
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 wrong, but I got my braces off while dating Billy 2 years ago, and I was hotter, so I could start dating Chad on the hockey team. It's so nice to have a friendly place to talk about things:-) Anyways this professor wants me to use c to hurl a...
15
5359
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
36
2651
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): options { directory "/var/named"; listen-on { 192.168.11.22; 127.0.0.1; }; forwarders { 168.126.63.1; };
22
6318
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
1900
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{ if ($LName=""){ header('Location:/verify_lname.htm'); }
8
29167
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 classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
0
8817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9322
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7966
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5964
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3171
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.