Class property misunderstanding

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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...
    > 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 Guest

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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"...
    5. 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...
  3. #2

    Default 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...
    > 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, 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...
    > > > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139