Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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

0
TeDeSm
Asked:
TeDeSm
1 Solution
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now