static class

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

    #1

    static class

    Is it possible to declare a class as static? I can't seem to do that.

    What if I need a class that I don't want instantiated? It will contain only
    static members.

    Thanks.


  • Alien

    #2
    Re: static class

    You can't make a class static. Just make a regular class and don't
    instantiate it.

    "someone" <a@a.com> wrote in message
    news:%23zJeIMKS DHA.3132@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Is it possible to declare a class as static? I can't seem to do that.
    >
    > What if I need a class that I don't want instantiated? It will contain[/color]
    only[color=blue]
    > static members.
    >
    > Thanks.
    >
    >[/color]


    Comment

    • someone

      #3
      Re: static class

      Do you know how the Math class is defined?

      If I try:
      Math m = new Math();
      I get:
      'System.Math.Ma th()' is inaccessible due to its protection level

      But of course I can do this:
      double d = Math.PI;

      That's the type of functionality I want.

      Thanks.



      "Alien" <alien@sympatic o.ca> wrote in message
      news:ZKYPa.1951 3$sI91.11642@ne ws04.bloor.is.n et.cable.rogers .com...[color=blue]
      > You can't make a class static. Just make a regular class and don't
      > instantiate it.
      >
      > "someone" <a@a.com> wrote in message
      > news:%23zJeIMKS DHA.3132@tk2msf tngp13.phx.gbl. ..[color=green]
      > > Is it possible to declare a class as static? I can't seem to do that.
      > >
      > > What if I need a class that I don't want instantiated? It will contain[/color]
      > only[color=green]
      > > static members.
      > >
      > > Thanks.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Bigtoga

        #4
        Re: static class

        I'm pretty new to C# so please someone correct me if I'm wrong but, if I
        understand correctly, you can use double d = Math.PI; because Math makes
        available a public static property, method, field, etc titled PI (or
        whatever). Create your class as normal and create your
        methods/properties/fields as static.



        Comment

        • Piotr Zbyszyñski

          #5
          Re: static class

          > Do you know how the Math class is defined?[color=blue]
          >
          > If I try:
          > Math m = new Math();
          > I get:
          > 'System.Math.Ma th()' is inaccessible due to its protection level[/color]
          Make constructor private (if you don't want to instantiate objects of your
          class, you don't realy nead constructor, but if you do so, you will get
          warning if you try Myclass m = new Myclass())[color=blue]
          >
          > But of course I can do this:
          > double d = Math.PI;[/color]
          Make all members static
          [color=blue]
          >
          > That's the type of functionality I want.[/color]
          You will get what you want ;)))
          Greetings


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: static class

            Someone,
            Make the default constructor of your class private, so no one can create an
            instance of the class.

            Make the class itself sealed, so no one can derive from it.

            Only create static members in the class.

            Hope this helps
            Jay


            "someone" <a@a.com> wrote in message
            news:R_YPa.5749 3$ZS3.6496929@n ews2.telusplane t.net...[color=blue]
            > Do you know how the Math class is defined?
            >
            > If I try:
            > Math m = new Math();
            > I get:
            > 'System.Math.Ma th()' is inaccessible due to its protection level
            >
            > But of course I can do this:
            > double d = Math.PI;
            >
            > That's the type of functionality I want.
            >
            > Thanks.
            >
            >
            >
            > "Alien" <alien@sympatic o.ca> wrote in message
            > news:ZKYPa.1951 3$sI91.11642@ne ws04.bloor.is.n et.cable.rogers .com...[color=green]
            > > You can't make a class static. Just make a regular class and don't
            > > instantiate it.
            > >
            > > "someone" <a@a.com> wrote in message
            > > news:%23zJeIMKS DHA.3132@tk2msf tngp13.phx.gbl. ..[color=darkred]
            > > > Is it possible to declare a class as static? I can't seem to do that.
            > > >
            > > > What if I need a class that I don't want instantiated? It will contain[/color]
            > > only[color=darkred]
            > > > static members.
            > > >
            > > > Thanks.
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Ignacio Machin

              #7
              Re: static class

              Hi,

              You CANNOT make a class static, you can make methods, properties and
              variables of that class static , though.

              If you don't want an instance of that classes to be created you can declare
              the constructor as private therefore nobody can create it. Also remember
              that if you are going to have only static method then this constructor will
              never be called, therefore you will need to have a constructor marked as
              static

              Hope this help,

              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation

              "someone" <a@a.com> wrote in message
              news:%23zJeIMKS DHA.3132@tk2msf tngp13.phx.gbl. ..[color=blue]
              > Is it possible to declare a class as static? I can't seem to do that.
              >
              > What if I need a class that I don't want instantiated? It will contain[/color]
              only[color=blue]
              > static members.
              >
              > Thanks.
              >
              >[/color]


              Comment

              • Jon Skeet

                #8
                Re: static class

                "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote:[color=blue]
                > You CANNOT make a class static, you can make methods, properties and
                > variables of that class static , though.[/color]

                Agreed...
                [color=blue]
                > If you don't want an instance of that classes to be created you can declare
                > the constructor as private therefore nobody can create it.[/color]

                Agreed.
                [color=blue]
                > Also remember
                > that if you are going to have only static method then this constructor will
                > never be called, therefore you will need to have a constructor marked as
                > static[/color]

                Not agreed. You *may* need a static constructor, but in many cases you
                don't.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Ignacio Machin

                  #9
                  Re: static class

                  Hi Jon,

                  [color=blue][color=green]
                  > > Also remember
                  > > that if you are going to have only static method then this constructor[/color][/color]
                  will[color=blue][color=green]
                  > > never be called, therefore you will need to have a constructor marked as
                  > > static[/color]
                  >
                  > Not agreed. You *may* need a static constructor, but in many cases you
                  > don't.[/color]

                  Ok, you are right, the correct word is "may" and no "need" , the english is
                  not my first language and sometimes I mix the words :)


                  Cheers,

                  --
                  Ignacio Machin,
                  ignacio.machin AT dot.state.fl.us
                  Florida Department Of Transportation




                  Comment

                  Working...