Passing variables from Main function to another C#

2019-02-25 01:37发布

I'm beating my head against the wall pretty severely with this. I have several variables inside a C# console application that I would like to re-use. However, I cannot for the life of me re-use the variables in another class. I would love any help or pointers you could provide - I've searched for quite some time and I'm completely stumped.

EDIT: Yes, the variables are inside my Main function. Sorry for leaving this out.

EDIT: Heavily redacted code below. The variable values I'd like to re-use in another class are in the middle. There are more but those 3 should be sufficient for the sample. Thanks for the assistance!!!

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();
        }
    }
}

标签: c# main
4条回答
做个烂人
2楼-- · 2019-02-25 02:15

I think there's a dedicated forum for struts on this site, best look there for more info.

Quick answer: the primary way of passing values from one action to another (I think you are working with struts Action classes?) is to put the values into the request or session (so, first job for you would be to read up on those topics: HttpServletRequest and HttpSession). Struts action classes do their work in the execute() method, and that method has a parameter of type HttpServletRequest. From the request you can get a handle to the session.

And both request and session offer methods getAttribute() and setAttribute(). So, to pass data from one action to another, set that data as a (request or session) attribute, then read out the attribute in the next action again.

查看更多
淡お忘
3楼-- · 2019-02-25 02:25

If the variable denote some information about an object (like name, id, etc.) then they should be encapsulated in a class. The instance of the class (called an object) should be used to access this information.

As you already have the variables that represent an object, the next step would be to group these variables into classes. These variables are represented as properties in the class. The operations performed on these members should be available as methods. Furthermore the access modifiers decide the visibility of the members.

Going through your example, I can identify 3 variables that represent a Customer (assumption, I am not sure of the exact use case). These will form the Customer class.

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

Furthermore, the foreach loop can be split into 2 parts for resuablity

  1. Read from the xml nodes and create a list of customers
  2. Perform the database operations (like trigger stored procedures, write values, etc.) on the list of customers

Additionally, you can create another class that does the operations (business logic) that you are performing in the console application. This will allow you to reuse the same logic in case you move it to another application (like winforms or web service).

More information

查看更多
时光不老,我们不散
4楼-- · 2019-02-25 02:29

you should define public property or public field

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

and when you want to pass value you can assign this value to property

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

or you can use class constructor too.

查看更多
仙女界的扛把子
5楼-- · 2019-02-25 02:29

The Program class is probably Static so you'll have to access those fields by class name instead of instance.

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";
}
查看更多
登录 后发表回答