{"id":464,"date":"2007-07-30T18:05:10","date_gmt":"2007-07-30T18:05:10","guid":{"rendered":"http:\/\/neosmart.net\/blog\/2007\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/"},"modified":"2018-02-08T00:52:33","modified_gmt":"2018-02-08T06:52:33","slug":"managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/","title":{"rendered":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET"},"content":{"rendered":"<p>One of the biggest advantages of managed frameworks\/platforms like Microsoft&#8217;s .NET Framework (and it&#8217;s Linux-counterpart, Mono), and Java is that you, as a developer, have a choice of not mucking around with pointers. To be totally honest, with Java you&#8217;re forced not to &#8211; in C#, it&#8217;s a choice you have to make.<\/p>\n<p>There&#8217;s plenty of good reasons for not using (<strong><em>unsafe!!<\/em><\/strong>) pointers, but that&#8217;s not the issue here. The question is, what if you want something to &#8220;point&#8221; to another object, and synchronize it&#8217;s value automatically, <em>without resorting to unsafe pointers?<\/em> There&#8217;s actually a quite simple answer using just a single line of C# code.<\/p>\n<p>Whereas in C++ you could write something like what appears below, in C# you&#8217;d have to declare it as unsafe, then jump through a hundred hoops to get it to properly point to a managed object:<\/p>\n<p><!--more--><\/p>\n<pre><code class=\"language-cpp\">CustomObject *myP;\r\nmyP = &amp;originalObject;\r\n*myP = Some_Value;\r\n<\/code><\/pre>\n<p>You might think it&#8217;s impossible to write such code in C# without worrying about unsafe expressions, nullifying the benefits of the GC, and a bunch of other things &#8211; but it&#8217;s not. In C#, code like this can do the trick of pointing one item to another &#8211; but with a catch:<\/p>\n<pre><code class=\"language-csharp\">MyDataSet.DvdsRow CurrentItem\r\n{\r\n\tget\r\n\t{\r\n\t\treturn (MyDataSet.DvdsRow)<sup id=\"rf1-464\"><a href=\"#fn1-464\" title=\"DataRowView)(myBindingSource.Current\" rel=\"footnote\">1<\/a><\/sup>.Row;\r\n\t}\r\n\tset\r\n\t{\r\n\t\tmyObject = value;\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>Looking at the code, the catch should be glaringly obvious: you must hard-code the object you&#8217;re pointing to! While the above code can be used to bypass the double-casting (a must when dealing with ADO.NET binding sources), you&#8217;ll need one property per binding source, which is a major constraint.<\/p>\n<p>You shouldn&#8217;t think of this as a pointer so much as it is an <em>alias<\/em>\u00a0&#8211; but to an item\/object instead of a namespace or class. It can help you bypass complex castings or long object names (that need to be kept in-sync with the original!), but isn&#8217;t really a pointer in that you can&#8217;t change the object it points to; a single-use pointer, if you will.<\/p>\n<p>Technically, there is a workaround. By using the immensely-difficult and often-misused on-the-fly compilation features of C#\/VB.NET you can actually create a class &#8220;managedPointer&#8221; that contains the above code in addition to a function to change the object it points to, but we&#8217;re not going to be getting into that here &#8211; nevertheless, it&#8217;s doable.<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-464\"><p>DataRowView)(myBindingSource.Current&nbsp;<a href=\"#rf1-464\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>One of the biggest advantages of managed frameworks\/platforms like Microsoft&#8217;s .NET Framework (and it&#8217;s Linux-counterpart, Mono), and Java is that you, as a developer, have a choice of not mucking around with pointers. To be totally honest, with Java you&#8217;re &hellip; <a href=\"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[354,325,360,499,11,326],"class_list":["post-464","post","type-post","status-publish","format-standard","hentry","category-software","tag-net-framework","tag-c","tag-coding","tag-pointers","tag-programming","tag-visual-basic"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-7u","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=464"}],"version-history":[{"count":3,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":4387,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/464\/revisions\/4387"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}