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. OK that's a new one (strange use of colon operator)

OK that's a new one (strange use of colon operator)

Scheduled Pinned Locked Moved C#
questioncomalgorithmstoolsoop
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.
  • J Offline
    J Offline
    Jasmine2501
    wrote on last edited by
    #1

    Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

    "Quality Software since 1983!"
    http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

    G L P C J 5 Replies Last reply
    0
    • J Jasmine2501

      Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

      "Quality Software since 1983!"
      http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Jasmine2501 wrote:

      Is this the same as calling that one with 2000 and 1 as default values?

      Well, not default values but actual values, but, yes.

      Jasmine2501 wrote:

      or call the other constructor within this one

      No, you can't call another constructor in the code, that's the purpose of the special syntax.

      Experience is the sum of all the mistakes you have done.

      1 Reply Last reply
      0
      • J Jasmine2501

        Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

        "Quality Software since 1983!"
        http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Jasmine2501 wrote:

        but what is that stuff after it

        The class constructor can also invoke another constructor from the same class by using the keyword this, for example: http://msdn2.microsoft.com/en-us/library/k6sa6h87(vs.71).aspx[^]

        led mike

        1 Reply Last reply
        0
        • J Jasmine2501

          Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

          "Quality Software since 1983!"
          http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

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

          It calls the constructor in the current class that has two int parameters and initializes them with the values 2000 and 1. You can fall through these constructors many times as in the following example:

          public SearchCriteria() : this(2000, 1) {}
          public SearchCriteria(int value, int otherValue) : this(value, otherValue, 20) {}
          public SearchCriteria(int value, int otherValue, int finalValue)
          {
            _value = value;
            _otherValue = otherValue;
            _finalValue = finalValue;
          }
          

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          1 Reply Last reply
          0
          • J Jasmine2501

            Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

            "Quality Software since 1983!"
            http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

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

            I should also point out that this is covered in books such as C# Step-by-Step (Microsoft Press). i.e. it is a very basic concept in the language that perhaps you might want to revise.

            Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * The Value of Smaller Methods * Creating Many-to-Many joins My website | blog

            1 Reply Last reply
            0
            • J Jasmine2501

              Just when you think you've seen everything... public SearchCriteria() : this(2000, 1) {} ...this is a default constructor for the SearchCriteria object... but what is that stuff after it... I can't find any documentation of this feature because searching for colon operator only gives me articles about inheritance of classes and interfaces. What are the implications of this construct, and what does it do... There is another constructor that takes two integers. Is this the same as calling that one with 2000 and 1 as default values? If it is, I'm confused as to the purpose of this notation, when you could just as easily initialize those values "normally" or call the other constructor within this one, or something. There must be a reason for this colon thingy?

              "Quality Software since 1983!"
              http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

              J Offline
              J Offline
              Jasmine2501
              wrote on last edited by
              #6

              I guess in my particular case, it didn't make a lot of sense because, for this particular class, it's the same as: public SearchCriteria() { m_year = 2000; m_month = 1; } And in my mind this is much more readable and efficient. If the thing was doing more work with the values, I could understand the use of this construct. The other constructor looks like: public SearchCriteria(int yr, int mo) { m_year = yr; m_month = mo; } I think it's some contractor's idea to getting more hours, making the code harder to read, and hoping that the company will be dependent on him in the future. I'm seeing quite a few things which are obviously that kind of trick, but this one got me interested as to whether there was a good reason for it or not. This particular class is way overblown... it's one of those classes that could have just as easily been a struct, because it doesn't actually do any processing, it's only an object to aggregate some values together and it never actually does anything with those values besides store them.

              "Quality Software since 1983!"
              http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

              P 1 Reply Last reply
              0
              • J Jasmine2501

                I guess in my particular case, it didn't make a lot of sense because, for this particular class, it's the same as: public SearchCriteria() { m_year = 2000; m_month = 1; } And in my mind this is much more readable and efficient. If the thing was doing more work with the values, I could understand the use of this construct. The other constructor looks like: public SearchCriteria(int yr, int mo) { m_year = yr; m_month = mo; } I think it's some contractor's idea to getting more hours, making the code harder to read, and hoping that the company will be dependent on him in the future. I'm seeing quite a few things which are obviously that kind of trick, but this one got me interested as to whether there was a good reason for it or not. This particular class is way overblown... it's one of those classes that could have just as easily been a struct, because it doesn't actually do any processing, it's only an object to aggregate some values together and it never actually does anything with those values besides store them.

                "Quality Software since 1983!"
                http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                There is a rationale for using this construct: Imagine the SearchCriteria class is modified to include additional initializations: private string m_whereClause; public SearchCriteria(int yr, int mo) { m_year = yr; m_month = mo; m_searchRX = new Regex(....); // precompile search criteria m_resultCache = null; } using SearchCriteria() : this(2000, 1) {} keeps the change and additional code in one place. Maybe this does not make any sense for the class you are using, but generally I would recommend using the this(...) in such a scenario.

                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                J 1 Reply Last reply
                0
                • P peterchen

                  There is a rationale for using this construct: Imagine the SearchCriteria class is modified to include additional initializations: private string m_whereClause; public SearchCriteria(int yr, int mo) { m_year = yr; m_month = mo; m_searchRX = new Regex(....); // precompile search criteria m_resultCache = null; } using SearchCriteria() : this(2000, 1) {} keeps the change and additional code in one place. Maybe this does not make any sense for the class you are using, but generally I would recommend using the this(...) in such a scenario.

                  We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                  blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                  J Offline
                  J Offline
                  Jasmine2501
                  wrote on last edited by
                  #8

                  Yes I agree. It does seem handy in that case, and I'll keep it in mind if that kind of situation ever arises. In this particular case though, it's just weird.

                  "Quality Software since 1983!"
                  http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

                  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