我正在用我的头撞墙。我想重复使用 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();
}
}
}
你应该定义公共财产或公共领域
public class Student
{
public string Name {get;set;}
}
当你想传递值时,你可以将该值分配给属性
Student st = new Student();
st.Name = "your value";
或者你也可以使用类构造函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)