Homework Assistance Needed
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Mar 2014
Posts
1

## Homework Assistance Needed

Question number 7: Movie Ticket Solution

I have coded with the following; however, the price field remains blank after I have coded the btnIf. Here is my code:
Private Sub btnIf_Click(sender As Object, e As EventArgs) Handles btnIf.Click
Dim txtAge As Integer
Dim lblPrice As Integer

If txtAge < 3 Then
lblPrice = 0

ElseIf txtAge > 3 Then
lblPrice = 9

ElseIf txtAge < 64 Then
lblPrice = 9

ElseIf txtAge < 65 Then
lblPrice = 6

End If

End Sub

2. ## Re: Homework Assistance Needed

I think you have posted this to the wrong forum. This is the c++ forum. What you have posted looks like VB.

3. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Homework Assistance Needed

As 2kaud said, wrong forum.

However, you have some pretty bad logic errors in your if statements. Assuming you actually assign a value to txtAge,

If it's < 3, the the first statement applies.

If it's equal 3. the third statement applies.

If it's any number greater than three, the second statement applies.

You'll never make it to the fourth statement.

4. Senior Member
Join Date
Jun 2002
Location
Stockholm, Sweden
Posts
1,641

## Re: Homework Assistance Needed

Typically when dealing with integers using the less-than-or-equal (<=) and greater-than-or-equal (>=) operators will make the code more readable.

For example, instead of X < 3 you would write X <= 2.

5. ## Re: Homework Assistance Needed

In addition to all that has been stated correctly in the earlier responses you got already: txtAge and lblPrice seem to be intended to represent some controls on a form. However, by declaring them as integers in the first two lines of your sub's body, you're creating new meanings for these names that hide the meanings they had before, outside of that sub. (This is frequently referred to as "name shadowing".) The consequence of this is that any changes you make to these variables in your sub don't have any effect on anything outside of that sub.

Your first step into the direction of solving your problem should be to remove these local variabe declarations. Chances are you created these variables to get rid of some sort of type mismatch errors, but that measure then has been rather counter-productive. Instead, you probably should append .Text to the control variable names, as you can't directly assign integer values to controls. (The rest is in the hands of the quite generous type conversion rules of VB, that I, admittedly, never even tried to understand completely...)

If you get "not defined" errors instead of type mismatches after removing the local declarations, you have some rather fundamental variable scope problem with your code.
Last edited by Eri523; April 2nd, 2014 at 01:28 AM. Reason: Quite some typos

6. Super Moderator Power Poster
Join Date
Jul 2008
Location
WV
Posts
5,255

## Re: Homework Assistance Needed

Moved from VB6 to VB.Net section

Note: In addition to what has been said in the previous posts it also looks like you have a mistake here
Code:
`ElseIf txtAge < 65 Then`
I would imagine that should be >=65 rather than <65
Better still it should be just a simple Else

Also note that the >3 part should be removed as that will stop the next two sections from ever being reached

And one other note as coded if age=3 then all of the if tests would be false
Edit:Correction in the case of = 3 the <64 portion would be true as it is not <3 and not >3 but is <64

In any case you need to give the logic more thought once you have removed those dim statements that are killing your values
Last edited by DataMiser; April 1st, 2014 at 10:33 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•