将变量从主函数传递到另一个C#类

将变量从主函数传递到另一个C#类,c#,main,C#,Main,这件事让我头痛不已。在一个C#控制台应用程序中有几个变量,我想重复使用。但是,我不能在另一个类中重复使用变量。我希望你们能给我任何帮助或指点——我已经找了很长时间了,我完全被难住了 编辑:是的,变量在我的主函数中。很抱歉把这个漏掉了 编辑:下面大量编辑的代码。我想在另一个类中重用的变量值在中间。还有更多,但这3个应足以用于样品。谢谢你的帮助 public static class MyApp { static void Main(string[] args)

这件事让我头痛不已。在一个C#控制台应用程序中有几个变量,我想重复使用。但是,我不能在另一个类中重复使用变量。我希望你们能给我任何帮助或指点——我已经找了很长时间了,我完全被难住了

编辑:是的,变量在我的主函数中。很抱歉把这个漏掉了

编辑:下面大量编辑的代码。我想在另一个类中重用的变量值在中间。还有更多,但这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();
        }
    }
}

如果变量表示关于对象的一些信息(如名称、id等),那么它们应该封装在一个函数中。应该使用类的实例(称为an)来访问此信息

由于已经有了表示对象的变量,下一步是将这些变量分组到类中。这些变量在类中表示为。在这些成员上执行的操作应可用。此外,还决定了成员的可见性

通过您的示例,我可以确定代表客户的3个变量(假设,我不确定确切的用例)。这些将构成Customer类

class Customer
{
    // You can either pass the UID through the constructor or 
    // expose a public setter to allow modification of the property
    public Customer(string uid)
    {
        this.UID = uid;
    }

    public string UID { get; private set; }
    public string Name { get; set; }
    public string Count { get; set; }
}
此外,
foreach
循环可分为两部分,以便于恢复

  • 从xml节点读取数据并创建一组客户
  • 对客户列表执行数据库操作(如触发器存储过程、写入值等)
  • 此外,您可以创建另一个类来执行您在控制台应用程序中执行的操作(业务逻辑)。这将允许您在将相同的逻辑移动到另一个应用程序(如winforms或web服务)时重用它

    更多信息


    您应该定义公共属性或公共字段

    public class Student
    {
    public string Name {get;set;}
    }
    
    当您想要传递值时,可以将该值指定给属性

    Student st = new Student(); 
    st.Name = "your value";
    

    或者您也可以使用类构造函数。

    程序类可能是静态的,因此您必须通过类名而不是实例来访问这些字段

    class Program
    {
        public string Name = "a name";
    
        static void Main(string[] args)
        {
            Name = "Hello"; //You can't do this, compile error
            Program p = new Program();
            p.Name = "Hi"; //You can do this
    
            SecondName = "Sn"; //You can do this
            Program.SecondName = "Tr"; //You can do this too
        }
        public static string SecondName = "Peat";
    }
    

    我认为在这个网站上有一个专门的struts论坛,最好在那里查看更多信息

    快速回答:将值从一个动作传递到另一个动作的主要方法(我认为您正在使用struts动作类?)是将值放入请求或会话(因此,您的第一项工作是阅读这些主题:HttpServletRequest和HttpSession)。Struts操作类在execute()方法中完成它们的工作,该方法有一个HttpServletRequest类型的参数。从请求中可以获得会话的句柄


    请求和会话都提供方法getAttribute()和setAttribute()。因此,要将数据从一个操作传递到另一个操作,请将该数据设置为(请求或会话)属性,然后在下一个操作中再次读取该属性

    您是指传递给
    Main
    函数的
    string[]args
    ?你能展示你到目前为止已经产生的代码吗?你有没有一个代码示例来说明哪里出了问题?这对我们有点帮助。请发布你的控制台应用程序的代码。不知道你在问什么。“重复使用”是什么意思?