473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Scope in Winforms problem


I want to access an object that is instantiated in the frmMain method from within a static method. When I try to access this object, I get errors that state "Object reference not set to an instance of an object."

I know this is a scope problem, but I don't know how to solve it. My application is a winforms app and I'm instatiating the object from frmMain(). The object is declared:

public static DataAccess myDataAccessor;

What do I need to do? Why isn't this public object accessible throughtout my application? Is there a better place than frmMain where objects that should be accesssible throughout an application reside? I'd like to avoid passing objects that already exist as arguments if I'm creating objects that are meant to be accessible througout my app.

Thanks!
Nov 16 '05 #1
5 1377
Patrick <Pa*****@discussions.microsoft.com> wrote:
I want to access an object that is instantiated in the frmMain method
from within a static method. When I try to access this object, I get
errors that state "Object reference not set to an instance of an
object."

I know this is a scope problem, but I don't know how to solve it. My
application is a winforms app and I'm instatiating the object from
frmMain(). The object is declared:

public static DataAccess myDataAccessor;

What do I need to do? Why isn't this public object accessible
throughtout my application? Is there a better place than frmMain
where objects that should be accesssible throughout an application
reside? I'd like to avoid passing objects that already exist as
arguments if I'm creating objects that are meant to be accessible
througout my app.


That doesn't sound like a scope issue at all. It sounds like you've not
actually instantiated it properly. My guess is that either the static
method isn't being called, or within the static method you're declaring
a local variable with the same name instead of using the static
variable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
My code looks like this:
public class FrmMain : System.Windows.Forms.Form
{
Nov 16 '05 #3
Patrick <Pa*****@discussions.microsoft.com> wrote:
My code looks like this:

public class FrmMain : System.Windows.Forms.Form
{
.
.
public DataAccess myDataAccessor;

.
.
public FrmMain()
{
VerifyDelete = true;
DataAccess myDataAccessor = new DataAccess();
Event myEvent = new Event();
InitializeComponent();
InitializeForm();
.
.
.
}

And InitializeForm has the following signature:

public void InitializeForm()

When I'm in frmMain, after the myDataAccessor and myEvent objects are
instatiated, the objects are accessible. As soon as I enter
InitializeForm, the objects report <undefined value> in the watch
window.

Does this give further clarification? I don't see anywhere I'm
misusing the functions.


Again, it's not an accessibility problem. The problem (as I suggested
in my last post) is that you've redeclared your myDataAccessor
variable.

Instead of

DataAccess myDataAccessor = new DataAccess();

(which is declaring a new local variable and assigning it a value) you
should have

myDataAccessor = new DataAccess();

(which sets the value of the *instance* variable myDataAccessor).

Same for myEvent.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Now I understand. I didn't notice the subtle syntactial difference that was making it a local variable. Thanks Jon. I'm rocking again.
"Jon Skeet [C# MVP]" wrote:
Patrick <Pa*****@discussions.microsoft.com> wrote:
My code looks like this:

public class FrmMain : System.Windows.Forms.Form
{
.
.
public DataAccess myDataAccessor;

.
.
public FrmMain()
{
VerifyDelete = true;
DataAccess myDataAccessor = new DataAccess();
Event myEvent = new Event();
InitializeComponent();
InitializeForm();
.
.
.
}

And InitializeForm has the following signature:

public void InitializeForm()

When I'm in frmMain, after the myDataAccessor and myEvent objects are
instatiated, the objects are accessible. As soon as I enter
InitializeForm, the objects report <undefined value> in the watch
window.

Does this give further clarification? I don't see anywhere I'm
misusing the functions.


Again, it's not an accessibility problem. The problem (as I suggested
in my last post) is that you've redeclared your myDataAccessor
variable.

Instead of

DataAccess myDataAccessor = new DataAccess();

(which is declaring a new local variable and assigning it a value) you
should have

myDataAccessor = new DataAccess();

(which sets the value of the *instance* variable myDataAccessor).

Same for myEvent.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
SP
My suggestion would be to implement the DataAccess as a singleton. It would
then be globally available and would instansiate itself.
SP

"Patrick" <Pa*****@discussions.microsoft.com> wrote in message
news:21**********************************@microsof t.com...
Now I understand. I didn't notice the subtle syntactial difference that was making it a local variable. Thanks Jon. I'm rocking again.

"Jon Skeet [C# MVP]" wrote:
Patrick <Pa*****@discussions.microsoft.com> wrote:
My code looks like this:

public class FrmMain : System.Windows.Forms.Form
{
.
.
public DataAccess myDataAccessor;

.
.
public FrmMain()
{
VerifyDelete = true;
DataAccess myDataAccessor = new DataAccess();
Event myEvent = new Event();
InitializeComponent();
InitializeForm();
.
.
.
}

And InitializeForm has the following signature:

public void InitializeForm()

When I'm in frmMain, after the myDataAccessor and myEvent objects are
instatiated, the objects are accessible. As soon as I enter
InitializeForm, the objects report <undefined value> in the watch
window.

Does this give further clarification? I don't see anywhere I'm
misusing the functions.


Again, it's not an accessibility problem. The problem (as I suggested
in my last post) is that you've redeclared your myDataAccessor
variable.

Instead of

DataAccess myDataAccessor = new DataAccess();

(which is declaring a new local variable and assigning it a value) you
should have

myDataAccessor = new DataAccess();

(which sets the value of the *instance* variable myDataAccessor).

Same for myEvent.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
5
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
1
by: Pieter | last post by:
Hi, In my application VB.NET 2005 application I placed a ReportViewer, and link it to a server-report: Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote...
4
by: 3Cooks | last post by:
I have a windows application written in Visual Basic 6.0 that is going to be redeveloped in dotNET. We are trying to decide if we should deploy using Webforms or Winforms and I need advice from...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: parez | last post by:
Hi, Whats the winforms equivalent of asp.net page load event? I am trying to clear a status message field every time a user clicks on any of the buttons..
23
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.