locked
Chapter 1 RRS feed

  • Question

  • User344225350 posted

    Hi there,

    I've been taking a look at the following tutorial http://www.asp.net/mvc/tutorials/overview/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript and a couple of things jumped out at me. I was wondering if there was a reason for these.

     

    In the class definition for UserModel, the [Required()] attribute on the City property has an empty set of parenthesis, is there a reason for that?

     

    Also, I noticed _usrList is declared as public. Maybe it's just a typo but it seems like an odd thing to do especially when it's preceded with an underscore.

     

    Thank's for your time, I'm actually most curious about the req'd attribute.

    Wednesday, January 25, 2012 1:03 PM

Answers

  • User-1620313041 posted

    In the class definition for UserModel, the [Required()] attribute on the City property has an empty set of parenthesis, is there a reason for that?

    Often attributes have parameters that are enclosed between the parentheses. If there are no aparameters one can simply write [Required], but [Required()] is also possible...they are EXACTLY THE SAME THING

    Also, I noticed _usrList is declared as public. Maybe it's just a typo but it seems like an odd thing to do especially when it's preceded with an underscore.

    In C# normally developers uses  an upper case letters as first letter of a public members, so ...yes it is a bad choice...or  a kindg of typo....but may be also it is a private var that has been promoted to a public for some reason(need to access the from some other place...) ...then the developer avoided to rename it to avoid the risk of introducing bugs...(very low risk...VS 2010 has good re-engineering tools). Also promoting a private to public is a bad practice...better to re-engineer the class in this case, or adding some method that do the job of accessing the member.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 25, 2012 4:28 PM
  • User-1005219520 posted

    francesco is correct. I've retired that tutorial since my Intro to ASP.NET MVC covers everything in the old unobtrusive tutorial

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2012 1:27 PM

All replies

  • User-1620313041 posted

    In the class definition for UserModel, the [Required()] attribute on the City property has an empty set of parenthesis, is there a reason for that?

    Often attributes have parameters that are enclosed between the parentheses. If there are no aparameters one can simply write [Required], but [Required()] is also possible...they are EXACTLY THE SAME THING

    Also, I noticed _usrList is declared as public. Maybe it's just a typo but it seems like an odd thing to do especially when it's preceded with an underscore.

    In C# normally developers uses  an upper case letters as first letter of a public members, so ...yes it is a bad choice...or  a kindg of typo....but may be also it is a private var that has been promoted to a public for some reason(need to access the from some other place...) ...then the developer avoided to rename it to avoid the risk of introducing bugs...(very low risk...VS 2010 has good re-engineering tools). Also promoting a private to public is a bad practice...better to re-engineer the class in this case, or adding some method that do the job of accessing the member.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 25, 2012 4:28 PM
  • User-1005219520 posted

    francesco is correct. I've retired that tutorial since my Intro to ASP.NET MVC covers everything in the old unobtrusive tutorial

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2012 1:27 PM