Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Class property misunderstanding

Reply
Thread Tools

Re: Class property misunderstanding

 
 
Kevin Spencer
Guest
Posts: n/a
 
      06-26-2003
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
http://www.takempis.com
Big things are made up of
lots of Little things.


"Jim Owen" <(E-Mail Removed)> wrote in message
news:u8k%23$(E-Mail Removed)...
> In the following snippet of code, I have a class called Thing. When a

Thing
> is constructed, it populates a date called MyDate, then runs another

method
> 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 just

set
> it in the constructor. Why is this? How should this code be changed to

work?
>
> 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.

Why?
> }
> }
>
> - Jim Owen
> 206-501-6936
>
>



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      06-26-2003
Don't feel bad, Jim. Sometimes the obvious ones are the hardest to notice!

HTH,

Kevin Spencer
Microsoft FrontPage MVP
Internet Developer
http://www.takempis.com
Big things are made up of
lots of Little things.

"Jim Owen" <(E-Mail Removed)> wrote in message
news:O4ZJWl$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> > http://www.takempis.com
> > Big things are made up of
> > lots of Little things.
> >
> >
> > "Jim Owen" <(E-Mail Removed)> wrote in message
> > news:u8k%23$(E-Mail Removed)...
> > > In the following snippet of code, I have a class called Thing. When a

> > Thing
> > > is constructed, it populates a date called MyDate, then runs another

> > method
> > > 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

just
> > set
> > > it in the constructor. Why is this? How should this code be changed to

> > work?
> > >
> > > 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.
> > Why?
> > > }
> > > }
> > >
> > > - Jim Owen
> > > 206-501-6936
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OO Javascript - Class.Prototype.Property vs. This.Property Spam Catcher Javascript 4 11-21-2007 05:23 PM
vec - a misunderstanding Mark Hobley Perl 1 03-18-2006 07:32 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Dynamic class methods misunderstanding Bill Mill Python 15 01-31-2005 11:56 AM
errors in book or my misunderstanding? David K MCSE 4 12-12-2003 03:22 PM



Advertisments