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. how to create Const Member methods inside a class. how to create const objects of a Class.

how to create Const Member methods inside a class. how to create const objects of a Class.

Scheduled Pinned Locked Moved C#
tutorial
9 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.
  • A Offline
    A Offline
    a_ravindra
    wrote on last edited by
    #1

    how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

    P E D Y 5 Replies Last reply
    0
    • A a_ravindra

      how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

      P Offline
      P Offline
      Paladin2000
      wrote on last edited by
      #2

      Perhaps you mean 'static', not 'constant'?

      1 Reply Last reply
      0
      • A a_ravindra

        how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        Can't really, however, the example below is close and may introduce some concepts that will get you close to your goal.

        public static class Program{

        private static readonly Foo mFoo;
        
        public static void Main(){
            //code here
        }
        static Program(){
           mFoo = new Foo(1);
        }
        

        }
        public class Foo{

        public int Value{
            get;
            private set;
        }
        
        public Foo(int value){
            Value = value;
        }
        

        }

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        A 1 Reply Last reply
        0
        • A a_ravindra

          how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

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

          There is no such thing as a Const method. Perhaps you're thinking about a Static method? Also, there is no such thing as a class declared as Const. Again, you're thinking about static??

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          1 Reply Last reply
          0
          • A a_ravindra

            how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

            P Offline
            P Offline
            Paladin2000
            wrote on last edited by
            #5

            Perhaps this is what you wanted..?

            public static class Foo
            {
            public static int AddOne(int Input)
            {
            return ++Input;
            }
            }

            class Program
            {
            static void Main(string[] args)
            {
            Console.WriteLine(Foo.AddOne(1));
            Console.Read();
            }
            }

            1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              Can't really, however, the example below is close and may introduce some concepts that will get you close to your goal.

              public static class Program{

              private static readonly Foo mFoo;
              
              public static void Main(){
                  //code here
              }
              static Program(){
                 mFoo = new Foo(1);
              }
              

              }
              public class Foo{

              public int Value{
                  get;
                  private set;
              }
              
              public Foo(int value){
                  Value = value;
              }
              

              }

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

              A Offline
              A Offline
              a_ravindra
              wrote on last edited by
              #6

              Thanks. How do you create a method , which will only be allowed to read the data Members, but can not be allowed to modify the data of the object . The static method, can still modify the static data members of the class.

              E Y 2 Replies Last reply
              0
              • A a_ravindra

                Thanks. How do you create a method , which will only be allowed to read the data Members, but can not be allowed to modify the data of the object . The static method, can still modify the static data members of the class.

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                The only static method is Main and it cannot change the value of mFoo since it is read only. Furthermore, the Value property in Foo has a private set property meaning that only the public property is generally accessible.

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                1 Reply Last reply
                0
                • A a_ravindra

                  how to create Const Member methods inside a class. how to create const objects of a Class. Class Base { int member1=10; public void ModifyMember() // I need to make this method Const { member1 +=1; } } //In Main public static void Main() { //how to creat cons objects of class Base }

                  Y Offline
                  Y Offline
                  yu jian
                  wrote on last edited by
                  #8

                  I only hear that the static method and const variable

                  1 Reply Last reply
                  0
                  • A a_ravindra

                    Thanks. How do you create a method , which will only be allowed to read the data Members, but can not be allowed to modify the data of the object . The static method, can still modify the static data members of the class.

                    Y Offline
                    Y Offline
                    yu jian
                    wrote on last edited by
                    #9

                    Perhaps you can use "get" keywords public static string yourProperty { get { return ....} }

                    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