The following is the code from an assignment I am struggling with. I am supposed to change the GetGallons function procedure to a sub procedure in a class that i created in another project.. Here is the code from the original class:

Public Class RectangularPool
    Private _decLength As Decimal
    Private _decWidth As Decimal
    Private _decDepth As Decimal

    Public Property Length As Decimal
        Get
            Return _decLength
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decLength = value
            Else
                _decLength = 0
            End If

        End Set
    End Property

    Public Property Width As Decimal
        Get
            Return _decWidth
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decWidth = value
            Else
                _decWidth = 0
            End If

        End Set
    End Property

    Public Property Depth As Decimal
        Get
            Return _decDepth
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decDepth = value
            Else
                _decDepth = 0
            End If

        End Set
    End Property

    Public Sub New()
        'default constructor
        _decLength = 0
        _decWidth = 0
        _decDepth = 0

    End Sub

    Public Function GetVolume() As Decimal
        Return _decLength * _decWidth * _decDepth
    End Function

    Public Function GetGallons() As Decimal
        Dim decVol As Decimal
        decVol = GetVolume()
        Return decVol * 7.48
    End Function
End Class

Here is the code from the main form:

Public Class frmMain

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        ' displays the number of gallons

        Dim pool As New RectangularPool
        Dim decGallons As Decimal

        Decimal.TryParse(txtLength.Text, pool.Length)
        Decimal.TryParse(txtWidth.Text, pool.Width)
        Decimal.TryParse(txtDepth.Text, pool.Depth)

        decGallons = pool.GetGallons
        lblGallons.Text = decGallons.ToString("N0")

        txtLength.Focus()
    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtDepth_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDepth.KeyPress
        ' allows only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
            e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDepth_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDepth.TextChanged
        lblGallons.Text = String.Empty
    End Sub

    Private Sub txtLength_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLength.KeyPress
        ' allows only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
            e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtLength_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLength.TextChanged
        lblGallons.Text = String.Empty
    End Sub

    Private Sub txtWidth_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtWidth.KeyPress
        ' allows only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
            e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtWidth_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWidth.TextChanged
        lblGallons.Text = String.Empty
    End Sub

    Private Sub lblGallons_Click(sender As System.Object, e As System.EventArgs) Handles lblGallons.Click

    End Sub
End Class

...I am supposed to change the GetGallons function procedure to a sub procedure and then modify the btnCal controls click event to accomodate the modification to the GetGallons procedure. I could really used an experienced hand here. Thank you.

Recommended Answers

All 8 Replies

The window has three text boxes(txtLength, txtWidth, and txtDepth) for input, and one label(lblGallons) for output. The original assignment was to code the the class called RectangularPool.vb the way it is displayed in my previous post. The application calculates the number of gallons given the dimensions of the pool using the formula length * width * depth * 7.48. The next assignment is to make the modifications to the class using the sub procedure instead of the function procedure, then the modify click event for the button that calculates(btnCalc) the number of Gallons in a pool to accomodate the modified sub procedure in the class RectangularPool.vb.

By the way this is w Visual Basic 2010 Windows application.

If the Sub doesn't return a value then you either have to have the sub modify a parameter (not recommended) or modify a private variable which is then accessed through a readonly property.

In this case I think you can also simply make the getGallons sub alter the text of lblGallons label (depending on where your getGallons sub is located).

Yeah I don't know Jim. From what I see in the instructions I am supposed to leave the procedure where it's at. Since a sub doesn't return a value I don't see how I am supposed to connect the results of the procedure to the lblGallons label. I am completely stumped.

If you change the Function GetGallons to a Sub named CalcGallons (for example), that sets a private variable, _gallons, then add a readonly property, Gallons you can calculate and display the result as follows

pool.CalcGallons()
lblGallons.Text = pool.Gallons.ToString("N0")

Yeah I don't know Jim. From what I see in the instructions I am supposed to leave the procedure where it's at. Since a sub doesn't return a value I don't see how I am supposed to connect the results of the procedure to the lblGallons label. I am completely stumped.

`The following is the code I modified for the class:

Public Class RectangularPool
    Private _decLength As Decimal
    Private _decWidth As Decimal
    Private _decDepth As Decimal
    Dim decVol As Decimal

    Public Property Length As Decimal
        Get
            Return _decLength
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decLength = value
            Else
                _decLength = 0
            End If

        End Set
    End Property

    Public Property Width As Decimal
        Get
            Return _decWidth
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decWidth = value
            Else
                _decWidth = 0
            End If

        End Set
    End Property

    Public Property Depth As Decimal
        Get
            Return _decDepth
        End Get
        Set(ByVal value As Decimal)
            If value > 0 Then
                _decDepth = value
            Else
                _decDepth = 0
            End If

        End Set
    End Property

    Public Sub New()
        'default constructor
        _decLength = 0
        _decWidth = 0
        _decDepth = 0

    End Sub

    Public Function GetVolume() As Decimal
        Return _decLength * _decWidth * _decDepth
    End Function

    Private Sub GetGallons(ByRef decGetVol As Decimal)

        decGetVol = GetVolume() * 7.48

    End Sub

End Class
  How do I make it read only and accesible to the label control? The code you gave me in the last post is intended for the main form right?

`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.