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. Get & Set

Get & Set

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 6 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.
  • L Offline
    L Offline
    Legolas_Bilbao
    wrote on last edited by
    #1

    when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

    Dios creo un equipo perfecto a los demas los lleno de extranjeros

    K D N V L 5 Replies Last reply
    0
    • L Legolas_Bilbao

      when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

      Dios creo un equipo perfecto a los demas los lleno de extranjeros

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #2

      You should use properties for this, e.g.,

      public string Address
      {
      get
      {
      return this.address;
      }
      set
      {
      this.address = value;
      }
      }

      Or, just for get.

      public string Address
      {
      get
      {
      return this.address;
      }
      }

      Kevin

      1 Reply Last reply
      0
      • L Legolas_Bilbao

        when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

        Dios creo un equipo perfecto a los demas los lleno de extranjeros

        D Offline
        D Offline
        Divyang Mithaiwala
        wrote on last edited by
        #3

        Hello Legolas, Actually set & get both are keyword of C#. You can use it when you define any property for class like...

        class TimePeriod
        {
            private double seconds;
        
            public double Hours
            {
                get { return seconds / 3600; }
                set { seconds = value * 3600; }
            }
        }
        

        You can get more help from here.


        regards, Divyang Mithaiwala System Engineer & Software Developer

        1 Reply Last reply
        0
        • L Legolas_Bilbao

          when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

          Dios creo un equipo perfecto a los demas los lleno de extranjeros

          N Offline
          N Offline
          NaNg15241
          wrote on last edited by
          #4

          If you want to use get & set you have to use a Property. (Unless you are programming in Java which don't have get & set) It seems like you have two options. 1. To have TWO methods FOR EACH property, of get and set, eg. string getName() { return this.name; } void setName(string NewName) { this.name = NewName; } (Just like in Java) 2. You can use it as a Property (the easyer and more comfort one), eg.:

          string Name
          {
            get { return this.name; }
            set { this.name = value; }
          }
          
          and to use it in code:
          string thisname = obj1.Name;
          obj1.Name = "My New Name";
          

          (You don't use the "()" in a Property) You can learn a little bit more if you read a little about this chapter. Hope I helped. NaNg.

          1 Reply Last reply
          0
          • L Legolas_Bilbao

            when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

            Dios creo un equipo perfecto a los demas los lleno de extranjeros

            V Offline
            V Offline
            vikas amin
            wrote on last edited by
            #5

            using System; class Circle { public int X { get { return(x); } set { x = value; // draw the object here. } } int x; } class Test { public static void Main() { Circle c = new Circle(); c.X = 35; } } In this code, the get or set accessor is called when the property X is referenced.

            Vikas Amin EATON PUNE

            1 Reply Last reply
            0
            • L Legolas_Bilbao

              when i declared a method string getName(){return this.name}; string getAddress(){return this.address;}... get appears like blue color its means c# word, then this methods get and set how can i use them or how its use i hope, i can explain good

              Dios creo un equipo perfecto a los demas los lleno de extranjeros

              L Offline
              L Offline
              Legolas_Bilbao
              wrote on last edited by
              #6

              But whatis better using get & set or using getName();??

              Dios creo un equipo perfecto a los demas los lleno de extranjeros

              E 1 Reply Last reply
              0
              • L Legolas_Bilbao

                But whatis better using get & set or using getName();??

                Dios creo un equipo perfecto a los demas los lleno de extranjeros

                E Offline
                E Offline
                eggsovereasy
                wrote on last edited by
                #7

                You don't have to type () with properties :P

                L 1 Reply Last reply
                0
                • E eggsovereasy

                  You don't have to type () with properties :P

                  L Offline
                  L Offline
                  Legolas_Bilbao
                  wrote on last edited by
                  #8

                  :laugh::laugh::laugh::laugh::laugh::laugh: ok dont problem ;)

                  Dios creo un equipo perfecto a los demas los lleno de extranjeros

                  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