Ask a Question related to ASP.NET General, Design and Development.
-
Kevin Spencer #1
Re: Class property misunderstanding
Hi Jim,
What you're dealing with here is scope. Variables have scope, which means
that they are accessible at different levels depending upon how and when
they are declared. Your class has a public property called "MyDate."
However, your initializer declares another variable called "MyDate" (The
syntax "datatype variableName" declares a variable. The public "MyDate" has
global scope. The "MyDate" in your function has function scope. The rule is
that when 2 variables have the same name, the one that is declared within a
function is used within that function, or you might say "overrules" the
global one within that function. Therefore, by declaring the variable, you
created a different variable and populated it instead of the global
property. you could fix it like this:
public Thing()
{
MyDate = DateTime.Today;
AnotherMethod();
}
However, it would be more efficient to do this:
public Thing()
{
FMyDate = DateTime.Today;
AnotherMethod();
}
The second version negates the necessity of invoking the "set" method of the
public property, sving some processing, as the "set" method of the property
changes the value of the protected field.
HTH,
Kevin Spencer
Microsoft FrontPage MVP
Internet Developer
[url]http://www.takempis.com[/url]
Big things are made up of
lots of Little things.
"Jim Owen" <jkoseattle@comcast.net> wrote in message
news:u8k%23$f3ODHA.3016@TK2MSFTNGP10.phx.gbl...Thing> In the following snippet of code, I have a class called Thing. When amethod> is constructed, it populates a date called MyDate, then runs anotherset> in Thing called AnotherMethod(). If I get to the line at the bottom that
> reads "int x = 0:", if I evaluate MyDate, it is null, even though I justwork?> it in the constructor. Why is this? How should this code be changed toWhy?>
> public class Thing
> {
> protected System.DateTime FMyDate;
> public System.DateTime MyDate
> {
> get { return FMyDate; }
> set { FMyDate = value; }
> }
>
> public Thing()
> {
> System.DateTime MyDate = DateTime.Today;
> AnotherMethod();
> }
>
> public void AnotherMethod()
> {
> MyDate.AddDays(365); // Right here, MyDate evaluates to null.> }
> }
>
> - Jim Owen
> 206-501-6936
>
>
Kevin Spencer Guest
-
Unable to set the XValues property of the Series class
Hi All, I have tried umpteen ways of setting the XValues in an Excel Spreadsheet from Perl 5.8.7 using Win32::OLE, and I cannot get anything to... -
how to add another property to class inheriting from IIdentity?
Hello peoples this is a question regarding security of windows application I want to add another property (the UserID) to... -
Help Anyone?How do I access Inner Class Property
hi guys, I am having this problems too... if I inherits it from System.Web.UI.WebControl.WebControls.. it works fine.. but if I inherits it from... -
Binding a textbox to a class property
This might sound like a elementary question but I've haven't been able to figure this one out. How can I bind the "text" property of a "textBox"... -
undefined class property
I've got this $user class that has a variable called $userIsSuperRoot. When I just set error_reporting to ALL I got an error on the last line that... -
Kevin Spencer #2
Re: Class property misunderstanding
Don't feel bad, Jim. Sometimes the obvious ones are the hardest to notice!
HTH,
Kevin Spencer
Microsoft FrontPage MVP
Internet Developer
[url]http://www.takempis.com[/url]
Big things are made up of
lots of Little things.
"Jim Owen" <jkoseattle@comcast.net> wrote in message
news:O4ZJWl$ODHA.3664@tk2msftngp13.phx.gbl...means> Oh my God, I must be tired! That's such an obvious error. I've known about
> variable scope since the 80's! And I stared at this code forever. LOL!
>
> --
> - Jim Owen
> 206-501-6936
> "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> wrote in message
> news:eoOVF59ODHA.1072@TK2MSFTNGP10.phx.gbl...> > Hi Jim,
> >
> > What you're dealing with here is scope. Variables have scope, whichwithin> has> > that they are accessible at different levels depending upon how and when
> > they are declared. Your class has a public property called "MyDate."
> > However, your initializer declares another variable called "MyDate" (The
> > syntax "datatype variableName" declares a variable. The public "MyDate"> is> > global scope. The "MyDate" in your function has function scope. The rule> > that when 2 variables have the same name, the one that is declaredyou> a> > function is used within that function, or you might say "overrules" the
> > global one within that function. Therefore, by declaring the variable,that> the> > created a different variable and populated it instead of the global
> > property. you could fix it like this:
> >
> > public Thing()
> > {
> > MyDate = DateTime.Today;
> > AnotherMethod();
> > }
> >
> > However, it would be more efficient to do this:
> >
> > public Thing()
> > {
> > FMyDate = DateTime.Today;
> > AnotherMethod();
> > }
> >
> > The second version negates the necessity of invoking the "set" method of> property> > public property, sving some processing, as the "set" method of the> > changes the value of the protected field.
> >
> > HTH,
> >
> > Kevin Spencer
> > Microsoft FrontPage MVP
> > Internet Developer
> > [url]http://www.takempis.com[/url]
> > Big things are made up of
> > lots of Little things.
> >
> >
> > "Jim Owen" <jkoseattle@comcast.net> wrote in message
> > news:u8k%23$f3ODHA.3016@TK2MSFTNGP10.phx.gbl...> > Thing> > > In the following snippet of code, I have a class called Thing. When a> > method> > > is constructed, it populates a date called MyDate, then runs another> > > in Thing called AnotherMethod(). If I get to the line at the bottomjust> > > reads "int x = 0:", if I evaluate MyDate, it is null, even though Inull.> > set> > work?> > > it in the constructor. Why is this? How should this code be changed to> > >
> > > public class Thing
> > > {
> > > protected System.DateTime FMyDate;
> > > public System.DateTime MyDate
> > > {
> > > get { return FMyDate; }
> > > set { FMyDate = value; }
> > > }
> > >
> > > public Thing()
> > > {
> > > System.DateTime MyDate = DateTime.Today;
> > > AnotherMethod();
> > > }
> > >
> > > public void AnotherMethod()
> > > {
> > > MyDate.AddDays(365); // Right here, MyDate evaluates to>> > Why?> >> > > }
> > > }
> > >
> > > - Jim Owen
> > > 206-501-6936
> > >
> > >
> >
>
Kevin Spencer Guest



Reply With Quote

