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.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Xml.Linq ;
using ContractService Provider.Contra ctModel;
namespace Main.Admin.Cont racts.Stock {
public partial class Add : System.Web.UI.P age {
protected void Page_Load(objec t sender, EventArgs e) {
//the following objects aren't accessible anywhere else except in here...
Contract Contract = new Contract();
Contract.Dictio nary = new ContractDiction ary<string, string>();
Contract.Sectio ns = new ContractSection s<string, string>();
}
protected void AddStockContrac tWizard_ActiveS tepChanged1(obj ect sender,
EventArgs e) {
AddStockContrac tWizard.HeaderT ext = AddStockContrac tWizard.ActiveS tep.Title;
}
protected void AddDefinitionBu tton_Click(obje ct 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.Dictio nary.Add(WordTe xtBox.Text, DefinitionTextB ox.Text);
ListItem ListItem = new ListItem();
ListItem.Text=W ordTextBox.Text +": "+DefinitionTex tBox.Text;;
ListItem.Value= WordTextBox.Tex t;
DefinitionList. Items.Add(ListI tem);
}
}
}
The line where it says: Contract.Dictio nary<string, string= new
ContractDiction ary<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...
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Mon, 21 Apr 2008 12:25:18 -0700, Andy B <a_*****@sbcglo bal.netwrote:
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.Dicti onary<string, string= new Dictionary<stri ng, 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.Dict ionary<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