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!

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.

This article has been dead for over six months. Start a new discussion instead.