I know you guys MUST be tired of scope questions ... but I need some help getting through this fog ...

The following code works (notice where I instanced 'cp') ....

public partial class frmMain : Form
    {
        ConnectParams cp = new ConnectParams();

        public frmMain()
        {
            // Fires up the form
            InitializeComponent();
        }

        // Menu File Exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient(cp.GetServerAddress(), cp.GetPortNumber());
        }
    }
}

However, the following code does not (notice where I instance 'cp') ... I get a "cp does not exist in this context" error for the button1_Click event.

namespace AQuickShoutOut
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            ConnectParams cp = new ConnectParams();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient(cp.GetServerAddress(), cp.GetPortNumber());
        }
    }
}

My question is ... doesn't the first example make the cp object global? I don't really want it (or need it) to be global.

But more importantly, why doesn't the second example work? It seems to me, instanced where it is, that it should be available to all the methods and events of frmMain.

So, yeah, I'm not asking you how to fix it ... I'm asking someone to beat it into my thick skull why the first version does, and the second version does.

I'm soooo frustrated with this whole scope thing in C# because I'm used to being lazy and just making crap global unless its specifically used in the function or method.

So now I'm trying to train myself to make nothing global ... and it just doesn't seem to be cooperating.

Thanks in advance.

Recommended Answers

All 3 Replies

By the way ... before anyone thinks I'm a total idiot, I do understand that the second instancing is done in the constructor for the form ... that hasn't escaped me.

That's more or less why it made sense to me to put it there first.

> doesn't the first example make the cp object global?
Perhaps it could be described as global within the frmMain class.

> I don't really want it (or need it) to be global.
You might not want it to be, but you *do* need it to be if more than one method is going to access it like that.

> why doesn't the second example work?
An easy way to think of basic scope is by looking at parentheses. An object can only be accessed in the same parentheses that it is declared in.

class Scoper
{
    int a;

    Scoper()
    {
        int b;

        // a and b are visible here
    }

    void method1()
    {
        int c;

        // a and c are visible here

        if (a != 0)
        {
            int d;
            // a, c and d are visible here
        }

        // a and c are visible here
    }

    void method2()
    {
        int e;

        // a and e are visible here

        if (a != 0)
        {
            int f;
            // a, e and f are visible here
        }

        // a and e are visible here
    }
}

> doesn't the first example make the cp object global?
Perhaps it could be described as global within the frmMain class.

> I don't really want it (or need it) to be global.
You might not want it to be, but you *do* need it to be if more than one method is going to access it like that.

> why doesn't the second example work?
An easy way to think of basic scope is by looking at parentheses. An object can only be accessed in the same parentheses that it is declared in.

I see ... so in general, its in the constructor method and goes away when the constructor is finished?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.