Re: Class property misunderstanding

Discussion in 'ASP .Net' started by Kevin Spencer, Jun 26, 2003.

  1. 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" <> wrote in message
    news:u8k%23$...
    > 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
    >
    >
    Kevin Spencer, Jun 26, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:O4ZJWl$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:u8k%23$...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
    Kevin Spencer, Jun 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Hobley

    vec - a misunderstanding

    Mark Hobley, Mar 18, 2006, in forum: Perl
    Replies:
    1
    Views:
    2,609
    John Bokma
    Mar 18, 2006
  2. Philip Tripp

    View State Misunderstanding

    Philip Tripp, Jul 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    528
    Philip Tripp
    Jul 26, 2004
  3. Shawn Wildermuth

    Misunderstanding ConnectionStrings?

    Shawn Wildermuth, Jan 9, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    1,915
    Steven Cheng[MSFT]
    Jan 11, 2006
  4. Replies:
    3
    Views:
    573
    Flinky Wisty Pomm
    Apr 3, 2006
  5. Bill Mill
    Replies:
    15
    Views:
    493
    Alex Martelli
    Jan 31, 2005
Loading...

Share This Page