Solved

variable scope, c#

Posted on 2014-10-02
3
189 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 33

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 34

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 34

Author Comment

by:Mike Eghtebas
ID: 40357657
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Can't disable touch pad on Windows 10 computer 16 45
TimeZone, day light savings, Sql server, asp.net 6 38
jquery tab header text 1 23
Input parameteres to DragOver 2 20
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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