Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

start c# from command line

Posted on 2013-02-05
7
Medium Priority
?
273 Views
Last Modified: 2013-02-11
I'm trying to run this program from command lines and  commented out static main in program.cs (see bottom of code).
What's wrong with this code?
The messages I get  are:

an object reference is required for  the non-static field, method, or property 'ILXSymbolMasterExtract.Form1.arrAttrBond'

An object reference is required for the non-static field, method, or property 'ILXSymbolMasterExtract.Form1.ParseXml(string, string, string, string, string[])'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Xml.XPath ;
namespace ILXSymbolMasterExtract
{
    public partial class Form1 : Form
    {


        string[] arrAttrBond = {
                                "sedol","curr","localId","issuShortDesc","exch","longIssuerName", "symbol",
                                "action","sectyType","cusip","issuLongDesc","localCode","dfltInd",
                                "mat","isin","sess","issuerName"
                           };
        string[] arrAttrInd = {
                                "sedol","curr","localId","issuShortDesc","exch","longIssuerName", "symbol",
                                "action","sectyType","cusip","issuLongDesc","localCode","dfltInd",
                                "isin","sess","issuerName","sicCode"
                           };

        string[] arrAttrTr = {
                                "sedol","issuerName","mat","symbol","sess","issuShortDesc","cusip",
                                "issuLongDesc","longIssuerName","sectyType","action","isin","exch","curr", 
                           };

        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form1_Load);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "hello";
        }
        static void Main()
        {
            try
            {
                string[] args = Environment.GetCommandLineArgs();
                string xsdfile = args[2];//ignored
                string inputfile = args[3];
                string Outputfile = args[4];
                string InstrumentType = args[5];
                switch (InstrumentType)
                {
                    case "bond":
                        ParseXml(inputfile, Outputfile, InstrumentType, "bnd", arrAttrBond);
                        break;
                    case "index":
                        ParseXml(inputfile, Outputfile, InstrumentType, "ind", arrAttrInd);
                        break;
                    case "treasury":
                        ParseXml(inputfile, Outputfile, InstrumentType, "tr", arrAttrTr);
                        break;
                }
                //XmlDeclaration xmldecl;
                //xmldecl = doc.CreateXmlDeclaration("1.0", null, null);

                ////Add the new node to the document.
                //XmlElement root = doc.DocumentElement;
                //doc.InsertBefore(xmldecl, root);
                //Console.WriteLine("Display the modified XML...");
                //doc.Save(Console.Out);
            }

            catch (Exception ex)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
            Environment.Exit(0);
        }
        private void ParseXml(string fn, string fnout, string InstTypeMain, string InstTypeMinor, string[] AttributeInst)
        {

            using (Stream stream = File.OpenRead(fn))
            {

                XmlTextReader r = new XmlTextReader(stream);
                r.WhitespaceHandling = WhitespaceHandling.None;
                r.MoveToContent();

                using (XmlTextWriter w = new XmlTextWriter(fnout, null))
                //using (XmlTextWriter w = new XmlTextWriter(fnout, Encoding.Unicode))
                {
                    w.Formatting = Formatting.Indented;
                    w.Indentation = 3;
                    //w.IndentChar = '\t';
                    w.IndentChar = ' ';
                    w.WriteStartDocument();
                    w.WriteStartElement("data");

                    int intParsingState = 0;

                    // changed the loop structure to start at root (not really necessary) but also to be able to use "continue" to skip r.Read() after r.Skip()...
                    do
                    {
                        switch (r.NodeType)
                        {
                            case XmlNodeType.Element:

                                if (r.LocalName == InstTypeMain)
                                {
                                    intParsingState = 1;
                                }
                                else if (r.LocalName == InstTypeMinor)
                                {
                                    if (intParsingState > 0)
                                    {
                                        w.WriteStartElement("n");
                                        foreach (string a in AttributeInst)
                                        {
                                            w.WriteElementString(a, r.GetAttribute(a).ToString());
                                        }
                                        w.WriteEndElement();
                                    }

                                }
                                else
                                {
                                    // skip children of other elements on the same depth as the <bond> element
                                    if (r.Depth >= 1)
                                    {
                                        r.Skip();
                                        continue; // don't call r.Read() in this case...
                                    }

                                }

                                break;
                            case XmlNodeType.EndElement:

                                if (r.LocalName == InstTypeMain)
                                {
                                    intParsingState = 2; // break do-while
                                }

                                break;
                            default:
                                break;
                        }
                        if (intParsingState < 2 && !r.Read())
                            break;
                    } while (intParsingState < 2);
                    w.WriteEndElement();
                    w.WriteEndDocument();
                    w.Flush();
                    w.Close();
                }
                r.Close();
            }
        }
        
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ILXSymbolMasterExtract
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        //[STAThread]
        //static void Main()
        //{
        //    string[] args = Environment.GetCommandLineArgs();
        //    string xsdfile = args[2];//ignored
        //    string inputfile = args[3];
        //    string Outputfile = args[4];
        //    string InstrumentType = args[5];



        //    // Display the number of command line arguments:
        //    // System.Console.WriteLine(args.Length);
        //    //string xsdfile;

        //    Application.EnableVisualStyles();
        //    Application.SetCompatibleTextRenderingDefault(false);
        //    Application.Run(new Form1());
        //}
    }
}

Open in new window

0
Comment
Question by:AlHal2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38856016
You try to call non static parseXml from ststic main function.
What u wanna do is take the code from main and put it in non static Init function.
In your main function call:
var form = new Form();
form.Init();
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38856023
If parsexml is a service function which means it doesn't need access class members you can declare it as static to fix the compile error
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 38856028
As sedgwick says, you can't use non-static fields in a static method. For example:

public class myClass
{
   private string myString = "hello";
   public static void doSomething()
   {
      string hello = myString;
   }
}

Open in new window


This simply won't work. In fact, I'm surprised this compiles.

Either make the fields you're using static also or do what Sedgwick says.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:AlHal2
ID: 38856066
I'm getting this message.

'System.Windows.Forms.Form' does not contain a definition for 'Init' and no extension method 'Init' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38856080
As i explained, add your own function called Init, and take all the cofe from Main and place it there.
0
 

Accepted Solution

by:
AlHal2 earned 0 total points
ID: 38858789
Is this what you mean?  I'm still getting the same error.

static void Main()
        {
            var form = new Form();
            form.Init();
        }
        private void Init()
        {
        try
            {
                string[] args = Environment.GetCommandLineArgs();
                string xsdfile = args[2];//ignored
                string inputfile = args[3];
                string Outputfile = args[4];
                string InstrumentType = args[5];
                switch (InstrumentType)
                {
                    case "bond":

                        ParseXml(inputfile, Outputfile, InstrumentType, "bnd", arrAttrBond);
                        break;
                    case "index":
                        ParseXml(inputfile, Outputfile, InstrumentType, "ind", arrAttrInd);
                        break;
                    case "treasury":
                        ParseXml(inputfile, Outputfile, InstrumentType, "tr", arrAttrTr);
                        break;
                }
            }

            catch (Exception ex)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
            Environment.Exit(0);
        }
0
 

Author Closing Comment

by:AlHal2
ID: 38875309
This works.

public static void Main()
        {
           
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
                       
        }

private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string[] args = Environment.GetCommandLineArgs();
                //MessageBox.Show(args[0] + "|" + args[1] + "|" + args[2] + "|" + args[3] + "|" + args[4] + "|" + args[5]);
                //Environment.Exit(0);
                string xsdfile = args[1];//ignored.  Increment values in args by 1 when running source code
                string inputfile = args[2];
                string Outputfile = args[3];
                string InstrumentType = args[4];
                //MessageBox.Show("Instrument Type = " +InstrumentType);
                switch (InstrumentType)
                {
                    case "bond":
                        ParseXml(inputfile, Outputfile, InstrumentType, "bnd", arrAttrBond);
                        break;
                    case "index":
                        ParseXml(inputfile, Outputfile, InstrumentType, "ind", arrAttrInd);
                        break;
                    case "treasury":
                        ParseXml(inputfile, Outputfile, InstrumentType, "tr", arrAttrTr);
                        break;
                }
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question