Search code examples
c#classasp.net-web-apistreamreaderusing

C#, how do I use WebAPI to create variables within a class, which can then be referenced in a different class?


I am updating a Windows Form App which converts different currencies into each other (e.g. Pounds to Euros and vice-versa). One idea I've had is to create a class which utilises a Web API framework in order to:

  • download the latest exchange rates upon starting the program
  • create variables which individually contain these rates
  • store these variables privately within the class and reference the relevant variables in each form

(Unfortunately the assignment REQUIRES a multiform layout - otherwise I would have used drop-down menus in a single form.)

This is the code which I have utilised thus far - in a Console App for the purposes of planning and testing:

static void Main(string[] args)
    {
        string URL = "https://openexchangerates.org/api/latest.json?app_id=4837847d2bc64fc496cf325525c5cf0d";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.ContentType = "application/json; charset=utf-8";
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes("GBP"));
        request.PreAuthenticate = true;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            string streamString = reader.ReadToEnd();
            string[] streamArray = Regex.Split(streamString, "\n|: |,\\s*");

            foreach (string s in streamArray)
            {
                if (s.Equals("\"GBP\""))
                {
                    int element = Array.IndexOf(streamArray,s);
                    string dTPString = streamArray[element + 1];
                    double dollarsToPounds = Convert.ToDouble(dTPString);
                }

                if (s.Equals("\"EUR\""))
                {
                    int element = Array.IndexOf(streamArray, s);
                    string dTEString = streamArray[element + 1];
                    double dollarsToEuros = Convert.ToDouble(dTEString);
                }
            }
        }

        Console.WriteLine(dollarsToPounds); //does not work
        Console.WriteLine(dollarsToEuros); //does not work
    }

Having tested the array assignments and both double variables "dollarsToPounds" and "dollarsToEuros" have been proven to possess their correct values within the context of the "using (Stream...)" statement - however, due to the properties of "using", these variables have not been assigned values outside of its context (declaring the variables at the start of Main() does not affect this outcome).

Is there a method structure I could use instead which would perform the same task as "using (Stream...)" i.e. to read the contents of the provided URL, which would then allow for the variables to be referenced elsewhere within the same class? This would then allow me to reference it outside of the class.

If you need me to explain the code further then by all means ask.


Solution

  • You can move the declarations of

    double dollarsToPounds 
    

    and

    double dollarsToEuros 
    

    to before the line

    static void Main(string[] args)