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



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.


"Andy B" <a_borka@xxxxxxxxxxxxx> wrote in message
news:eYoANHMpIHA.552@xxxxxxxxxxxxxxxxxxxxxxx
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?
"George Ter-Saakov" <gt-nsp@xxxxxxxxxxx> wrote in message
news:e3vIjJJpIHA.3408@xxxxxxxxxxxxxxxxxxxxxxx
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.



"Andy B" <a_borka@xxxxxxxxxxxxx> wrote in message
news:OmMZlAJpIHA.3428@xxxxxxxxxxxxxxxxxxxxxxx
At that rate, wouldn't it be even better to move the Contract
StockContract = new Contract() line to the constructer?
"George Ter-Saakov" <gt-nsp@xxxxxxxxxxx> wrote in message
news:e5jWxnIpIHA.1164@xxxxxxxxxxxxxxxxxxxxxxx
You have a problem in your code
Contract StockContract = new Contract(); -- correct
---incorrect-----
StockContract.Dictionary = new ContractDictionary(); ---incorrect
StockContract.Sections = new ContractSections<string,
g>(); ---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.


"Andy B" <a_borka@xxxxxxxxxxxxx> wrote in message
news:uwn1YbIpIHA.4884@xxxxxxxxxxxxxxxxxxxxxxx
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);

}

}

}

"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx> wrote
in message news:OM7eZ2HpIHA.5016@xxxxxxxxxxxxxxxxxxxxxxx
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.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box! |
*************************************************
"Andy B" <a_borka@xxxxxxxxxxxxx> wrote in message
news:OadrQwHpIHA.5016@xxxxxxxxxxxxxxxxxxxxxxx
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?

















.


Loading