【发布时间】:2012-02-01 07:38:34
【问题描述】:
我的头撞墙了。我想重用的 C# 控制台应用程序中有几个变量。但是,我不能在我的生活中重新使用另一个类中的变量。我希望您能提供任何帮助或指示 - 我已经搜索了很长时间,但我完全被难住了。
编辑:是的,变量在我的 Main 函数中。很抱歉漏掉了这个。
编辑:下面大量编辑代码。我想在另一个类中重用的变量值在中间。还有更多,但那 3 个应该足以作为样本。感谢您的帮助!!!
public static class MyApp
{
static void Main(string[] args)
{
// loads XML doc
foreach (XmlNode node in nodes)
{
try
{
// does a bunch of stuff
// Parses variables from REST API
XDocument docdetailxml = XDocument.Parse(xmldoc);
XNamespace ns = docdetailxml.Root.GetDefaultNamespace();
var buid = docdetailxml.Root.Element(ns + "busid").Value;
var bname = docdetailxml.Root.Element(ns + "busname").Value;
var bcount = docdetailxml.Root.Element(ns + "buscount").Value;
// Invoke SQL connection string
// Trigger Stored Procedure and write values to database
// If needed, trigger email notification
// Close connections
}
catch (Exception e)
{
Console.WriteLine("Error encountered: " + e.Message);
// Exit the application
System.Environment.Exit(1);
}
finally
{
// Exit the application
// System.Environment.Exit(0);
}
}
}
private static void GetConnectionString()
{
throw new NotImplementedException();
}
private static void GetConnectionStrings()
{
throw new NotImplementedException();
}
}
}
【问题讨论】:
-
您是指传递给您的
Main函数的string[] args吗?你能展示一下你目前生成的代码吗? -
您有代码示例来说明问题所在吗?这对我们有一点帮助。
-
请发布您的控制台应用程序的代码。
-
不知道你在这里问什么。 “重复使用”是什么意思?