CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2014
    Posts
    1

    Homework Assistance Needed

    http://books.google.com/books?id=rec...lution&f=false

    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
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.
    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
  •  





Click Here to Expand Forum to Full Width

Featured