Solved

variable scope, c#

Posted on 2014-10-02
3
180 Views
Last Modified: 2014-10-02
I have the following code.

Question: How I can revise it such csString will be visible in behind code of the form (see line 12)?

namespace CodeSampleCS
{
    public partial class frmCS01 : Form
    {
        public frmCS01()
        {
            InitializeComponent();
        }

        private void frmCS01_Load(object sender, EventArgs e)
        {
            MessageBox.Show("msg form");  //<-- cnString not available here 
        }
    }
}

namespace CodeSampleCS
{
    
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        public static string cnString="";
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            cnString = "xxx"; xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01());
            MessageBox.Show("msg 2" + cnString);
        }
    }
}

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40357567
You have multiple ways to handle this, but the safest would probably be with something like:
namespace CodeSampleCS
{
    public partial class frmCS01 : Form
    {
        private string cnString = string.Empty;
        public frmCS01(string argument)
        {
            if (!string.IsNullOrEmpty(argument))
                        cnString = argument;
            InitializeComponent();
        }

        private void frmCS01_Load(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("msg form{0}", !string.IsNullOrEmpty(cnString) ? string.Format(" - {0}", cnString) : ""));  //<-- cnString not available here 
        }
    }
}

namespace CodeSampleCS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            cnString = "xxx"; xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01(cnString));
            MessageBox.Show("msg 2" + cnString);
        }

        public static string cnString { get; set; }
    }
}

Open in new window


-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357617
Hi saige,

Perfect.

Also, line:  cnString = xmlReader.ReadElementString("connection");

was complaining the xml file is not in Debug folder. I had to manually copy and past it there.

I wonder why the xml file was not saved in the right location when I created it?

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357657
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now