Making an instance of an object available for all events and methods of a class

A

Andy B

I am trying to figure out how to make an object instance available for all
methods of a class. I tried to do something like this:

public class test {
TheObject Instance = new TheObject();
TheObject.Dictionary<string, string> = new Dictionary<string, string>();
....
}

The first line (TheObject instance = new TheObject();) doesn't get
complained about by the compiler. The next line after it for some reason
doesn't work. I cant get intelisense out of vs2008 for the object if it
isn't inside a method for some reason. Any idea why this is?
 
J

Jon Skeet [C# MVP]

Andy B said:
I am trying to figure out how to make an object instance available for all
methods of a class. I tried to do something like this:

public class test {
TheObject Instance = new TheObject();
TheObject.Dictionary<string, string> = new Dictionary<string, string>();
...
}

The first line (TheObject instance = new TheObject();) doesn't get
complained about by the compiler. The next line after it for some reason
doesn't work. I cant get intelisense out of vs2008 for the object if it
isn't inside a method for some reason. Any idea why this is?

What is the second line meant to be doing? It looks sort of like a
declaration (in that TheObject.Dictionary<string,string> could be a
nested type) but then there's no variable declaration.

Did you mean

Instance.Dictionary = new Dictionary<string,string>?

If so, that's a statement which should be in a method - such as a
constructor for your test class.
 
A

Andy B

Well, I would imagine that the subject of the post would have everything to
do with the question I posted. The original question was: How do you get an
instance of an object to be useable/available for all methods/events of a
class? I tried putting what I needed to use of the object inside the class
directly, but vs was somehow complaining about it and not saying why... I
can post a full example here, but for "what it's worth..." I posted a full
code sample up here once before and basically got told not to post so much
code all at once because "nobody would read it anyways". Either way, here is
the problem. I need to make this object available to all events/methods of
the code behind of an asp.net page. Since code behinde pages (as far as I
know of, can't have constructers - only a page_load event. I tried putting
the object creation in there, but a particular method couldn't "access" the
code it needed to. Here is the code with comments as it is now.

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Xml.Linq;

using ContractServiceProvider.ContractModel;

namespace Main.Admin.Contracts.Stock {

public partial class Add : System.Web.UI.Page {



protected void Page_Load(object sender, EventArgs e) {

//the following objects aren't accessible anywhere else except in here...

Contract Contract = new Contract();

Contract.Dictionary = new ContractDictionary<string, string>();

Contract.Sections = new ContractSections<string, string>();

}

protected void AddStockContractWizard_ActiveStepChanged1(object sender,
EventArgs e) {

AddStockContractWizard.HeaderText = AddStockContractWizard.ActiveStep.Title;

}

protected void AddDefinitionButton_Click(object sender, EventArgs e) {

//the following code is broken because vs complains. it says that a
reference to "contract" is needed. How do you do that?

Contract.Dictionary.Add(WordTextBox.Text, DefinitionTextBox.Text);

ListItem ListItem = new ListItem();

ListItem.Text=WordTextBox.Text+": "+DefinitionTextBox.Text;;

ListItem.Value=WordTextBox.Text;

DefinitionList.Items.Add(ListItem);

}

}

}


The line where it says: Contract.Dictionary<string, string> = new
ContractDictionary<string, string>();

Is a nested type inside another type contained inside a public property. It
does actually work because I ran it through a test project before using it
inside the real code. Any ideas how to fix this "references to an object
needed" problem?

Sorry for the sounding of being cranky but it is a long day today...


I am trying to figure out how to make an object instance available for
all
methods of a class. I tried to do something like this:

public class test {
TheObject Instance = new TheObject();
TheObject.Dictionary<string, string> = new Dictionary<string, string>();
...
}

That code sample doesn't show a single method.
The first line (TheObject instance = new TheObject();) doesn't get
complained about by the compiler.

Well, it is a legal line after all. :)
The next line after it for some reason doesn't work.

I wouldn't expect it to.

"TheObject" is a class name. Even if one assumes that
"TheObject.Dictionary<string, string>" could be a valid l-value expression
(and I don't think it can be...you can't have a generic property or
field), you can't put assignment statements like that in a class
declaration. You can only declare things in a class declaration. If you
want an executable statement, it needs to be inside an executable element,
such as a constructor, method, property setter or getter, etc.

For what it's worth, the subject of your thread here doesn't make much
sense either. An "event" isn't something that would have access to
anything. Nor does the subject seem to have anything to do with the code
you posted, due to the lack of an actual example of a method that might
use this hypothetical instance.

Based solely on your subject, it seems _possible_ that you are trying to
add a static field to a class, so that the field can be accessed by
members of the class. But that's pure speculation. There's really not
enough in your post for me to know for sure that's what you're trying to
do.

In other words, you should really try to figure out how to state your
question in a more clear, less ambiguous way.

Pete
 
H

Hans Kesting

Andy B pretended :
Here is the code with comments as it is now.
[snip]

namespace Main.Admin.Contracts.Stock {
public partial class Add : System.Web.UI.Page {

protected void Page_Load(object sender, EventArgs e) {
//the following objects aren't accessible anywhere else except in here...
Contract Contract = new Contract();

Contract.Dictionary = new ContractDictionary<string, string>();

Contract.Sections = new ContractSections<string, string>();

}

Two possible problems here:
1) as Peter pointed out, the variable you declare here is local to this
method. You will need to declare it at class-level to make it visible
"elsewhere".
2) your variable has the same name as it's type. So it's not clear to
me (and how would it then be clear to the compiler) whether
"Contract.Dictionary" is some static property in the Contract *Type* or
an instance property of your local Contract *variable*. If you rename
that variable to "contract" (starting with a lowercase 'c'), that
problem disappears.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top