How to make an object instance available to all members of a page?
How to make an object instance available to all members of a page?
am 22.04.2008 15:23:39 von 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?
Re: How to make an object instance available to all members of a page?
am 22.04.2008 15:32:03 von 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.
"Andy B" wrote in message
news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>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?
>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 15:34:29 von NoSpamMgbworld
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" wrote in message
news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>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?
>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 16:40:50 von 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\St ock\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\St ock\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\St ock\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\St ock\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();
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)" wrote in
message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
> 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" wrote in message
> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>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?
>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 17:02:52 von 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
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();
}
.....
}
George.
"Andy B" wrote in message
news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>
>
> 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)" wrote in
> message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>> 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" wrote in message
>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>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?
>>>
>>>
>>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 17:47:24 von Andy B
At that rate, wouldn't it be even better to move the Contract StockContract
= new Contract() line to the constructer?
"George Ter-Saakov" wrote in message
news:e5jWxnIpIHA.1164@TK2MSFTNGP04.phx.gbl...
> You have a problem in your code
> Contract StockContract = new Contract(); -- correct
> ---incorrect-----
> StockContract.Dictionary = new ContractDictionary(); ---incorrect
> StockContract.Sections = new ContractSections
> ring>(); ---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();
> }
> ....
> }
> George.
>
>
> "Andy B" wrote in message
> news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
>> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>>
>>
>> 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)" wrote in
>> message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>> 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" wrote in message
>>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>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?
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 18:03:19 von 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.
"Andy B" wrote in message
news:OmMZlAJpIHA.3428@TK2MSFTNGP02.phx.gbl...
> At that rate, wouldn't it be even better to move the Contract
> StockContract = new Contract() line to the constructer?
> "George Ter-Saakov" wrote in message
> news:e5jWxnIpIHA.1164@TK2MSFTNGP04.phx.gbl...
>> You have a problem in your code
>> Contract StockContract = new Contract(); -- correct
>> ---incorrect-----
>> StockContract.Dictionary = new ContractDictionary(); ---incorrect
>> StockContract.Sections = new ContractSections
>> ing>(); ---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();
>> }
>> ....
>> }
>> George.
>>
>>
>> "Andy B" wrote in message
>> news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
>>> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>>>
>>>
>>> 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)" wrote
>>> in message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>> 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" wrote in message
>>>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>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?
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 23:42:51 von 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();
StockContract.Sections = new ContractSections
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" wrote in message
news:e3vIjJJpIHA.3408@TK2MSFTNGP03.phx.gbl...
> 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" wrote in message
> news:OmMZlAJpIHA.3428@TK2MSFTNGP02.phx.gbl...
>> At that rate, wouldn't it be even better to move the Contract
>> StockContract = new Contract() line to the constructer?
>> "George Ter-Saakov" wrote in message
>> news:e5jWxnIpIHA.1164@TK2MSFTNGP04.phx.gbl...
>>> You have a problem in your code
>>> Contract StockContract = new Contract(); -- correct
>>> ---incorrect-----
>>> StockContract.Dictionary = new ContractDictionary(); ---incorrect
>>> StockContract.Sections = new ContractSections
>>> ng>(); ---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();
>>> }
>>> ....
>>> }
>>> George.
>>>
>>>
>>> "Andy B" wrote in message
>>> news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
>>>> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>>>>
>>>>
>>>> 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)" wrote
>>>> in message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>> 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" wrote in message
>>>>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>>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?
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 22.04.2008 23:58:47 von George Ter-Saakov
StockContract.Dictionary = new ContractDictionary();
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" wrote in message
news:eYoANHMpIHA.552@TK2MSFTNGP06.phx.gbl...
> 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();
> StockContract.Sections = new ContractSections
>
> 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" wrote in message
> news:e3vIjJJpIHA.3408@TK2MSFTNGP03.phx.gbl...
>> 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" wrote in message
>> news:OmMZlAJpIHA.3428@TK2MSFTNGP02.phx.gbl...
>>> At that rate, wouldn't it be even better to move the Contract
>>> StockContract = new Contract() line to the constructer?
>>> "George Ter-Saakov" wrote in message
>>> news:e5jWxnIpIHA.1164@TK2MSFTNGP04.phx.gbl...
>>>> You have a problem in your code
>>>> Contract StockContract = new Contract(); -- correct
>>>> ---incorrect-----
>>>> StockContract.Dictionary = new ContractDictionary(); ---incorrect
>>>> StockContract.Sections = new ContractSections
>>>> 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();
>>>> }
>>>> ....
>>>> }
>>>> George.
>>>>
>>>>
>>>> "Andy B" wrote in message
>>>> news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
>>>>> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>>>>>
>>>>>
>>>>> 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)" wrote
>>>>> in message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>> 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" wrote in message
>>>>>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>>>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?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Re: How to make an object instance available to all members of a page?
am 23.04.2008 00:46:26 von Andy B
Makes sense now...
"George Ter-Saakov" wrote in message
news:utYrLQMpIHA.2636@TK2MSFTNGP04.phx.gbl...
> StockContract.Dictionary = new ContractDictionary();
> 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" wrote in message
> news:eYoANHMpIHA.552@TK2MSFTNGP06.phx.gbl...
>> 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();
>> StockContract.Sections = new ContractSections
>>
>> 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" wrote in message
>> news:e3vIjJJpIHA.3408@TK2MSFTNGP03.phx.gbl...
>>> 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" wrote in message
>>> news:OmMZlAJpIHA.3428@TK2MSFTNGP02.phx.gbl...
>>>> At that rate, wouldn't it be even better to move the Contract
>>>> StockContract = new Contract() line to the constructer?
>>>> "George Ter-Saakov" wrote in message
>>>> news:e5jWxnIpIHA.1164@TK2MSFTNGP04.phx.gbl...
>>>>> You have a problem in your code
>>>>> Contract StockContract = new Contract(); -- correct
>>>>> ---incorrect-----
>>>>> StockContract.Dictionary = new ContractDictionary(); ---incorrect
>>>>> StockContract.Sections = new ContractSections
>>>>> >(); ---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>();
>>>>> }
>>>>> ....
>>>>> }
>>>>> George.
>>>>>
>>>>>
>>>>> "Andy B" wrote in message
>>>>> news:uwn1YbIpIHA.4884@TK2MSFTNGP06.phx.gbl...
>>>>>> 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\St ock\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\St ock\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\St ock\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\St ock\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();
>>>>>>
>>>>>>
>>>>>> 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)"
>>>>>> wrote in message news:OM7eZ2HpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>>> 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" wrote in message
>>>>>>> news:OadrQwHpIHA.5016@TK2MSFTNGP02.phx.gbl...
>>>>>>>>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?
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>