9 Replies - 236 Views - Last Post: 29 February 2012 - 07:54 PM Rate Topic: -----

#1 cashisking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 12

Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:05 PM

I'm trying to find out what exactly is happening in this code, to explain to my cousin. One of my classmates helped me write it, but I'm yet to fully understand what exactly is going on with it, myself. I attempted to explain it in the comment section on the side, but I'm not sure if I am right. Can some one tell me if I'm right or remove my comments, an add yours, to fully explain what is happening in the code?

Thanks,
Cash

 Public Class frmPatientWeightConverter

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' When ^Diplay^ button on program is clicked, the following algarithm will occur' 

        Dim weight, convert As Decimal ' Weight AND Convert- is declared a decimal
        Dim strWeight As String  ' What ever typed in textbox is a string FIRST! Even though its a numerical charactor 
        strWeight = txtWeight.Text ' strWeight is assigned to the text box  field which name is txtWeight
        Decimal.TryParse(strWeight, weight) ' Now, what ever is the string input from the text box, will now get converted into a decimal 

        If radKilograms.Checked Then 'when Kilo button is checked
            convert = weight / 2.2 ' weight is now a number, and DIVIDED by 2
            lblConvert.Text = convert.ToString & " Kilograms" ' Now the result of is displayed in the text box (150.50 Kilograms) AS A string
            lblConvert.Visible = True ' simply means show string in the lable (xxx.xx)

        ElseIf radPounds.Checked Then 'When Pound button is checked on radio button
            convert = weight * 2.2 'weight is now a number, and MULTIPLIED by 2
            lblConvert.Text = convert.ToString & " Pounds" 'Now the result of is displayed in the text box ( 567.09 POUNDS) AS A string
            lblConvert.Visible = True 'show result
        Else
            MessageBox.Show("Invalid input")
        End If

    End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtWeight.Clear() 'clears out text where the Weight was out putted
        radKilograms.Checked = True
        lblConvert.Visible = False
    End Sub 


Is This A Good Question/Topic? 0
  • +

Replies To: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:36 PM

What the code seems to be doing is, it converts the value for weight to Kilograms or Pounds, depending on the user selection. Your comments pretty much explains everything.

here strWeight = txtWeight.Text, its more like the value from the txtWeight Textbox is assigned to the strWeight variable. not the other way around.
Was This Post Helpful? 1
  • +
  • -

#3 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:43 PM

Which part of the code do you not understand .. As the code seems straightforward to me..

Quote

' When ^Diplay^ button on program is clicked, the following algarithm will occur'


You misspelled Algorithm but that doesn't matter .

Like nK0de said most of your code explanation are quite clear in terms of a layman's term.
Was This Post Helpful? 1
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:46 PM

The comment next to lblConvert.Visible = True (both instances) is wrong. Visible does not mean show the string, it means whether the label control itself is visible or not.
Was This Post Helpful? 1
  • +
  • -

#5 cashisking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 12

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:46 PM

Your absolutely right, nK0de. Thanks for pointing that out. Now that I look at it, I'm disappointed that I didn't catch that. I appreciate you taking your time, to help out. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:48 PM

Since TxtWeight.Text is also a string you mustn't declare strweigt - just TryParse with TxtWeight.Text.
Sorry - you can have it but it's an optional step.
Was This Post Helpful? 1
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:51 PM

I guess I would say that decimal.TryParse Tries to perform the conversion and if successful will place the value in the decimal datatype specified. However, if unsuccessful the result is 0 and will therefore produce undesirable results. It would probably be better to use an if statement like:
If decimal.TryParse(strWeight, weight) Then
 'The conversion was successful
  Perform the conversion
Else
 'The conversion was unsuccessful.
  Notify user that the value entered isn't valid
End If


EDIT: Oh and the Obsessive Compulsive side of me would bring the conversion closer:
1 kilogram = 2.20462262 pounds

This post has been edited by CharlieMay: 29 February 2012 - 07:57 PM

Was This Post Helpful? 1
  • +
  • -

#8 cashisking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 12

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:52 PM

View Postshadachi, on 29 February 2012 - 07:43 PM, said:

Which part of the code do you not understand .. As the code seems straightforward to me..

Quote

' When ^Diplay^ button on program is clicked, the following algarithm will occur'


You misspelled Algorithm but that doesn't matter .

Like nK0de said most of your code explanation are quite clear in terms of a layman's term.

Ha, I did misspelled algorith, my bad. I was typing fast... I think I understand it, I just wanted to make sure it was okay. Thanks for the reply, though.


Also, @DimitriV, thanks.
Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:54 PM

No problem mate :)
Was This Post Helpful? 1
  • +
  • -

#10 cashisking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 12

Re: Newbie here. Can You Break Down Whats Happeninng In This Source Code?

Posted 29 February 2012 - 07:54 PM

View PostCharlieMay, on 29 February 2012 - 07:51 PM, said:

I guess I would say that decimal.TryParse Tries to perform the conversion and if successful will place the value in the decimal datatype specified. However, if unsuccessful the result is 0 and will therefore produce undesirable results. It would probably be better to use an if statement like:
If decimal.TryParse(strWeight, weight) Then
 'The conversion was successful
  Perform the conversion
Else
 'The conversion was unsuccessful.
  Notify user that the value entered isn't valid
End If

Thank you, CharlieMay. I appreciate it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1