2012-01-03 169 views
3

我正在对着墙壁狠狠地敲打着我的头。我在C#控制台应用程序中有几个我想要重用的变量。但是,我不能在我的生活中重新使用另一个班的变量。我很乐意提供任何帮助或指引 - 我已经搜索了很长时间,而且我完全陷入了困境。将变量从Main函数传递到另一个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(); 
     } 
    } 
} 
+0

你是指传递给你的'Main'函数的'string [] args'吗?你能显示迄今为止制作的代码吗? – Default 2012-01-03 07:53:49

+0

你有代码示例来显示出现问题吗?这会帮助我们一点。 – 2012-01-03 07:53:57

+0

请发布您的控制台应用程序的代码。 – 2012-01-03 07:53:57

回答

2

如果变量表示关于物体(例如姓名,ID等)的一些信息那么就应该在class进行封装。应该使用该类的实例(称为object)来访问此信息。

由于您已经有表示对象的变量,下一步就是将这些变量分组到类中。这些变量在该类中表示为properties。对这些成员进行的操作应为methods。此外,access modifiers决定成员的可见性。通过您的示例,我可以确定代表客户的3个变量(假设,我不确定确切的用例)。这些将形成客户类。

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节点被分成2份为resuablity

  1. 读取并创建客户
  2. list
  3. 执行数据库操作(如触发存储过程,写入值等)

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

更多信息

2

你应该定义公共财产或公共领域

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

,当你想传递价值,你可以将此值赋给财产

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

,或者您可以使用类的构造函数了。

-1

程序类可能是静态的,所以你必须通过类名而不是实例来访问这些字段。

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

我认为这个网站有一个专门的struts论坛,最好看看有更多的信息。

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

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

相关问题