T
truevision.net
Hi,
I have a slight problem with how things work that I'm sure I'm
missing. I don't really get it how I am supposed to get references
between classes and instances of them. Example:
using System;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Object references
public Database db;
// Methods
public void openToolStripMenuItem_Click(object sender,
EventArgs e)
{
Database db = new Database();
}
private void closeToolStripMenuItem_Click(object sender,
EventArgs e)
{
db.SaveAs();
}
}
}
I have a seperate class that I call Database.
In the above example I select the "open" menu on the form and I
instantiate the db (Database) class. After that I select the "close"
on the form to save the file but here I get the error that "Object
reference not set to an instance of an object.".
I thought I did that in the openToolStripMenuItem_Click method. I was
however forced to set an object reference earlier in the code
though...
What am I missing here?
How can I access the methods of a class in a method that were
instatiated in another method?
Please advice.
/TJ
I have a slight problem with how things work that I'm sure I'm
missing. I don't really get it how I am supposed to get references
between classes and instances of them. Example:
using System;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Object references
public Database db;
// Methods
public void openToolStripMenuItem_Click(object sender,
EventArgs e)
{
Database db = new Database();
}
private void closeToolStripMenuItem_Click(object sender,
EventArgs e)
{
db.SaveAs();
}
}
}
I have a seperate class that I call Database.
In the above example I select the "open" menu on the form and I
instantiate the db (Database) class. After that I select the "close"
on the form to save the file but here I get the error that "Object
reference not set to an instance of an object.".
I thought I did that in the openToolStripMenuItem_Click method. I was
however forced to set an object reference earlier in the code
though...
What am I missing here?
How can I access the methods of a class in a method that were
instatiated in another method?
Please advice.
/TJ