{"id":489,"date":"2019-12-24T09:02:00","date_gmt":"2019-12-24T16:02:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=489"},"modified":"2019-12-23T18:10:07","modified_gmt":"2019-12-24T01:10:07","slug":"structures-vs-classes","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/12\/24\/structures-vs-classes\/","title":{"rendered":"Structures vs Classes"},"content":{"rendered":"\n<p>While structures &amp; classes may seem to be very similar in most respects there aren&#8217;t many cases when I would suggest you use a structure instead of a class.<\/p>\n\n\n\n<p>A class with public member properties isnt that different from a structure in terms of how your code uses it.<\/p>\n\n\n\n<p>The biggest difference is that for a class you must use new to get a new instance. With a structure you just declare it and use it.<\/p>\n\n\n\n<p>Structures are handy for some things. If you have to read or write a specific binary file format you might use a structure. If you&#8217;re going to call some OS API via a declare then structures are useful. And if you&#8217;re reading or writing binary data to binary streams via sockets or other communication channels they are useful.<\/p>\n\n\n\n<p>But there are downsides. Since structures are JUST data any methods to manipulate them have to go in a module. Unlike a class you cannot add methods to the structure itself. And so you end up using a very non-OOP style &#8211; maybe a module with the structure defined in there along with all the code that manipulates the structure.<\/p>\n\n\n\n<p>Classes allow you to just bundle up the data and code into a single item and all the code that manipulates the class is part of the class &#8211; along with any hidden methods it might need that can be completely hidden from the outside world.<\/p>\n\n\n\n<p>If your still using VB style syntax and creating a lot of structures I&#8217;d suggest you give using classes a try.<\/p>\n\n\n\n<p>Once you get the hang of it you will appreciate how classes let you organize your code and data in a nice single item.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While structures &amp; classes may seem to be very similar in most respects there aren&#8217;t many cases when I would suggest you use a structure instead of a class. A class with public member properties isnt that different from a structure in terms of how your code uses it. The biggest difference is that for &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/12\/24\/structures-vs-classes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Structures vs Classes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}