Search code examples
c#refresh

Refresh userControl from outside class


I have an application that displays result data from a database. Occasionally if a result comes in with additional data then we append the record. In the UserControl the values are "NA" until this happens. I have a refreshdisplay() method within the UserControl class that works beautifully, unfortunately if I call it from my dataHandler class after using new the UserControl does not refresh. I am guessing this is because I used

UserControlResultDisplay resultDisplay = new UserControlResultDisplay();

How can I use a method and interact with the existing UserControl without instantiating a new?

Here are some code snippets:

UserControlResultDisplay resultDisplay = new UserControlResultDisplay();

public void UpdateResultDB(ResultDataJFTOT resultData)
{    
    AnalysisListCommon myresult = PContext.GetInstance().DbHandlerLocal.StoredResult(
        resultData.SampleId,
        resultData.TestDate.ToString("yyyy-MM-ddTHH:mm", CultureInfo.InvariantCulture),
        resultData.InstrumentSn,
        StringRepository.constStringSampleName);
    if (myresult != null)
    {
        Result r = new Result(new Guid(myresult.ResultId));
        ResultData rd = r.GetResultData("Rating", FindResultDataMode.byVariableIdentifier);
        string xmlTubeRating = resultData.tRating.ToString().Replace("#LT#", "<");
        rd.Text = xmlRating;
        rd.Store();
        rd = r.GetResultData("TestDate", FindResultDataMode.byVariableIdentifier);
        rd.Text = resultData.Date.ToString();
        rd.Store();
        resultDisplay.RefreshDisplay();
    }
    else
    {
        AddTestToQueue(resultData);
    }   
}

Solution

  • As you pointed out, the problem lies in the statement:

    UserControlResultDisplay resultDisplay = new UserControlResultDisplay();
    

    This instantiates a new object (no relation to the existing one) so running functions on it has absolutely no chance of affecting what is displayed on the GUI.

    Before I get to the correct code, let me try and clear up what an "Object" is.

    Every type in C#, whether explicitly or implicitly, can be considered an "Object" (even value types like int), so that distinction is largely irrelevant. You are correct, however, in saying that there are two classifications of objects:

    1. Value Types which are passed (not surprisingly) by value, and do not need/allow the new keyword during creation. These are your int, float, char etc.
    2. Reference Types which are passed by reference and do use the new keyword to create them. These are anything declared with class (possibly a few others, but thats a good rule of thumb).

    Both are perfectly acceptable parameters to any function, they are just passed differently. For what you are trying to do, we actually take advantage of this. If you write:

    UserControlResultDisplay resultDisplay;
    
    public MyDataClass(UserControlResultDisplay uiDisplay)
    {
        resultDisplay = uiDisplay;
    }
    

    You are getting (and storing) a reference to the existing UI object. Now when you call functions on it, it will affect the UI (assuming everything on the UI side is set up correctly of course). You would invoke this constructor like this (assuming it is from the UI class):

    MyDataClass data = new MyDataClass(this);
    

    If you invoke it somewhere else, you obviously need a reference to the UI from somewhere else to pass to MyDataClass.