4 Replies - 1099 Views - Last Post: 19 June 2011 - 07:27 PM Rate Topic: -----

#1 SteveG4274   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-June 11

I have a coding problem with a VB 2008 project.

Posted 18 June 2011 - 07:27 PM

I have been asked to complete a coding project for school on VB 2008. The bpook asks for us to develop an application that asks for your weight, will convert it to pounds or kilograms. The problem I am having is displaying whether the weight is pounds or kilograms and also I need to put an error message if pounds entered is over 500 and if the kilograms entered is over 225. Below is the code I have so far.

'Name:      Patient Weight Calculator
'Author:    Stephen Gardner
'Date:      June 16, 2011
'Purpose:   This mobile application converts your weight from kiolgrams
'           and from kilograms from pounds based on that pounds will be multiplied by 2.2 to 
'           equal kiolgrams and kilograms will be divided by 2.2 to equal pounds.

Option Strict On

Public Class frmPatientWeightCalculator

    Private Sub lblEnterWeight_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblEnterWeight.ParentChanged

    End Sub

    Private Sub btnDisplayWeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayWeight.Click
        'The btnDisplayWeight event handler converts your weight from pounds
        'to kilograms or from kilograms to pounds.

        'Declaraton Section
        Dim strlblDecide As String
        Dim decEnterWeight As Decimal
        Dim decConvertedWeight As Decimal
        Dim decWeightByType As Decimal
        Dim decPounds = decConvertedWeight * 2.2D
        Dim decKilograms = decConvertedWeight / 2.2D

        'Did user enter a numeric value?
        If IsNumeric(Me.txtEnterWeight.Text) Then
            decEnterWeight = Convert.ToDecimal(Me.txtEnterWeight.Text)

            'Is Weight greater than zero?
            If decEnterWeight > 0 Then
                'Determine weight by conversion
                If Me.radKilograms.Checked Then
                    decWeightByType = decKilograms
                ElseIf Me.radPounds.Checked Then
                    decWeightByType = decPounds
                End If
                'Calculate and display the weight
                decConvertedWeight = decEnterWeight * decWeightByType
                Me.lblConvertedWeight.Text = decConvertedWeight.ToString("n1")

                'display weight by conversion type
                If Me.radKilograms.Checked Then
                    strlblDecide = "Kilograms"
                ElseIf Me.radPounds.Checked Then
                    strlblDecide = "Pounds"
                End If
            Else
                'Display message if user entered a negative value
                MsgBox("You entered" & decEnterWeight.ToString() & _
                        ". Enter a Number Greater Than Zero.", , "Input Error")
                Me.txtEnterWeight.Text = ""
                Me.txtEnterWeight.Focus()
            End If
        Else
            'Display error message if user entered a nonnumeric value
            MsgBox("Enter Your Weight.", , "Input Error")
            Me.txtEnterWeight.Text = ""
            Me.txtEnterWeight.Focus()
        End If
    End Sub

    Private Sub PatientWeightCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'This event handler is executed when
        'the form is loaded at the start of
        'the program . It sets the focus to
        'the Enter Weight text box and clears
        'the converted weight label

        Me.txtEnterWeight.Focus()
        lblConvertedWeight.Text = ""
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'This event handler is executed when
        'the user clicks the Clear button. It
        'clears the Enter Weight text box and the 
        'Converted Weight labels, rests the radio
        'buttons with Convert Pounds to Kilograms,
        'and sets the focus to the Enter Weight text 
        'box
        Me.txtEnterWeight.Text = ""
        Me.lblConvertedWeight.Text = ""
        Me.radKilograms.Checked = True
        Me.radPounds.Checked = False
        Me.txtEnterWeight.Focus()

    End Sub

    Private Sub txtEnterWeight_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEnterWeight.TextChanged

    End Sub

    Private Sub lblDecide_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDecide.ParentChanged
       
    End Sub
End Class


Thanks to anyone who can assist me.

This post has been edited by modi123_1: 18 June 2011 - 09:06 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I have a coding problem with a VB 2008 project.

#2 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I have a coding problem with a VB 2008 project.

Posted 18 June 2011 - 11:55 PM

First look at your variable declaration:
Dim decConvertedWeight As Decimal
        Dim decWeightByType As Decimal
        Dim decPounds = decConvertedWeight * 2.2D
        Dim decKilograms = decConvertedWeight / 2.2D

Here you used decConvertedWeight before assigning it a value, also not specifying its type! Then in your calculation I see things which are not important to be there.
HINT:
May be try this algorithm:
DECLARATION:
Dim decEnterWeight As Double
        Dim result As Double
        Dim convertTo As Double = 2.2 'you may set it final

COLLECT DATA:(As you did, and check for non positive and other checking as you did)
CALCULATION:
 If ToKG then
      result = decEnterWeight * convertTo
    elseIf ToP then
      result = decEnterWeight / convertTo


:)
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: I have a coding problem with a VB 2008 project.

Posted 19 June 2011 - 04:33 AM

Sometime you don't have to enter the type, as long the compiler can infer what it is.
Was This Post Helpful? 1
  • +
  • -

#4 SteveG4274   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-June 11

Re: I have a coding problem with a VB 2008 project.

Posted 19 June 2011 - 07:16 PM

Your suggestions, apprecitated very much, did not work. I tried cutting down on thedeclarations but nothing ghas owrked thus far. Thanks for your assistance.
Steve
Was This Post Helpful? 0
  • +
  • -

#5 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I have a coding problem with a VB 2008 project.

Posted 19 June 2011 - 07:27 PM

Can you post the updated code to see if anything going wrong?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1