DownNode
DownNode

Reputation: 25

Getting a variable from another class and method on demand

I don't know if this is possible but what I'm trying to achieve is to get a sensitive password from a field only when needed and then clear it (without custom clear methods every time).

so I have this class/method to get :

public class letterhead
{
    public static letterhead phpClass;
    public string letterheadres;

    void Start()
    {
        phpClass = this;
    }

    public void Letterhead()
    {
        StartCoroutine(LetterheadAsync());
    }

    IEnumerator LetterheadAsync()
    {
        UnityWebRequest www = UnityWebRequest.Get($"https://mysite/letterhead1.php?dg=letterhead");
        yield return www.SendWebRequest();


        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {

            var result = www.downloadHandler.text;
            result = www.downloadHandler.text;
            Debug.Log(result);
            letterheadres = result;                
        }
    }
}

and this Class that I need the letterheadres:

  public class Password{
    private string letterhead3 = letterhead.phpClass.letterheadres;
}

now as you can see I have to first call the Letterhead() to get the letterheadres and clear it myself, what I'm trying to achieve is that when I need the letterhead3 from Password class, to get it from Letterhead() from letterhead class and then clear it automatically.

Upvotes: 0

Views: 51

Answers (1)

Afridi Kayal
Afridi Kayal

Reputation: 2295

Are you looking for something like this?

public class Password {
    public static string letterhead
    {
        get
        {
            string value = letterhead.phpClass.letterheadres; // Make a copy which you will return
            // Write your clearing procedure here
            // letterhead.phpClass.letterheadres = null;
            return value;
        }
    }
}

// You can get the value from other class as
string value = Password.letterhead; // If there is a value, You will get it and it will be cleared automatically due to the getter.

Make sure you have fetched the data before you access it. Moreover, this will work once. If you need the data again, You have to make the web request again and get the value after it is fetched. If you get the correct value, It is guaranteed that the contents (In the letterhead class) will be cleared.

Upvotes: 1

Related Questions