{"id":169,"date":"2010-03-23T14:13:00","date_gmt":"2010-03-23T08:43:00","guid":{"rendered":"https:\/\/ashokaon.tech\/blog\/?p=169"},"modified":"2020-07-18T01:23:43","modified_gmt":"2020-07-17T19:53:43","slug":"simple-auto-implemented-properties-in-visual-basic-2010","status":"publish","type":"post","link":"https:\/\/ashokaon.tech\/blog\/simple-auto-implemented-properties-in-visual-basic-2010\/","title":{"rendered":"Auto-Implemented properties comes to Visual Basic 2010"},"content":{"rendered":"<p><span style=\"font-size: 10px;background-color:#FFFF99;display:block\">(Note: This blog post was originally published under my old domain(codesmiles.com), <a href=\"http:\/\/codesmiles.com\/post\/simple-auto-implemented-properties-in-visual-basic-2010.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. <a href=\"https:\/\/web.archive.org\/web\/2010*\/http:\/\/www.codehappiness.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Web Archive link<\/a>.<\/span><\/p>\n<p style=\"color: #777; font-size: 11px\">(This post is part of my <a href=\"http:\/\/www.codesmiles.com\/post\/visual-studio-2010-new-features.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio 2010 series<\/a>)<\/p>\n<p>In C# we can create properties for classes in simple way by just specifying the below code, this option is called auto-implemented property, as the implementation is taken care by the compiler.[more]<\/p>\n<p><span style=\"font-family: courier new\"> <\/span><\/p>\n<pre class=\"brush: c-sharp;\">public int Price { get; set; }<\/pre>\n<p>&nbsp;<\/p>\n<p>In Visual Basic we don\u2019t have such simple option to create properties, hence to create a simple class we have to write the below code..<\/p>\n<p><span style=\"font-family: courier new\"> <\/span><\/p>\n<pre class=\"brush: vb;\">Class Client\r\n    Private _Code As String\r\n    Public Property Code() As String\r\n        Get\r\n            Return _Code\r\n        End Get\r\n        Set(ByVal value As String)\r\n            _Code = value\r\n        End Set\r\n    End Property\r\n\r\n    Private _Name As String\r\n    Public Property Name() As String\r\n        Get\r\n            Return _Name\r\n        End Get\r\n        Set(ByVal value As String)\r\n            _Name = value\r\n        End Set\r\n    End Property\r\n\r\n    Private _CreditLimit As Single = 2000\r\n    Public Property CreditLimit() As Single\r\n        Get\r\n            Return _CreditLimit\r\n        End Get\r\n        Set(ByVal value As Single)\r\n            _CreditLimit = value\r\n        End Set\r\n    End Property\r\nEnd Class<\/pre>\n<p>&nbsp;<\/p>\n<p>But in Visual Basic 2010(in Visual Studio 2010) we can write the below code instead of the above..<\/p>\n<p><span style=\"font-family: courier new\"> <\/span><\/p>\n<pre class=\"brush: vb;\">Class Client\r\n    Public Property Code As String\r\n    Public Property Name As String\r\n    Public Property CreditLimit As Single = 2000\r\nEnd Class<\/pre>\n<p>&nbsp;<\/p>\n<p>See how much simplicity and clarity auto-implemented properties can give to your classes when compared to your manually-implemented properties.<\/p>\n<p>Most of the business entity classes I have seen in projects have such simple properties, I mean, only few require some custom logic to be included in the property method, like the one below..<\/p>\n<p><span style=\"font-family: courier new\"> <\/span><\/p>\n<pre class=\"brush: vb;\">Private _CreditLimit As Single = 2000\r\nPublic Property CreditLimit() As Single\r\n    Get\r\n        Return _CreditLimit\r\n    End Get\r\n    Set(ByVal value As Single)\r\n        If value &gt; 5000 Then\r\n            Throw New Exception(\"CreditLimit cannot be greater than 5000\")\r\n        End If\r\n\r\n        _CreditLimit = value\r\n    End Set\r\nEnd Property<\/pre>\n<p>&nbsp;<\/p>\n<p>So if you are a Visual Basic coder you can start creating your classes, particularly your business entities with such simple code in Visual Basic 2010.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Note: This blog post was originally published under my old domain(codesmiles.com), here. Web Archive link. (This post is part of my Visual Studio 2010 series) In C# we can create properties for classes in simple way by just specifying the below code, this option is called auto-implemented property, as the<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[30],"_links":{"self":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":4,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions\/314"}],"wp:attachment":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}