4 Replies - 316 Views - Last Post: 07 March 2013 - 10:44 PM Rate Topic: -----

#1 pyrogomez101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-March 13

I need help in Visual Basic,I am having trouble with a validation code

Posted 07 March 2013 - 09:17 PM

So I think I have finished this program except for the fact that I do not get why my restrictions dont work. It is under Functions Parts() and Labor(). I am trying to put restrictions so that you can't leave it blank and that you have to enter a positive number. I get a weird error when I leave the answers blank and I also tried using a dif line of coding such as

decParts = Decimal.TryParse(txtParts.txt, decparts)

and when i changed to that, my blank restriction would work but then my restriction for only having positive numbers wont work. I would enter a positive number like 1 or 50 and it would still say that it was not a positive number even though i have it as < 0. Any help will be great, I will appreciate it.



  Public Class Form1

    Const decTAX_RATE As Decimal = 0.06D 'Tax Rate
    Const decOIL_CHANGE As Decimal = 26D
    Const decLUBE_JOB As Decimal = 18D
    Const decRADIATOR_FLUSH As Decimal = 30D
    Const decTRANSMISSION_FLUSH As Decimal = 80D
    Const decInspection As Decimal = 15D
    Const decMUFFLER_REPLACEMENT As Decimal = 100D
    Const decTIRE_ROTATION As Decimal = 20D

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
        Dim decSERVICES_LABOR As Decimal
        Dim decPARTS As Decimal
        Dim decTAX As Decimal
        Dim decTOTAL As Decimal

        decSERVICES_LABOR = OilAndLubeCharges() + FlushCharges() + MiscCharges() + (Labor() * 20)
        decPARTS = Parts()
        decTAX = CalcTax(decPARTS)
        decTOTAL = decTAX + decSERVICES_LABOR + decPARTS

        lblSL.Text = decSERVICES_LABOR.ToString("c")
        lblParts.Text = decPARTS.ToString("c")
        lblTax.Text = decTAX.ToString("c")
        lblTotal.Text = decTOTAL.ToString("c")

    End Sub

    Function OilAndLubeCharges() As Decimal
        Dim decOilOrLube As Decimal

        If chkOilChange.Checked = True Then
            decOilOrLube += decOIL_CHANGE
        End If

        If chkLubeJob.Checked = True Then
            decOilOrLube += decLUBE_JOB
        End If

        Return decOilOrLube
    End Function

    Function FlushCharges() As Decimal
        Dim decFlushes As Decimal

        If chkRadiatorFlush.Checked = True Then
            decFlushes += decRADIATOR_FLUSH
        End If

        If chkTransmissionFlush.Checked = True Then
            decFlushes += decTRANSMISSION_FLUSH
        End If

        Return decFlushes
    End Function

    Function MiscCharges() As Decimal
        Dim decMisc As Decimal

        If chkInspection.Checked = True Then
            decMisc += decInspection
        End If

        If chkMuffler.Checked = True Then
            decMisc += decMUFFLER_REPLACEMENT
        End If

        If chkTireRotation.Checked = True Then
            decMisc += decTIRE_ROTATION
        End If

        Return decMisc
    End Function

    Function Parts() As Decimal

        Dim decParts As Decimal

        decParts = CDec(txtParts.Text)

        If decParts = False Then
            MessageBox.Show("Please enter a dollar amount for Parts.")
            With txtLabor
                .SelectAll()
                .Focus()
            End With
        End If

        If decParts < 0 Then
            MessageBox.Show("Please enter a positive value for" & " the parts cost.")
        End If

        Return decParts

    End Function

    Function Labor() As Decimal
        Dim decLabor As Decimal

        decLabor = CDec(txtLabor.Text)

        If decLabor = False Then
            MessageBox.Show("Please enter how many hours for Labor.")
            With txtLabor
                .SelectAll()
                .Focus()
            End With
        End If

        If decLabor < 0 Then
            MessageBox.Show("Please enter a positive value for" & " the Labor hours.")
        End If

        Return decLabor

    End Function

    Function CalcTax(ByVal decAmount As Decimal) As Decimal
        Return decAmount * decTAX_RATE

    End Function


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click

        ClearAll()

    End Sub

    Sub ClearAll()
        chkOilChange.Checked = False
        chkLubeJob.Checked = False
        chkRadiatorFlush.Checked = False
        chkTransmissionFlush.Checked = False
        chkInspection.Checked = False
        chkMuffler.Checked = False
        chkTireRotation.Checked = False
        txtParts.Clear()
        txtLabor.Clear()
        lblSL.Text = String.Empty
        lblParts.Text = String.Empty
        lblTax.Text = String.Empty
        lblTotal.Text = String.Empty

    End Sub


    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

   
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: I need help in Visual Basic,I am having trouble with a validation code

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: I need help in Visual Basic,I am having trouble with a validation code

Posted 07 March 2013 - 09:40 PM

I see a few things...
If decParts = False Then


decPats cannot be False. False is a Boolean value, and decParts is a decimal variable. There is no Trueness or Falseness about any varible that is not a Boolean variable.

I don't understand why you have the following in your Parts() function. You don't actually do anything in that function with txtLabor.

            With txtLabor
	         .SelectAll()
	         .Focus()
	    End With


Parts() doesn't do anything with the information you get about the value. You check to see it it's negative, and if it is, you tell the user, then return the value anyway.


I HIGHLY recommend you place two lines above the Class statement...

Option Strict On
Option Explicit On


Those two lines will save you countless hours of puzzling out why things are not working.

Work on that for now. I'll have a look at the Labor() function in a while.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: I need help in Visual Basic,I am having trouble with a validation code

Posted 07 March 2013 - 09:48 PM

When you use CDec the return type is a Decimal. So why are you then comparing it to false? Then when you use TryParse it returns boolean so why are you then trying to compare it to zero?

' Take the value in txtParts and convert it to decimal and put it in decparts
' but then return true/false and stick that in decparts
' See a problem?
decParts = Decimal.TryParse(txtParts.txt, decparts) 



What you need to do is something like...

If Decimal.TryParse(txtParts.text, decparts) then
   ' In here we know the parse succeeded because tryparse returned true
   ' Now decparts is a decimal so compare it a value
   If decparts > 0 then
       ' Golden
   End if
Else
   ' We know the parse didn't succeed
   ' Mention here that they need to enter a decimal value
End if



Get the idea? :)
Was This Post Helpful? 0
  • +
  • -

#4 pyrogomez101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-March 13

Re: I need help in Visual Basic,I am having trouble with a validation code

Posted 07 March 2013 - 10:06 PM

@lar3ry

Now I get it, I was looking at a wrong example and I guess I mixed it up and screwed myself up. Thank you for making me realize that. I am not familiar with the 2 lines you gave me, could you expand on exactly what they do?

@Martyr2

Oh ok I get that idea clearly, now that makes sense to prevent my error for the negative numbers.

Is this also the same to way to incorporate an error message if I leave both text boxes blank?

Thank you so much for your help guys, I am a beginner and trying to understand this, I truly appreciate your input.
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: I need help in Visual Basic,I am having trouble with a validation code

Posted 07 March 2013 - 10:44 PM

View Postpyrogomez101, on 07 March 2013 - 11:06 PM, said:

I am not familiar with the 2 lines you gave me, could you expand on exactly what they do?


This will cause the editor to point out errors of declaration and data type to you. A little wiggly blue underline will show up under any line, or part of a line that is in error. If you move your mouse cursor over the error, you will be shown the error, and if you put the mouse cursor at the end of the underline, you will sometimes see a red exclamation mark. Clicking on that will usually show you how to correct the error, and clicking on the suggestion will correct it for you.

It's a great tool, and you should consider using it all the time, not just when debugging. It will help you learn, save you keystrokes, and best of all, prevent some data-type errors that could otherwise be difficult to find.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1