我正在更新一个Windows表单应用程序,该应用程序将不同的货币相互转换(例如,英镑到欧元,反之亦然(。我的一个想法是创建一个利用 Web API 框架的类,以便:
- 启动程序时下载最新汇率
- 创建单独包含这些速率的变量
- 将这些变量私下存储在类中,并在每种形式中引用相关变量
(不幸的是,作业需要多表单布局 - 否则我会在单个表单中使用下拉菜单。
这是我到目前为止在控制台应用程序中用于规划和测试的代码:
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
}
在测试了数组赋值之后,双精度变量"dollarToPounds"和"dollarToEuros"已被证明在"using(Stream...("语句的上下文中具有正确的值 - 但是,由于"using"的属性,这些变量没有在其上下文之外被赋值(在 Main(( 开头声明变量不会影响此结果(。
是否有一种方法结构可以代替,它将执行与"使用(Stream...("相同的任务,即读取提供的URL的内容,然后允许在同一类中的其他地方引用变量?这将允许我在类外引用它。
如果您需要我进一步解释代码,请务必询问。
感谢托尼的建议,我已经为我提出的上述问题实施了解决方案! 关键是在输入实现 WebAPI 和 StreamReader 的方法之前声明变量 dollarToPounds 和 USDTOEURS,并在这样做之前在方法开始时调用类的实例:
class Program
{
double dollarsToPounds;
double dollarsToEuros;
static void Main(string[] args)
{
Program program = new Program();
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];
program.dollarsToPounds = Convert.ToDouble(dTPString);
}
if (s.Equals(""EUR""))
{
int element = Array.IndexOf(streamArray, s);
string dTEString = streamArray[element + 1];
program.dollarsToEuros = Convert.ToDouble(dTEString);
}
}
}
Console.WriteLine(program.dollarsToPounds); //works!
Console.WriteLine(program.dollarsToEuros); //works!
}
}
结构,简化:
class ClassName
{
//variables go here
static void MethodName()
{
ClassName instance = new ClassName();
//Web API, using StreamReader, arrays, nested foreach-if statements go here
//variables can now be referenced outside of "using" context
}
}
再次感谢您!当我明天给他看这个时,我的讲师可能会觉得有点傻。
你可以移动 的声明
double dollarsToPounds
和
double dollarsToEuros
到 行前
static void Main(string[] args)