Static properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    #1

    Static properties

    I'm having a bit of trouble understanding Static properties.

    I have two forms and I need to be able to access a property from from1 in
    form2.

    The code below does not work but I'm not sure why not. And also why is it
    necessary to declare the variable bIshShown twice?


    Form1 code:

    private static bool bIsShown = false;

    public bool bIsShown

    {

    get{ return bIsShown; }

    set { bIsShown = value; }

    }





    Form2 code:

    form1.bIsShown = false;



    Thanks,

    Ron


  • Dries

    #2
    Re: Static properties

    RSH,

    Form1 code:[color=blue]
    >
    > private static bool bIsShown = false;
    >
    > public bool bIsShown
    >
    > {
    >
    > get{ return bIsShown; }[/color]
    must be: get { return Form1.bIsShown; }[color=blue]
    >
    > set { bIsShown = value; }[/color]
    must be: set { Form1.bIsShown = value; }[color=blue]
    >
    > }[/color]

    the reason this has to be written like this is that you want to access a
    static variable from within an instance. The static variable can be seen
    as a variable that is not in a class, so when you are in an instance you
    must explicitly write that you are not looking in this instance for the
    variable.

    Post some more information on the errors you get, I suppose you get an
    error that the name bIsShown cannot be used twice...
    [color=blue]
    > Form2 code:
    >
    > form1.bIsShown = false;[/color]

    Comment

    • Peter Rilling

      #3
      Re: Static properties

      Fields store data, properties do not. You are not declaring a variable
      twice, you are defining two different types of objects with the same name.
      (Also, you might choose to name the private variable something other than
      the property for readability. I tend to put m_ at the from of all my
      private variables, others have different conventions.)

      Only static properties can assess static fields. Make the property static
      as well.

      "RSH" <way_beyond_oop s@yahoo.com> wrote in message
      news:u88EolJEGH A.1120@TK2MSFTN GP11.phx.gbl...[color=blue]
      > I'm having a bit of trouble understanding Static properties.
      >
      > I have two forms and I need to be able to access a property from from1 in
      > form2.
      >
      > The code below does not work but I'm not sure why not. And also why is it
      > necessary to declare the variable bIshShown twice?
      >
      >
      > Form1 code:
      >
      > private static bool bIsShown = false;
      >
      > public bool bIsShown
      >
      > {
      >
      > get{ return bIsShown; }
      >
      > set { bIsShown = value; }
      >
      > }
      >
      >
      >
      >
      >
      > Form2 code:
      >
      > form1.bIsShown = false;
      >
      >
      >
      > Thanks,
      >
      > Ron
      >
      >[/color]


      Comment

      • Joanna Carter [TeamB]

        #4
        Re: Static properties

        "RSH" <way_beyond_oop s@yahoo.com> a écrit dans le message de news:
        u88EolJEGHA.112 0@TK2MSFTNGP11. phx.gbl...

        | I have two forms and I need to be able to access a property from from1 in
        | form2.

        You need to remember that the code that you write in a module containing a
        form class is*not* a form, it represents what goes on in an instance of the
        class that you are writing.

        Static fields/properties are a way of accessing state that is common to the
        *type* of the form class, not an *instance* of that class.

        You can create more than one instance of any class therefore, whether you
        use a static field/property or an instance field/property depends on whether
        you want to restrict the value of that member to represent something
        relevent to the class rather than an instance. e.g. a good static
        field/property would be InstanceCount, which can be updated whenever you
        create a new instance of the form to reflect how many instances of that form
        class have been created.

        I cannot tell from your example whether you intend class use or a value for
        each instance.

        | The code below does not work but I'm not sure why not. And also why is it
        | necessary to declare the variable bIshShown twice?
        |
        |
        | Form1 code:
        |
        | private static bool bIsShown = false;

        This is a static field, applicable to the class Form1, not to instances of
        that class. If it were public, you would call it like this : bool test =
        Form1.blsShown;

        | public bool bIsShown
        | {
        | get{ return bIsShown; }
        | set { bIsShown = value; }
        | }

        Although this instance property can access the static field, both the field
        and the property have the same name and I would doubt if this even compiles.

        You need to decide whether you want to change the property to be static, or
        whether you want to change the field to be an instance field that would
        reflect the state of form1 rather than the class Form1.

        Joanna

        --
        Joanna Carter [TeamB]
        Consultant Software Engineer


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Static properties

          RSH <way_beyond_oop s@yahoo.com> wrote:[color=blue]
          > I'm having a bit of trouble understanding Static properties.
          >
          > I have two forms and I need to be able to access a property from from1 in
          > form2.
          >
          > The code below does not work but I'm not sure why not.[/color]

          Well, for two reasons:

          1) Assuming that form1 is a reference to an instance of Form1, you're
          trying to access a static property as if it were an instance property.

          2) You can't declare two members with the same name (which you're
          trying to).

          Note that you've got a static variable but an instance property - this
          is almost certanily not what you actually want. Either both should be
          static or neither should.
          [color=blue]
          > And also why is it necessary to declare the variable bIshShown twice?[/color]

          You're not - you're declaring a variable, and a property.

          I'd recommend not using Hungarian naming, by the way - certainly not
          for public members. (I've never liked it for variables either, but I
          know some people do.) The naming conventions can be found at
          http://tinyurl.com/2cun

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Static properties

            Peter Rilling <peter@nospam.r illing.net> wrote:[color=blue]
            > Fields store data, properties do not. You are not declaring a variable
            > twice, you are defining two different types of objects with the same name.
            > (Also, you might choose to name the private variable something other than
            > the property for readability. I tend to put m_ at the from of all my
            > private variables, others have different conventions.)[/color]

            That much is true.
            [color=blue]
            > Only static properties can assess static fields. Make the property static
            > as well.[/color]

            That isn't. A static property can't access an instance field (without
            an instance to refer to) but an instance property can refer to a static
            variable. Here's an example - which is also a demonstration of why it's
            a bad idea...

            using System;

            public class Test
            {
            static string name;

            public string Name
            {
            get { return name; }
            set { name = value; }
            }

            static void Main()
            {
            Test t1 = new Test();
            Test t2 = new Test();

            t1.Name = "Fred";
            t2.Name = "Bill";

            Console.WriteLi ne (t1.Name);
            }
            }

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • RSH

              #7
              Re: Static properties

              Thanks!

              That definitely makes sense now.
              I was looking at it all wrong...it was somewhat confusing that all of the
              examples I saw were using the same variable name only the case was different
              (I see that now)...but they indeed are very different animals.

              Thanks!

              Ron
              private static bool bishown;

              public static bool bIsShown

              {

              get

              {

              return bishown;

              }

              set

              {

              bishown = value;

              }

              }





              "Joanna Carter [TeamB]" <joanna@not.for .spam> wrote in message
              news:%23xRXevJE GHA.3148@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              > "RSH" <way_beyond_oop s@yahoo.com> a écrit dans le message de news:
              > u88EolJEGHA.112 0@TK2MSFTNGP11. phx.gbl...
              >
              > | I have two forms and I need to be able to access a property from from1
              > in
              > | form2.
              >
              > You need to remember that the code that you write in a module containing a
              > form class is*not* a form, it represents what goes on in an instance of
              > the
              > class that you are writing.
              >
              > Static fields/properties are a way of accessing state that is common to
              > the
              > *type* of the form class, not an *instance* of that class.
              >
              > You can create more than one instance of any class therefore, whether you
              > use a static field/property or an instance field/property depends on
              > whether
              > you want to restrict the value of that member to represent something
              > relevent to the class rather than an instance. e.g. a good static
              > field/property would be InstanceCount, which can be updated whenever you
              > create a new instance of the form to reflect how many instances of that
              > form
              > class have been created.
              >
              > I cannot tell from your example whether you intend class use or a value
              > for
              > each instance.
              >
              > | The code below does not work but I'm not sure why not. And also why is
              > it
              > | necessary to declare the variable bIshShown twice?
              > |
              > |
              > | Form1 code:
              > |
              > | private static bool bIsShown = false;
              >
              > This is a static field, applicable to the class Form1, not to instances of
              > that class. If it were public, you would call it like this : bool test =
              > Form1.blsShown;
              >
              > | public bool bIsShown
              > | {
              > | get{ return bIsShown; }
              > | set { bIsShown = value; }
              > | }
              >
              > Although this instance property can access the static field, both the
              > field
              > and the property have the same name and I would doubt if this even
              > compiles.
              >
              > You need to decide whether you want to change the property to be static,
              > or
              > whether you want to change the field to be an instance field that would
              > reflect the state of form1 rather than the class Form1.
              >
              > Joanna
              >
              > --
              > Joanna Carter [TeamB]
              > Consultant Software Engineer
              >
              >[/color]


              Comment

              Working...