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