Homework Assistance Needed
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Homework Assistance Needed

  1. #1
    Join Date
    Mar 2014

    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. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    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.

    [moderator would you move please]
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2017

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003

    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. #4
    Join Date
    Jun 2002
    Stockholm, Sweden

    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.
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Jun 2010

    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
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    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
    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.
    Always use [code][/code] tags when posting code.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a Codeguru.com survey!

HTML5 Development Center