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
  1. Home
  2. General Programming
  3. C#
  4. accessing public static member of one class in another class

accessing public static member of one class in another class

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
6 Posts 5 Posters 0 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.
  • K Offline
    K Offline
    KRISHNARAYALU
    wrote on last edited by
    #1

    Hi, i need to access public static member of one class into another class. please provide the help. Learning c#. Ex: class ABC { static int nEllipsecount = 0; // in some function increment nEllipseCount; }; // class xyz in another file class ABC; class xyz { if(ABC :: nEllipseCount >0) { //Process these statements; } }; i couldn't be able access static value into to another class. is my approach wrong ? Please help me to resolve. Thanks & Regards, Royal

    M D L 3 Replies Last reply
    0
    • K KRISHNARAYALU

      Hi, i need to access public static member of one class into another class. please provide the help. Learning c#. Ex: class ABC { static int nEllipsecount = 0; // in some function increment nEllipseCount; }; // class xyz in another file class ABC; class xyz { if(ABC :: nEllipseCount >0) { //Process these statements; } }; i couldn't be able access static value into to another class. is my approach wrong ? Please help me to resolve. Thanks & Regards, Royal

      M Offline
      M Offline
      Matt T Heffron
      wrote on last edited by
      #2

      First, as you've shown it, nEllipsecount is NOT public. In c# the default is private. So, what you seem to want is:

      class ABC
      {
      public static int nEllipsecount = 0; // made it public
      // in some function increment nEllipseCount;

      }

      // class xyz in another file

      class xyz
      {

      // below presumably in a method...
      if (ABC.nEllipseCount >0) // syntax for reference to class static
      {
      //Process these statements;

      }

      }

      If it isn't made public then you can't see it from xyz at all. (Unless it is protected and you inherit from ABC.)

      A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.

      K 1 Reply Last reply
      0
      • K KRISHNARAYALU

        Hi, i need to access public static member of one class into another class. please provide the help. Learning c#. Ex: class ABC { static int nEllipsecount = 0; // in some function increment nEllipseCount; }; // class xyz in another file class ABC; class xyz { if(ABC :: nEllipseCount >0) { //Process these statements; } }; i couldn't be able access static value into to another class. is my approach wrong ? Please help me to resolve. Thanks & Regards, Royal

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Your code is hinting that you're working with C++ and not C#. Is this true??

        A guide to posting questions on CodeProject

        How to debug small programs
        Dave Kreskowiak

        1 Reply Last reply
        0
        • K KRISHNARAYALU

          Hi, i need to access public static member of one class into another class. please provide the help. Learning c#. Ex: class ABC { static int nEllipsecount = 0; // in some function increment nEllipseCount; }; // class xyz in another file class ABC; class xyz { if(ABC :: nEllipseCount >0) { //Process these statements; } }; i couldn't be able access static value into to another class. is my approach wrong ? Please help me to resolve. Thanks & Regards, Royal

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Even in C++, which this is, you should not do that.

          1 Reply Last reply
          0
          • M Matt T Heffron

            First, as you've shown it, nEllipsecount is NOT public. In c# the default is private. So, what you seem to want is:

            class ABC
            {
            public static int nEllipsecount = 0; // made it public
            // in some function increment nEllipseCount;

            }

            // class xyz in another file

            class xyz
            {

            // below presumably in a method...
            if (ABC.nEllipseCount >0) // syntax for reference to class static
            {
            //Process these statements;

            }

            }

            If it isn't made public then you can't see it from xyz at all. (Unless it is protected and you inherit from ABC.)

            A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.

            K Offline
            K Offline
            KRISHNARAYALU
            wrote on last edited by
            #5

            Thanks alot Matt,

            public static int nEllipsecount = 0; // made it public

            In the above statement I'm declaring the static member in public only, kindly tell me any other way i can declare as public. Thanks & Regards, Royal

            P 1 Reply Last reply
            0
            • K KRISHNARAYALU

              Thanks alot Matt,

              public static int nEllipsecount = 0; // made it public

              In the above statement I'm declaring the static member in public only, kindly tell me any other way i can declare as public. Thanks & Regards, Royal

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You can't declare a public member/method/class etc, with anything other than the public keyword. C# requires explicit scope declaration if you're trying to do anything other than declare something as private, which is the default scope. What are you trying to achieve because using a public static is generally not a good idea?

              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