Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to hold value into Global variable from a Windows Form in a C# DLL
    primarykey
    data
    text
    <p>I have a DLL which has the API calls for certain Trade related functions (getAccounts, getPositions, CreateOrders etc). Most of the API calls require an Access Token in order to get the required functionality. The Access Token is obtained by showing a Windows Form to the user which takes input and sets the Access Token on a button click event. I have my Global variable for the access token set in the following class:</p> <pre><code>namespace TradeAddin { public sealed class MyConstants { //User Access Token public static string user_token; } } </code></pre> <p>I am setting the value for the above token as <code>MyConstants.user_token = strAccessToken;</code> from the Form <code>btSubmitAuthCode_Click</code> event that I have added to the Solution as follows:</p> <pre><code> namespace TradeAddin { public partial class TradeLoginAuthForm : Form { public TradeLoginAuthForm () { InitializeComponent(); btSubmitAuthCode.Enabled = false; } private void btSubmitAuthCode_Click(object sender, EventArgs e) { string strAuthCode = txtAuthCode.Text.Trim(); string strAccessToken = TradeApiCalls.getAccessToken(strAuthCode); if (strAccessToken != null) { MyConstants.user_token = strAccessToken; this.Close(); } else { MessageBox.Show("Invalid Authorization Code. Please ReEnter!", "Trade Exception: Invalid Authorization Code", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void txtAuthCode_TextChanged(object sender, EventArgs e) { btSubmitAuthCode.Enabled = !(txtAuthCode.Text.Trim() == String.Empty); } } } </code></pre> <p>I close the Windows Form after getting the AccessToken. Now when I initiate other API calls (From DLL initialize method) that require AccessToken, I get <code>null</code> returned for AccessToken variable which I am using for the below API Calls:</p> <pre><code>namespace TradeAddIn { public class TradeApiCalls { public string getAccounts() { if (string.IsNullOrEmpty(MyConstants.user_token)) { TradeLoginAuthForm TradeForm = new TradeLoginAuthForm (); TradeForm.Show(); } string URI = "https://api.trade.com/a1/account/id"; using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.Authorization] = "Bearer " + MyConstants.user_token; </code></pre> <p>How do I retain the value of the Global variable in order to access it later? Any help is greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload