Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Class properties

Class properties

Scheduled Pinned Locked Moved C#
csharpcomtutorialquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 1 Offline
    1 Offline
    1nsp1r3d
    wrote on last edited by
    #1

    Hi all, While reading this tutorial, I encountered this: class SplashScreen : System.Windows.Forms.Form { ... static SplashScreen ms_frmSplash = null; ... } And I'm a bit worried because of this statement. How can a class have a property of itself? Or the static keyword makes it not act as a class property? Thanks.

    S C 2 Replies Last reply
    0
    • 1 1nsp1r3d

      Hi all, While reading this tutorial, I encountered this: class SplashScreen : System.Windows.Forms.Form { ... static SplashScreen ms_frmSplash = null; ... } And I'm a bit worried because of this statement. How can a class have a property of itself? Or the static keyword makes it not act as a class property? Thanks.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      ms_frmSplash is a *reference* to a SplashScreen object, not a SplashScreen object itself. If you had a SplashScreen object itself, then it would be a problem, the compiler wouldn't be able to generate the object layout without going into infinite recursion. References are of a fixed size (regardless of the referenced object), so the compiler has no problem with the above code. The static keyword has nothing to do with that. The code that you posted is very similar to something like this in C++

      class Node
      {
      Node *p;
      };

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • 1 1nsp1r3d

        Hi all, While reading this tutorial, I encountered this: class SplashScreen : System.Windows.Forms.Form { ... static SplashScreen ms_frmSplash = null; ... } And I'm a bit worried because of this statement. How can a class have a property of itself? Or the static keyword makes it not act as a class property? Thanks.

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        First, to correct the words that you are using, this is not a property, it is called a field in .NET (although many ex-C++ developers will call it a member variable). 1nsp1r3d wrote: How can a class have a property of itself? A class can have "fields" of any type. Including itself. As to how? Well, the code snipped you gave shows you how. 1nsp1r3d wrote: Or the static keyword makes it not act as a class property? The static keyword makes the "field" available at the class level rather than at the object instance level. A better question would have been "why did they do this?" If you are creating a "singleton" (that is a class that only ever has one instance through the lift of the application) then you need to create a static reference to the one and only instance of the class in the class. If an object is part of a chain of object then it will need to reference another object of the same type. For example, the "decarator pattern" does this. Does this help?


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        1 1 Reply Last reply
        0
        • C Colin Angus Mackay

          First, to correct the words that you are using, this is not a property, it is called a field in .NET (although many ex-C++ developers will call it a member variable). 1nsp1r3d wrote: How can a class have a property of itself? A class can have "fields" of any type. Including itself. As to how? Well, the code snipped you gave shows you how. 1nsp1r3d wrote: Or the static keyword makes it not act as a class property? The static keyword makes the "field" available at the class level rather than at the object instance level. A better question would have been "why did they do this?" If you are creating a "singleton" (that is a class that only ever has one instance through the lift of the application) then you need to create a static reference to the one and only instance of the class in the class. If an object is part of a chain of object then it will need to reference another object of the same type. For example, the "decarator pattern" does this. Does this help?


          My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

          1 Offline
          1 Offline
          1nsp1r3d
          wrote on last edited by
          #4

          Thanks go to both of you :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups