【问题标题】:Passing variables from Main function to another C# class将变量从 Main 函数传递到另一个 C# 类
【发布时间】: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 吗?你能展示一下你目前生成的代码吗?
  • 您有代码示例来说明问题所在吗?这对我们有一点帮助。
  • 请发布您的控制台应用程序的代码。
  • 不知道你在这里问什么。 “重复使用”是什么意思?

标签: c# main


【解决方案1】:

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

public class Student
{
public string Name {get;set;}
}

当你想传递值时,你可以把这个值赋给属性

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

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

【讨论】:

    【解决方案2】:

    如果变量表示有关对象的某些信息(如名称、id 等),则应将它们封装在 class 中。应该使用类的实例(称为object)来访问此信息。

    由于您已经拥有代表对象的变量,下一步是将这些变量分组到类中。这些变量在类中表示为properties。对这些成员执行的操作应以methods 的形式提供。此外,access modifiers 决定了成员的可见性。

    通过您的示例,我可以确定代表客户的 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 循环可以拆分为 2 部分以实现可重复性

    1. 从 xml 节点读取并创建 list 的客户
    2. 对客户列表执行数据库操作(如触发存储过程、写入值等)

    此外,您可以创建另一个类来执行您在控制台应用程序中执行的操作(业务逻辑)。这将允许您重用相同的逻辑,以防您将其移动到另一个应用程序(如 winforms 或 Web 服务)。

    更多信息

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

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

        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";
        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-08
          • 1970-01-01
          • 1970-01-01
          • 2021-04-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多