How to reference a control on a Form from a Public Clas

I have a form which passes some values to a sub in a Public Class. The sub performs some calculations and passes a string back to a textbox on the calling form. The method I have did work some time ago, but now no longer works.

What is the best way to do this?
' From command button on calling form
Call CalcPlanetPositions.Calc_Planet_Positions(geo_lon, geo_lat, altitude_above_sea, birthDateTime)
'..............

'<PUBLIC CLASS>
Public Class CalcPlanetPositions

Public Shared Sub Calc_Planet_Positions(ByVal GeoLon As Double, ByVal GeoLat As Double, _
     ByVal AltitudeAboveSea As Double, ByVal BirthDateTime As DateTime)

        ' Instanstiate a form type to enable results to be passed back to a form control
        Dim _f As frmCalculateChart = CType(My.Application.OpenForms("FormCalculate"), frmCalculateChart)

'<SNIP>
' _f Throws a Null Exception on next line

        _f.txtResults.Text = "Birth date: " & BirthDate & " Birth time: " & BirthTime & vbCrLf & _
         "Sid: " & CommonFunctions.Dec2hms(JulDaySidTime) & vbCrLf & ""

Open in new window

TeDeSmAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Change your sub declaration from:

    Public Shared Sub Calc_Planet_Positions(ByVal GeoLon As Double, ByVal GeoLat As Double, _
        ByVal AltitudeAboveSea As Double, ByVal BirthDateTime As DateTime)

To:

    Public Shared Sub Calc_Planet_Positions(ByVal frmCalc As FormCalculate, ByVal GeoLon As Double, ByVal GeoLat As Double, _
        ByVal AltitudeAboveSea As Double, ByVal BirthDateTime As DateTime)

Note that the first parameter is now a reference to the form.

In the form, the call will look like this, using "Me" to pass the Form:
' From command button on calling form
    Call CalcPlanetPositions.Calc_Planet_Positions(Me, geo_lon, geo_lat, altitude_above_sea, birthDateTime)

Open in new window


Back in Calc_Planet_Positions(), you'll use "frmCalc" to update the Form:  
Public Shared Sub Calc_Planet_Positions(ByVal frmCalc As FormCalculate, ByVal GeoLon As Double, ByVal GeoLat As Double, ByVal AltitudeAboveSea As Double, ByVal BirthDateTime As DateTime)

        ' Use the Form reference, "frmCalc", that was passed in:
        frmCalc.txtResults.Text = "Birth date: " & BirthDate & " Birth time: " & BirthTime & vbCrLf & _
            "Sid: " & CommonFunctions.Dec2hms(JulDaySidTime) & vbCrLf & ""
        
        ' ...

    End Sub

Open in new window

0
 
TeDeSmAuthor Commented:
Great solution. the application is working again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.