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

    Please Help Me!!

    Hello. My name is Jennifer. I am new to the world of programming and to this forum. Please be kind. I have an assignment for my intro to programming class due tonight by midnight and i cannot figure out what is wrong with this code. I feel like i am over my head with this class. Someone please help me. I have attached my coding below so you can see what i am doing wrong. I believe it is a calculation error, not a programming error. But like i said, i am new and have no clue what i am doing.

    Code:
      Private Sub calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calc.Click
            
            Dim north As Integer
            Dim south As Integer
            Dim east As Integer
            Dim west As Integer
    
    
            Dim LastYearSales As Double
            Dim NextYearSales As Double = 0
            Dim IncreaseAmount As Double = 0
    
            LastYearSales = Val(lastY.Text)
    
    
    
            north = 0.05
            south = 0.08
            east = 0.03
            west = 0.06
    
    
            If NSEW.Text.ToUpper = "NORTH" Then
                IncreaseAmount = north * LastYearSales
            ElseIf NSEW.Text.ToUpper = "SOUTH" Then
                IncreaseAmount = south * LastYearSales
            ElseIf NSEW.Text.ToUpper = "EAST" Then
                IncreaseAmount = east * LastYearSales
            ElseIf NSEW.Text.ToUpper = "WEST" Then
                IncreaseAmount = west * LastYearSales
            End If
    
    
    
            NextYearSales = IncreaseAmount + LastYearSales
    
    
            lblAns.Text = NextYearSales
        End Sub
    
    
    
    
    End Class
    Thank you for your time. This has been most frustrating to say the least!!
    Last edited by DataMiser; March 5th, 2013 at 11:07 PM. Reason: added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Please Help Me!!

    See what the value returned...

    Code:
    LastYearSales = Val(lastY.Text)
    Debug.Print LastYearSales
    Add the line, and then look in the debug.window, or place a breakpoint (F9) and step thru the code.
    It should not be hard to find.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Please Help Me!!

    You have declared your 4 variables as Integer but you are assigning decimal values to them so you have a problem before you get to the calculations. Integers can hold whole numbers only so those values are all rounded off as soon as they are assigned. These should be defined as Decimal rather than Integer

    For the sake of readability you should use a Select Case structure rather than If Then ElseIf structure.

    On your last line you are assigning a value from a numeric var to a string. Apparently you have Option Strict turned off or this would give you an error message. You should get in the habit of doing proper conversions in this case when you want to assign a value from a numeric var to a variable of type string such as the Text property of a Label you should use the .ToString method

    Also the code you have posted is not VB6 code but is .Net code which should be posted in the VB.Net section of the forum.


    <-- Thread moved to Vb.Net area -->
    Last edited by DataMiser; March 5th, 2013 at 11:04 PM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Mar 2013
    Posts
    6

    Re: Please Help Me!!

    Thank you for the quick responses. The code is now corrected but the it won't calculate. If I put (for example) region - north and 100 for last years sales I get an answer of 100. What am I doing wrong?

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Please Help Me!!

    Hard to say without seeing the code you are now using.

    I tried your code and if I use what you have posted in the OP I get 100 if I enter 100 and north into the text boxes.

    When I change the variable type to decimal as I suggested before then with those same values I get a result of 105

    Also note that you can define the value as part of the declaration so there is no need to use 8 lines for your 4 vars

    If you change your existing code at the top to

    Code:
            Dim north As Decimal
            Dim south As Decimal
            Dim east As Decimal
            Dim west As Decimal
    Then you should see the correct result

    You could change it to
    Code:
            Dim north As Decimal = 0.05
            Dim south As Decimal = 0.08
            Dim east As Decimal = 0.03
            Dim west As Decimal = 0.06
    and delete the 4 lines where you are assigning values now
    In this case you would also get the expected result but you would do so with a simpler piece of code that is a few lines shorter
    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