I have to use a basic ByRef and ByVal in a program and its not showing up in the labels when I hit the calculate button. The program I am creating is very basic and it calculates your age, nextyear age, month born, astrological sign, days old and seconds old.

I know how to calculate most all but, am stuggling with my ByRef and ByVal.

Here is the snippet of the code I am struggling with:

 Private Sub ageNextYear(ByVal nextYearage As Integer)
        nextYearage = currentAge + 1
        Me.lblnextYearAge.Text = nextYearage.ToString
    End Sub



    Private Sub Yourdays(ByVal lbldaysOld)
        lbldaysOld.text = lblcurrentAge.Text * 365
    End Sub

    Private Sub yourSeconds(ByVal lblsecondsOld)
        lblsecondOld.Text = lbldaysOld.Text * 86400
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Call ageNextYear(currentAge)

        currentAge = System.DateTime.Now.Year - Me.txtYear.Text
        lblcurrentAge.Text = currentAge.ToString
        Call ageNextYear(currentAge)
        lblnextYearAge.Text = nextYearAge.ToString

When I run the program it displays the next year age as zero and does not display the seconds or days old.

I have attatched a picture to show what happens.

Capture2

Any help would be appreciated, as I am VERY bad at coding. :(

Thank you!

Dani AI

Generated

Quick diagnosis: the code calls the helper Subs before the numeric inputs are parsed, passes values in ways that never change the caller, and mixes label Text (strings) directly into arithmetic. As noted, a ByVal parameter is a copy; changing it inside a Sub does not change the outer variable. As pointed out, comparing txtMonth.Text to numbers and relying on implicit conversions is fragile — a DateTimePicker or explicit parsing is safer.

Fix plan (concise):

  • Turn on Option Strict On. That prevents silent string-to-number conversions and highlights the real problems.
  • Parse inputs with Integer.TryParse (or use a DateTimePicker). Do the parsing and compute currentAge before calling any helper Subs.
  • Prefer a Function that returns values instead of trying to mutate a caller variable via ByRef. If a returned value is needed, assign it to the field and update the label.
  • Use Long for seconds (Int32 overflows for large second counts).
  • Watch control name typos (e.g., lblsecondOld vs lblsecondsOld) and avoid passing Label controls into numeric helpers.

Example corrected approach (new code, not a copy of the forum snippets):

Option Strict On

Private Function CalculateAge(year As Integer, month As Integer) As Integer
    Dim birth = New DateTime(year, Math.Max(1, Math.Min(12, month)), 1)
    Dim age = DateTime.Now.Year - birth.Year
    If DateTime.Now < birth.AddYears(age) Then age -= 1
    Return age
End Function

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim y As Integer, m As Integer = 1
    If Not Integer.TryParse(txtYear.Text, y) Then Exit Sub
    Integer.TryParse(txtMonth.Text, m)

    Dim ageYears = CalculateAge(y, m)
    lblcurrentAge.Text = ageYears.ToString()
    lblnextYearAge.Text = (ageYears + 1).ToString()

    Dim span = DateTime.Now - New DateTime(y, Math.Max(1, Math.Min(12, m)), 1)
    lbldaysOld.Text = CInt(span.TotalDays).ToString()
    lblsecondsOld.Text = CLng(span.TotalSeconds).ToString()
End Sub

Quick troubleshooting checklist: compute inputs first, enable Option Strict, use TryParse, prefer Functions that return numbers, use Long for seconds, and fix any label name typos. This addresses the zero/blank outputs seen in the original posts by and follows the guidance from and .

Recommended Answers

All 3 Replies

If you pass a variable into a Sub ByVal, the Sub receives a copy of the variable. Any changes made inside the Sub will have no effect on the outside.

If you pass a variable ByRef, any changes made inside the Sub will affect it also on the outside.

It's all about who knows what, or "scoping"

I suggest you turn on Option Explicit, by the way. That way VS will flag any failures to declare (DIM) variables. You can either add the line

Option Explicit

At the top of your program, or find the corresponding setting in Tools-Options-Projects and Solutions-VB defaults. (May be in a different place, depending on which version of VS you are using).

As you have not shown your variable declarations (Dim statements) it's hard to work out what else is wrong.

And don't wory about being "bad at coding". We all had to start somewhere.

Here is my code I have made I hope it helps

Option Explicit On
Public Class Form1

    Dim yourName As String
    Dim monthName As String
    Dim day As Integer
    Dim year As Integer
    Dim sign As String
    Dim currentAge As Integer
    Dim nextYearAge As Integer
    Dim daysOld As Integer
    Dim secondsOld As Integer



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

    Private Sub ageNextYear(ByVal nextYearage As Integer)
        nextYearage = currentAge + 1
        Me.lblnextYearAge.Text = nextYearage.ToString
    End Sub



    Private Sub Yourdays(ByVal lbldaysOld)
        lbldaysOld.text = lblcurrentAge.Text * 365
    End Sub

    Private Sub yourSeconds(ByVal lblsecondsOld)
        lblsecondOld.Text = lbldaysOld.Text * 86400
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Call ageNextYear(currentAge)


        currentAge = System.DateTime.Now.Year - Me.txtYear.Text
        lblcurrentAge.Text = currentAge.ToString
        Call ageNextYear(currentAge)
        lblnextYearAge.Text = nextYearAge.ToString



        If Me.txtMonth.Text = 1 Then
            lblyourMonth.Text = "January"
        ElseIf Me.txtMonth.Text = 2 Then
            lblyourMonth.Text = "February"
        ElseIf Me.txtMonth.Text = 3 Then
            lblyourMonth.Text = "March"
        ElseIf Me.txtMonth.Text = 4 Then
            lblyourMonth.Text = "April"
        ElseIf Me.txtMonth.Text = 5 Then
            lblyourMonth.Text = "May"
        ElseIf Me.txtMonth.Text = 6 Then
            lblyourMonth.Text = "June"
        ElseIf Me.txtMonth.Text = 7 Then
            lblyourMonth.Text = "July"
        ElseIf Me.txtMonth.Text = 8 Then
            lblyourMonth.Text = "August"
        ElseIf Me.txtMonth.Text = 9 Then
            lblyourMonth.Text = "September"
        ElseIf Me.txtMonth.Text = 10 Then
            lblyourMonth.Text = "October"
        ElseIf Me.txtMonth.Text = 11 Then
            lblyourMonth.Text = "November"
        ElseIf Me.txtMonth.Text = 12 Then
            lblyourMonth.Text = "December"
        End If



    End Sub


End Class

@anaconda

Your If block will not work as you expect because you are comparing strings (txtMonth.Text) to integers. A date picker is a better method for date selection.

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.