How to make an object instance available to all members of a page?

A

Andy B

I have an instance of an object that needs to be accessed by all members of
a page like page_load, button_click events and so on. Where in the
codebehind would I put the creation of the object instance?
 
G

George Ter-Saakov

VB or C#?

in C# it's called member variable.

class MyClass
{
int _iCounter = 0;
...page_Load(....)
{
_iCounter = 0;
}
.... page_Render(...)
{
_iCounter = 5;
}
}

George.
 
C

Cowboy \(Gregory A. Beamer\)

You can create the object in the constructor, if you have enough
information. If this object is dynamic, you will have to go back to
something like PreInit.
 
A

Andy B

It is dynamic in the sense that there is a wizard control feeding it all of
its values, otherise it is always there. This is done in c#. I tried all
sorts of things like putting it just in the page class, but the compiler
tells me that it doesn't like that idea. I keep getting some invalid token
problems or something. Here is the errors I get along with the page
codebehind.

Error 1 Invalid token '=' in class, struct, or interface member declaration
C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Main\Admin\Contracts\Stock\Add.aspx.cs
18 26 Main
Error 2 Method must have a return type C:\Documents and Settings\Andy\My
Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Main\Admin\Contracts\Stock\Add.aspx.cs
18 32 Main
Error 3 Invalid token '=' in class, struct, or interface member declaration
C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Main\Admin\Contracts\Stock\Add.aspx.cs
19 27 Main
Error 4 Invalid token '(' in class, struct, or interface member declaration
C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Main\Admin\Contracts\Stock\Add.aspx.cs
19 65 Main

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 {

Contract StockContract = new Contract();

StockContract.Dictionary = new ContractDictionary();

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


protected void Page_Load(object sender, EventArgs e) {

}

protected void AddStockContractWizard_ActiveStepChanged1(object sender,
EventArgs e) {

AddStockContractWizard.HeaderText = AddStockContractWizard.ActiveStep.Title;

}

protected void AddDefinitionButton_Click(object sender, EventArgs e) {

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

}

}

}
 
G

George Ter-Saakov

You have a problem in your code
Contract StockContract = new Contract(); -- correct
---incorrect-----
StockContract.Dictionary = new ContractDictionary(); ---incorrect
StockContract.Sections = new ContractSections<string,
tring>(); ---incorrect
---You can only declare variables here and set their default values.
StockContract.Dictionary = new ContractDictionary();
is actually a code that needs to be moved to "code execution" portion of
your program. I.E method or constructor.


You need to move incorrect lines to constructor.
So it will be
public partial class Add : System.Web.UI.Page {
Contract StockContract = new Contract();

public Add ()
{
StockContract.Dictionary = new ContractDictionary();
StockContract.Sections = new ContractSections<string, string>();
}
.....
}
George.
 
A

Andy B

At that rate, wouldn't it be even better to move the Contract StockContract
= new Contract() line to the constructer?
 
G

George Ter-Saakov

Yes, and no....
Contract StockContract = new Contract()
This line defines a default value.

If you move it to constructor then default value will become null... So you
will lose some runtime because first compiler will assign null to
StockContract and only then execute constructor which will execute "new" and
override null.

Obviously assigning null is very fast and it becomes more theoretical than
practical concern :)

For example I always prefer to define my default values. So I always know
what to expect.

PS. You might have more than one constructor and then you forced to have
that line in every constructor. As opposed to just define it as a default
value once.


George.
 
A

Andy B

This makes sense as far as having:
Contract StockContract = new Contract();

Being a default value. But if the above line is an assignment of a default
value, then why is the 2 lines below illegal outside of a constructer or
method? Don't they assign default values as well?

StockContract.Dictionary = new ContractDictionary<string, string>();
StockContract.Sections = new ContractSections<string, string();

I don't see much difference in the 3 lines unless the difference comes into
play because of the Dictionary/Sections properties. Is this the case? And if
Dictionary/Sections properties are assigned their values in a Button_Click
event, will those assigned properties be available inside of another method?
I.e. another button_Click event? In my original posted example, the
Dictionary property was assigned its values in a AddDictionaryButton_Click
event. Will they be available to the WizardFinishButton_Click event where
the whole StockContract object gets serialized into an xml file? or is there
other things that need to be done to make them available elsewhere?
 
G

George Ter-Saakov

StockContract.Dictionary = new ContractDictionary<string, string>();
That is not assigning default value
the StockContract was already allocated and already has Dictionary set to
some default value.

It just does not make sense to allow code like that. Executable code in C#
must be in method and must be called in order to execute.
Assigning default value is a logical exception from that rule.

If you think about it all makes sense:
When you write "StockContract obj" compiler must allocate space for "obj".
And it needs to initialize it to point to null...
But why null? So C# developers allowed you to specify the default value that
will be assigned to the obj.


George.
 
A

Andy B

Makes sense now...


George Ter-Saakov said:
StockContract.Dictionary = new ContractDictionary<string, string>();
That is not assigning default value
the StockContract was already allocated and already has Dictionary set to
some default value.

It just does not make sense to allow code like that. Executable code in C#
must be in method and must be called in order to execute.
Assigning default value is a logical exception from that rule.

If you think about it all makes sense:
When you write "StockContract obj" compiler must allocate space for "obj".
And it needs to initialize it to point to null...
But why null? So C# developers allowed you to specify the default value
that will be assigned to the obj.


George.
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top