Solved

public variable. c#

Posted on 2014-10-02
6
145 Views
Last Modified: 2014-10-02
The good solution from it_siege (http://www.experts-exchange.com/Programming/Languages/.NET/Q_28530111.html) uses arguments to pass cnString variable.

Question: Could you please revise this code to do it using a public variable?

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) : "")); 
        }
    }
}
--------------------------------------------
namespace CodeSampleCS
{
    static class Program
    {
        public static string cnString = "";
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            cnString = xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01(cnString));
            MessageBox.Show("msg 2" + cnString);
        }
    }
}

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
  • 2
6 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 40357706
It should be exceedingly rare that you ever expose a variable (a.k.a. a field) publicly. Best practice is to create a public property that exposes the variable.

What is the goal here?
0
 
LVL 32

Accepted Solution

by:
it_saige earned 400 total points
ID: 40357709
To do it using a public variable (which is generally frowned upon) would take a couple of modifications (depended upon the way you wanted to do it -

1.  Using an instance variable passed as an argument:
namespace CodeSampleCS
{
    public partial class frmCS01 : Form
    {
        private Program caller = null;
        public frmCS01(Program argument)
        {
            if (argument != null)
                caller = argument;

            InitializeComponent();
        }

        private void frmCS01_Load(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("msg form{0}", !string.IsNullOrEmpty(caller.cnString) ? string.Format(" - {0}", caller.cnString) : "")); 
        }
    }
}
--------------------------------------------
namespace CodeSampleCS
{
    class Program
    {
        public string cnString = "";
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            cnString = xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01(this));
            MessageBox.Show("msg 2" + cnString);
        }
    }
}

Open in new window


2.  Implement a globals class:
namespace CodeSampleCS
{
    public class Globals
    {
        private static string cnString = string.Empty;
        public static string ConnectionString
        {
            get { return cnString; }
            set 
            {
                if (!string.IsNullOrEmpty(value) && !value.Equals(cnString))
                    cnString = value;
            }
        }
    }
}

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

        private void frmCS01_Load(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("msg form{0}", !string.IsNullOrEmpty(Globals.ConnectionString) ? string.Format(" - {0}", Globals.ConnectionString) : "")); 
        }
    }
}
--------------------------------------------
namespace CodeSampleCS
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            Globals.ConnectionString = xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + Globals.ConnectionString);
            Application.Run(new frmCS01());
            MessageBox.Show("msg 2" + Globals.ConnectionString);
        }
    }
}

Open in new window


-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357764
kaufmed,

With vba background, having a global variable and using everywhere has its advantages.

I appreciate good comments and suggestion from both and follow it accordingly.

The current question is also good to know to be used when it is appropriate but not reading connection string.

I have a follow up question as to why cnString read from the xml file is different. I will post a link shortly.

Mike
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40357774
With vba background, having a global variable and using everywhere has its advantages.
Yes, that's typically the feeling of people who come from backgrounds where globals are prevalent. But as you program (object-oriented) more and more, and your programming becomes more advanced, you will begin to see how globals actually can do more harm that good. They especially make parallel programming difficult.
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40357780
Parrallel programming and threading are two examples of why I stated
which is generally frowned upon
@kaufmed - I just now realized that your avatar is not a pirate.  :D

-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40357823
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

18 Experts available now in Live!

Get 1:1 Help Now