Question functions?

audio_uphoria

Active member
Joined
Jan 28, 2009
Messages
31
Programming Experience
Beginner
Hello, I had an assignment where I'm supposed to use two sub procedures. Now I'm supposed to do the same program but using two functions. The subs were very easy and straight forward but I cant get functions to work no matter what I do. Here's my code for the assignment with the subs, going off that can you help me with the functions. The functions are supposed to replace the subs.

VB.NET:
Option Explicit On
Option Strict On
Public Class frmMain
    Dim Input As Integer
    Dim Output As Double
    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Integer.TryParse(txtInput.Text, Input)
        If rdoToF.Checked = False And rdoToC.Checked = False _
        Or txtInput.Text = "" Then
            MessageBox.Show("Please specify what you want to convert to", "Error Message")
            txtInput.Focus()
        ElseIf rdoToC.Checked = True Then

            Call ConvertToCelsius()
            lblOutPut.Text = Output.ToString("n1")
        ElseIf rdoToF.Checked = True Then

            Call ConvertToFar()
            lblOutPut.Text = Output.ToString("n1")
        End If


    End Sub
    Private Sub ConvertToCelsius()
        Output = (5 / 9) * (Input - 32)
    End Sub
    Private Sub ConvertToFar()
        Output = (9 / 5) * Input + 32
    End Sub
    Private Sub txtInput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.Enter
        txtInput.SelectAll()
    End Sub

    Private Sub rdoToC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoToC.Click
        If rdoToC.Checked = True Then
            lblOutPut.Text = ""
            lblDegreeIn.Text = "F"
            lblDegreeOut.Text = "C"
            txtInput.Focus()
        End If
    End Sub

    Private Sub rdoToF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoToF.Click
        If rdoToF.Checked = True Then
            lblOutPut.Text = ""
            lblDegreeIn.Text = "C"
            lblDegreeOut.Text = "F"
            txtInput.Focus()
        End If
    End Sub

    Private Sub txtInput_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged
        lblOutPut.Text = ""
    End Sub
    Private Sub frmMain_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave, btnExit.Click
        Me.Close()
    End Sub
End Class
 
Pass the value in the textbox where your imput is in the sub
Delete your variable input...
Private Sub ConvertToCelsius(Byval input as string)
Output = (5 / 9) * (Input - 32)
End Sub
Private Sub ConvertToFar(Bvyal input as string)
Output = (9 / 5) * Input + 32
End Sub
When you call the sub now use:
ConVertToCelsius(txtInput.text) ' the val in textbox to be converted
ConvertToFar(txtInput.text)

txtOutput.text = output.toString

Now textbox1.text has the same value as 'input' does inside of the function.
 
The difference between a Sub method and a Function method is the latter return a value. In this case you should also add a parameter to the methods as suggested. The return type of your functions here is supposed to be Double. Remove the class level variable declarations for Input and Output in frmMain, they are superfluous when you have implemented the methods correctly as functions with input parameters. This conversion is an exercise in OOP programming, in making methods not depend on anything external to them. A little help with one method signature:
VB.NET:
Function ConvertToCelsius(Byval input as Integer) As Double

In the btnConvert_Click method you are calling the shared function method Integer.TryParse, but you are not catching and handling the return value. TryParse method return Boolean (true/false) indicating if it the parsing was successful or not.
 
JohnH is right:

VB.NET:
Private Function ConvertToCelsius(Byval input As String) As Double
  Dim result as Double
  result = (5 / 9) * (input - 32)
  Return result 
End Function

label1.text = ConvertToCelsius(textbox1.text)

OK, NO MORE DRINKING AND CODING, I PROMISE. :cool:
 
JohnH is right:

VB.NET:
Private Function ConvertToCelsius(Byval input As String) As Double
  Dim result as Double
  result = (5 / 9) * (input - 32)
  Return result 
End Function

label1.text = ConvertToCelsius(textbox1.text)

OK, NO MORE DRINKING AND CODING, I PROMISE. :cool:
Just tidy it up a bit:
VB.NET:
Private Function ConvertToCelsius(Byval input As String) As Decimal
    Return (5 / 9) * (input - 32)
End Function
Also for stuff like this I'm a huge fan of Decimal instead of Double, but that's just me.
 
Back
Top