{"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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"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":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"One of the biggest advantages of managed frameworks\/platforms like Microsoft&#039;s .NET Framework (and it&#039;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&#039;re forced not to - in C#, it&#039;s a choice you have to make. There&#039;s plenty\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"NeoSmart Technologies\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#article\",\"name\":\"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET | The NeoSmart Files\",\"headline\":\"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET\",\"author\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/author\\\/neosmart\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#organization\"},\"datePublished\":\"2007-07-30T18:05:10-05:00\",\"dateModified\":\"2018-02-08T00:52:33-06:00\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#webpage\"},\"articleSection\":\"Software, .net framework, c#, coding, pointers, programming, visual basic\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/category\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/category\\\/software\\\/#listItem\",\"position\":2,\"name\":\"Software\",\"item\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/category\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#listItem\",\"name\":\"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#listItem\",\"position\":3,\"name\":\"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/category\\\/software\\\/#listItem\",\"name\":\"Software\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#organization\",\"name\":\"The NeoSmart Files\",\"description\":\"Recovery software and more\",\"url\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/author\\\/neosmart\\\/#author\",\"url\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/author\\\/neosmart\\\/\",\"name\":\"NeoSmart Technologies\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f542321efe8fccee6fb0b3322c2f647e2ab44a861d6b18ff16a79d032f8574cd?s=96&d=mm&r=pg\",\"width\":96,\"height\":96,\"caption\":\"NeoSmart Technologies\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#webpage\",\"url\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/\",\"name\":\"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET | The NeoSmart Files\",\"description\":\"One of the biggest advantages of managed frameworks\\\/platforms like Microsoft's .NET Framework (and it'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're forced not to - in C#, it's a choice you have to make. There's plenty\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/author\\\/neosmart\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/author\\\/neosmart\\\/#author\"},\"datePublished\":\"2007-07-30T18:05:10-05:00\",\"dateModified\":\"2018-02-08T00:52:33-06:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/\",\"name\":\"The NeoSmart Files\",\"description\":\"Recovery software and more\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/neosmart.net\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET | The NeoSmart Files","description":"One of the biggest advantages of managed frameworks\/platforms like Microsoft's .NET Framework (and it'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're forced not to - in C#, it's a choice you have to make. There's plenty","canonical_url":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#article","name":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET | The NeoSmart Files","headline":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET","author":{"@id":"https:\/\/neosmart.net\/blog\/author\/neosmart\/#author"},"publisher":{"@id":"https:\/\/neosmart.net\/blog\/#organization"},"datePublished":"2007-07-30T18:05:10-05:00","dateModified":"2018-02-08T00:52:33-06:00","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#webpage"},"isPartOf":{"@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#webpage"},"articleSection":"Software, .net framework, c#, coding, pointers, programming, visual basic"},{"@type":"BreadcrumbList","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/neosmart.net\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/category\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/category\/software\/#listItem","position":2,"name":"Software","item":"https:\/\/neosmart.net\/blog\/category\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#listItem","name":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET"},"previousItem":{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#listItem","position":3,"name":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET","previousItem":{"@type":"ListItem","@id":"https:\/\/neosmart.net\/blog\/category\/software\/#listItem","name":"Software"}}]},{"@type":"Organization","@id":"https:\/\/neosmart.net\/blog\/#organization","name":"The NeoSmart Files","description":"Recovery software and more","url":"https:\/\/neosmart.net\/blog\/"},{"@type":"Person","@id":"https:\/\/neosmart.net\/blog\/author\/neosmart\/#author","url":"https:\/\/neosmart.net\/blog\/author\/neosmart\/","name":"NeoSmart Technologies","image":{"@type":"ImageObject","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/f542321efe8fccee6fb0b3322c2f647e2ab44a861d6b18ff16a79d032f8574cd?s=96&d=mm&r=pg","width":96,"height":96,"caption":"NeoSmart Technologies"}},{"@type":"WebPage","@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#webpage","url":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/","name":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET | The NeoSmart Files","description":"One of the biggest advantages of managed frameworks\/platforms like Microsoft's .NET Framework (and it'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're forced not to - in C#, it's a choice you have to make. There's plenty","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/neosmart.net\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/#breadcrumblist"},"author":{"@id":"https:\/\/neosmart.net\/blog\/author\/neosmart\/#author"},"creator":{"@id":"https:\/\/neosmart.net\/blog\/author\/neosmart\/#author"},"datePublished":"2007-07-30T18:05:10-05:00","dateModified":"2018-02-08T00:52:33-06:00"},{"@type":"WebSite","@id":"https:\/\/neosmart.net\/blog\/#website","url":"https:\/\/neosmart.net\/blog\/","name":"The NeoSmart Files","description":"Recovery software and more","inLanguage":"en-US","publisher":{"@id":"https:\/\/neosmart.net\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"464","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2020-12-23 20:06:00","updated":"2026-01-10 21:50:09","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/neosmart.net\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/neosmart.net\/blog\/category\/software\/\" title=\"Software\">Software<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tManaged Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/neosmart.net\/blog\/"},{"label":"Software","link":"https:\/\/neosmart.net\/blog\/category\/software\/"},{"label":"Managed Pointers to Managed Objects (or Aliases for Objects) in C# and Visual Basic .NET","link":"https:\/\/neosmart.net\/blog\/managed-pointers-to-managed-objects-or-aliases-for-objects-in-c-and-visual-basic-net\/"}],"jetpack_shortlink":"https:\/\/wp.me\/p4xDa-7u","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_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}]}}