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.