Desktop productivity for business analysts and programmers

Passing text to the settings property from Main and Form classes in EG Custom Task in VB.Net

Reply
Frequent Contributor
Posts: 81

Passing text to the settings property from Main and Form classes in EG Custom Task in VB.Net

I'm trying to generate some code to run in EG 5.1 that is based on 3 List Box objects in VB.Net.  I'd like to pass a comma-separated list of values from the Form class, via the Settings class, to the Main class of an EG Custom Task.  I've used the following VB.Net (VS 2010) code to pass the _settings object from Main to Form, but, even though I get no errors, nothing appears back in my GetSasCode routine when I read the _settings properties back in Main.

In Main:

    Private _settings As New Settings

        form._settings = _settings

        form.ShowDialog(Owner)

        ' save the settings from the form

        _settings = form._settings

    Public Overrides Function GetSasCode() As String

        ' generate the SAS program and return as a String

        Return _settings.ToSasProgram()

    End Function

In Form:

    Public _settings As Settings

        Dim sb As New StringBuilder()

        Dim i As Integer

        If lstStyle.SelectedItems.Count > 0 Then

            For i = 0 To (lstStyle.SelectedItems.Count - 1)

                sb.AppendFormat(",{0}", lstStyle.SelectedItems(i).ToString())

            Next

        End If

        If lstStatgraph.SelectedItems.Count > 0 Then

            For i = 0 To (lstStatgraph.SelectedItems.Count - 1)

                sb.AppendFormat(",{0}", lstStatgraph.SelectedItems(i).ToString())

            Next

        End If

        If lstTable.SelectedItems.Count > 0 Then

            For i = 0 To (lstTable.SelectedItems.Count - 1)

                sb.AppendFormat(",{0}", lstTable.SelectedItems(i).ToString())

            Next

        End If

        _settings._runcode = sb.ToString().Substring(1)

In Settings:

    Public Property _runcode As String

In Settings ToSasProgram:

        Dim sb As New StringBuilder()

        Dim run_code1 As String

        Dim template_name As String

        run_code1 = _runcode

        While Not String.IsNullOrEmpty(run_code1)

            template_name = run_code1.Substring(0, run_code1.IndexOf(","))

            sb.AppendFormat(" source {0};", template_name)

            sb.AppendLine()

            run_code1 = run_code1.Substring(run_code1.IndexOf(",") + 1)

        End While

_runcode appears to contain nothing.

Why is this happening?stom............Phil

Community Manager
Posts: 2,889

Re: Passing text to the settings property from Main and Form classes in EG Custom Task in VB.Net

Phil,

A couple of observations...

- assuming that your Form class has an OK and Cancel button, you should be checking that the DialogResult from your ShowDialog() call is DialogResult.OK before you "commit" the setting values in your task class.  (See also: 10 tips for creating dialog windows like the pros - The SAS Dummy).

- you're missing a very important part of the task: the part where you save the task settings and then restore them via the XmlState property.  In the SasTask-derived class, this is done by overriding the GetXmlState (to save) and RestoreStateFromXml (to restore).  The convention is to pass the values via XML, but that is not strictly required.  So for your task, you could add this to your task class ("Main"):

    Public Overrides Function GetXmlState() As String

        Return _settings

    End Function

    Public Overrides Sub RestoreStateFromXml(ByVal xmlState As String)

       _settings = xmlState

    End Sub

This step is necessary because your task class instance is not reused from the "design phase" where you work with the UI to the "run" phase where your class generates a program to run within EG.

Think of your task class as an empty container.  During "design" (when you see the UI), you are collecting information that is needed to fulfill the task's purpose.  After you click OK and close the UI, the information is stored away in your project, and your task class (container) goes away (freeing memory).

When it comes time to run the task (which may be just milliseconds later), a new task container is created, and the stored information from the project is "poured" into it (via the XmlState property, or the above override functions).  This -- along with the Consumer.ActiveData, perhaps -- provides you with the state information you need to create the SAS program that you want to run.

Chris

Frequent Contributor
Posts: 81

Re: Passing text to the settings property from Main and Form classes in EG Custom Task in VB.Net

Chris,

Once again your answer has helped me resolve my problem.  Although you suggested that I didn't need to convert to XML, I ended up using the ToXml and FromXml routine supplied in the VB.Net template (for simplicity), and borrowing some code from the BasicStatsVB_withUI project to pass the _settings data to and from the form.  I also moved the ToSasProgram function from Settings to Main, so that it was with the related code.

However, I think I now understand why my old "Proc Report" custom task, that worked fine in EG 4.1, stopped working in EG 4.2, and I think I now understand how to fix it.  This means I have yet another EG Add-in project to complete!

I know you don't like VB.Net, but thanks for tolerating it enough to help me out.

Many thanks...........Phil

PS. I am building a template extractor for ODS Styles, Statgraphs and Tables.

Ask a Question
Discussion stats
  • 2 replies
  • 244 views
  • 3 likes
  • 2 in conversation