static / new / singleton

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

    #1

    static / new / singleton

    Hello,

    I have a simple question :

    In the below code, why do i get an (ERROR) saying
    "An object reference is required for the nonstatic field, method, or
    property 'mLockeEnv3'"

    I don't understand why mLockeEnv3 should be static.....

    Also, what is the difference betwenn mLockeEnv & mLockeEnv2 as both
    declarations are working....


    public class CLockeMain
    {
    public static CLockeEnv mlockeEnv;
    public static CLockeEnv mlockeEnv2 = new CLockeEnv();
    public CLockeEnv mLockeEnv3;

    static void Main()
    {
    mLockeEnv = CLockeEnv.GetIn stance();
    mLockeEnv2 = CLockeEnv.GetIn stance();
    mLockeEnv3 = CLockeEnv.GetIn stance(); (ERROR)
    }
    }



    Note :
    CLockeEnv is a singleton class with an internal private member pointing
    to itsel. GetInstance creates the object or returns the existing
    instance.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: static / new / singleton

    Cybertof,

    When you declare a field without the static modifier, that field is
    accessible only to the instance. Because you are making the assignment in a
    static method, the static method doesn't know which instance to set the
    field to, so it must be static.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19eccc 508b4bc8fe98968 5@msnews.micros oft.com...[color=blue]
    > Hello,
    >
    > I have a simple question :
    >
    > In the below code, why do i get an (ERROR) saying
    > "An object reference is required for the nonstatic field, method, or
    > property 'mLockeEnv3'"
    >
    > I don't understand why mLockeEnv3 should be static.....
    >
    > Also, what is the difference betwenn mLockeEnv & mLockeEnv2 as both
    > declarations are working....
    >
    >
    > public class CLockeMain
    > {
    > public static CLockeEnv mlockeEnv;
    > public static CLockeEnv mlockeEnv2 = new CLockeEnv();
    > public CLockeEnv mLockeEnv3;
    >
    > static void Main()
    > {
    > mLockeEnv = CLockeEnv.GetIn stance();
    > mLockeEnv2 = CLockeEnv.GetIn stance();
    > mLockeEnv3 = CLockeEnv.GetIn stance(); (ERROR)
    > }
    > }
    >
    >
    >
    > Note :
    > CLockeEnv is a singleton class with an internal private member pointing
    > to itsel. GetInstance creates the object or returns the existing
    > instance.[/color]


    Comment

    • Cybertof

      #3
      Re: static / new / singleton

      I understand what you mean.

      Why are generated winforms have a 'static' Main ?

      Most of the time declared like this :
      //[STAThread]
      static void Main()
      {...}

      What are the consequences to remove the [STAThread] ?
      What are the consequences to remove the static keyword in Winforms
      generated code ?


      Regards,
      Christophe.




      In article <#FJjN6MjDHA.25 60@TK2MSFTNGP12 .phx.gbl>,
      nicholas.paldin o@exisconsultin g.com says...[color=blue]
      > Cybertof,
      >
      > When you declare a field without the static modifier, that field is
      > accessible only to the instance. Because you are making the assignment in a
      > static method, the static method doesn't know which instance to set the
      > field to, so it must be static.
      >
      > Hope this helps.
      >
      >
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: static / new / singleton

        Cybertof,

        If you remove the STAThread attribute, then some of your windows forms
        controls might not work. The attribute is a way of indicating to the
        runtime that this thread is a single-threaded apartment thread, meaning that
        all access is synchronized (through the windows message loop). Some windows
        forms controls which are COM controls require this.

        If you remove the static keyword from the Main method, then your code
        will not run. When the runtime looks through your code to execute, it
        doesn't have any instances of any of your objects. The only thing it can
        call to begin a program is a static method, which is on a type, which it can
        locate (and not have to create like an instance).


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nick(dot)paldin o=at=exisconsul ting<dot>com

        "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
        news:MPG.19ecd2 73a7d5da1898968 6@msnews.micros oft.com...[color=blue]
        > I understand what you mean.
        >
        > Why are generated winforms have a 'static' Main ?
        >
        > Most of the time declared like this :
        > //[STAThread]
        > static void Main()
        > {...}
        >
        > What are the consequences to remove the [STAThread] ?
        > What are the consequences to remove the static keyword in Winforms
        > generated code ?
        >
        >
        > Regards,
        > Christophe.
        >
        >
        >
        >
        > In article <#FJjN6MjDHA.25 60@TK2MSFTNGP12 .phx.gbl>,
        > nicholas.paldin o@exisconsultin g.com says...[color=green]
        > > Cybertof,
        > >
        > > When you declare a field without the static modifier, that field is
        > > accessible only to the instance. Because you are making the assignment[/color][/color]
        in a[color=blue][color=green]
        > > static method, the static method doesn't know which instance to set the
        > > field to, so it must be static.
        > >
        > > Hope this helps.
        > >
        > >
        > >[/color][/color]


        Comment

        • Cybertof

          #5
          Re: static / new / singleton

          Nicholas,

          Thanks for your explanation.
          So, if i resume, I cannot declare in my main class a public instance of
          a class and access it directly from the static main proc (for example to
          instanciate it).
          If i want to do this, i have to declare my public class to be static.
          Or maybe I instanciate a new class beeing declared within the main proc
          (and not in the class body) but then i cannot access to it from an
          external class (as it is a member of a proc and not a class).

          Are there any good solutions ?

          Also, I declare a public class Class1 in a source1.cs file, this Class1
          is beeing instancied somewhere.
          How can i have access to it from another class (in another source2.cs
          file) for example ? Do i need to call a method that returns me a
          reference to an existing instance (so this is the singleton method) or
          is there another solution (not to use static auto-referencing
          'singleton' classes) ?


          Regards,
          Christophe.


          In article <#tKNFENjDHA.11 68@tk2msftngp13 .phx.gbl>,
          nicholas.paldin o@exisconsultin g.com says...[color=blue]
          > Cybertof,
          >
          > If you remove the STAThread attribute, then some of your windows forms
          > controls might not work. The attribute is a way of indicating to the
          > runtime that this thread is a single-threaded apartment thread, meaning that
          > all access is synchronized (through the windows message loop). Some windows
          > forms controls which are COM controls require this.
          >
          > If you remove the static keyword from the Main method, then your code
          > will not run. When the runtime looks through your code to execute, it
          > doesn't have any instances of any of your objects. The only thing it can
          > call to begin a program is a static method, which is on a type, which it can
          > locate (and not have to create like an instance).
          >
          >
          >[/color]

          Comment

          • Cybertof

            #6
            Re: static / new / singleton

            Nicholas, do you have an opinion on my last message ?

            Thanks.

            Christophe.

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: static / new / singleton

              Cybertof,

              I am not quite sure what you want to do. You can have ANY class's Main
              method be the entry point. From there, you can create any instances of any
              public classes that you wish, and that is where everything begins. If you
              want separate instances of classes to be aware of each other, then you have
              to give them access to the reference, whether by passing it through a
              method, or setting a property/field, or even setting it in a static field
              somewhere where the other instance can get at it. No matter what, the
              responsibility is on you to make it accessible to the other instances.


              --
              - Nicholas Paldino [.NET/C# MVP]
              - nick(dot)paldin o=at=exisconsul ting<dot>com

              "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
              news:MPG.19ece2 54300d431f98968 a@msnews.micros oft.com...[color=blue]
              > Nicholas, do you have an opinion on my last message ?
              >
              > Thanks.
              >
              > Christophe.
              >[/color]


              Comment

              • Cybertof

                #8
                Re: static / new / singleton

                In fact, i'm just wondering where to put global variables in my
                application code that would be accessible from various classes that may
                be created in the application.
                The only way i have in mind now is to make a static singleton class that
                would return a same reference to itself each time a class asks for it.
                Or maybe a global static struct...
                Are there other ways to do this (global variable storage) ?


                Thanks.
                Cybertof.


                In article <eqI3VAQjDHA.19 60@TK2MSFTNGP12 .phx.gbl>,
                nicholas.paldin o@exisconsultin g.com says...[color=blue]
                > Cybertof,
                >
                > I am not quite sure what you want to do. You can have ANY class's Main
                > method be the entry point. From there, you can create any instances of any
                > public classes that you wish, and that is where everything begins. If you
                > want separate instances of classes to be aware of each other, then you have
                > to give them access to the reference, whether by passing it through a
                > method, or setting a property/field, or even setting it in a static field
                > somewhere where the other instance can get at it. No matter what, the
                > responsibility is on you to make it accessible to the other instances.
                >
                >
                >[/color]

                Comment

                • twt

                  #9
                  Re: static / new / singleton

                  try a class (call it Global) and make all its field static (for changable
                  fields) or const (for constant ones).

                  === twt

                  "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                  news:MPG.19ee34 64951b4a6f98969 8@msnews.micros oft.com...[color=blue]
                  > In fact, i'm just wondering where to put global variables in my
                  > application code that would be accessible from various classes that may
                  > be created in the application.
                  > The only way i have in mind now is to make a static singleton class that
                  > would return a same reference to itself each time a class asks for it.
                  > Or maybe a global static struct...
                  > Are there other ways to do this (global variable storage) ?
                  >
                  >
                  > Thanks.
                  > Cybertof.
                  >
                  >
                  > In article <eqI3VAQjDHA.19 60@TK2MSFTNGP12 .phx.gbl>,
                  > nicholas.paldin o@exisconsultin g.com says...[color=green]
                  > > Cybertof,
                  > >
                  > > I am not quite sure what you want to do. You can have ANY class's[/color][/color]
                  Main[color=blue][color=green]
                  > > method be the entry point. From there, you can create any instances of[/color][/color]
                  any[color=blue][color=green]
                  > > public classes that you wish, and that is where everything begins. If[/color][/color]
                  you[color=blue][color=green]
                  > > want separate instances of classes to be aware of each other, then you[/color][/color]
                  have[color=blue][color=green]
                  > > to give them access to the reference, whether by passing it through a
                  > > method, or setting a property/field, or even setting it in a static[/color][/color]
                  field[color=blue][color=green]
                  > > somewhere where the other instance can get at it. No matter what, the
                  > > responsibility is on you to make it accessible to the other instances.
                  > >
                  > >
                  > >[/color][/color]


                  Comment

                  Working...