{"id":1550,"date":"2023-02-26T21:11:50","date_gmt":"2023-02-27T04:11:50","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1550"},"modified":"2023-02-26T21:11:50","modified_gmt":"2023-02-27T04:11:50","slug":"byref-and-byval-part-ii","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2023\/02\/26\/byref-and-byval-part-ii\/","title":{"rendered":"Byref and ByVal part II"},"content":{"rendered":"\n<p>Long ago I wrote about <a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/10\/21\/byval-and-byref\/\">byref &amp; byval<\/a><br>Apparently this wasnt widely read not even by some long standing members of the community as I still see completely wrong explanations of bevel &amp; byref persist.<\/p>\n\n\n\n<p>First you have to understand that there are two types of VARIABLES. <br>VALUE types and REFERENCE types.<br>With a VALUE type the compiler sees there is space in memory set aside when your program runs to hold a value &#8211; a number a floating point value etc. The thing IN that spot IS the actual data.<br>With a REFERENCE type whats in that spot isnt the data directly &#8211; it s the address of the data.<\/p>\n\n\n\n<p>Normally when you pass a value you pass it BYVAL <br>This is the default<\/p>\n\n\n\n<p>So what exactly does this mean ?<\/p>\n\n\n\n<p>I literally means that the value that is held by whatever type of variable is passed in can&#8217;t be altered. <br>If you pass in an integer, IN the method you can change it however you want, but those changes WILL NOT be preserved in the calling code.<\/p>\n\n\n\n<p>Something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Opening\n  dim I as integer = 100\n\n  system.debuglog str(i) \/\/ prints 100\n\n  foo(i)\n\n  system.debuglog str(i) \/\/ prints 100\nend sub\n\nsub foo ( I as integer )\n  I = 500\n  system.debuglog \"in foo \" + str(i) \/\/ prints in foo 500\nend sub\n<\/code><\/pre>\n\n\n\n<p>Would print 100 and 500 as indicated. The value would not be preserved from the call to foo. So when we return the next time I is printed in the caller it prints 100.<\/p>\n\n\n\n<p>This is the easy case for a VALUE type.<\/p>\n\n\n\n<p>If we switch to a REFERENCE type its EXACTLY THE SAME ! The trick is &#8220;the value&#8221; that is preserved is the pointer, or reference, that is preserved. NOT THE data referred to. Let me illustrate<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Opening\n  Dim d As New date\n  System.debuglog d.SQLDateTime\n  foo(d)\n  System.debuglog d.SQLDateTime\n\nEnd Sub\n\nsub foo ( d as date )\n  \n  d = New date(d.year - 2, d.month - 4, d.day - 6, _\n    d.hour - 8, d.minute - 10)\n\n  System.debuglog \"in foo = \" + d.SQLDateTime\nend sub\n<\/code><\/pre>\n\n\n\n<p>Note that if you run this you will see that d in the main calling routine before &amp; after the call prints the SAME date. This despite the fact that in the called routine, foo, we altered the date that d referred to.<br>What this shows is that what is being passed by VALUE is the REFERENCE (pointer or address)<\/p>\n\n\n\n<p>And again IN the routine we called we can do whatever we want to change what the REFERENCE is by creating a new one and that change wont stick when we leave.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>there IS a thing that many people referen to as &#8220;byref&#8221; that ISNT <br>SEE THE END OF THIS ARTICLE<\/li>\n<\/ul>\n\n\n\n<p>No wit we switch to using BYREF to make it so &#8220;changes to the value held&#8221; are preserved we get<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Opening\n  dim I as integer = 100\n\n  system.debuglog str(i) \/\/ prints 100\n\n  foo(i)\n\n  system.debuglog str(i) \/\/ prints 500 !!!!!!!!!!!!\nend sub\n\nsub foo ( byref I as integer )\n  I = 500\n  system.debuglog \"in foo \" + str(i) \/\/ prints in foo 500\nend sub<\/code><\/pre>\n\n\n\n<p>The change to the value held IS preserved ! <br>BYREF allows us to alter what the variable held<br><br>And for BYREF with a reference type its much the same<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Opening\n  Dim d As New date\n  System.debuglog d.SQLDateTime\n  foo(d)\n  System.debuglog d.SQLDateTime \/\/ prints THE SAME as from foo !!!!\n\nEnd Sub\n\nsub foo ( byref d as date )\n  \n  d = New date(d.year - 2, d.month - 4, d.day - 6, _\n    d.hour - 8, d.minute - 10)\n\n  System.debuglog \"in foo = \" + d.SQLDateTime\nend sub<\/code><\/pre>\n\n\n\n<p>BYREF lets you change what the VALUE of the passed in variable is.<br>But, it depends on what kind of value is passed in to see what the effects are<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>SOME people incorrectly refer to the following as &#8220;byref&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Opening\n  Dim d As New date\n  System.debuglog d.SQLDateTime\n  foo(d)\n  System.debuglog d.SQLDateTime \/\/ prints THE SAME as from foo !!!!\n\nEnd Sub\n\nsub foo ( d as date )\n  \n  d.hour = d.hour - 12\n\n  System.debuglog \"in foo = \" + d.SQLDateTime\nend sub<\/code><\/pre>\n\n\n\n<p>Bu this is NOT &#8220;byref&#8221;<br>We haven&#8217;t change the REFERENCE ( the actual instance) being referred to<\/p>\n\n\n\n<p>We HAVE altered one of the properties of the item passed in &#8211; that happens to be a reference type<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>I hope this clears up the confusion<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long ago I wrote about byref &amp; byvalApparently this wasnt widely read not even by some long standing members of the community as I still see completely wrong explanations of bevel &amp; byref persist. First you have to understand that there are two types of VARIABLES. VALUE types and REFERENCE types.With a VALUE type the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2023\/02\/26\/byref-and-byval-part-ii\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Byref and ByVal part II&#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-1550","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\/1550","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=1550"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1550\/revisions"}],"predecessor-version":[{"id":1551,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1550\/revisions\/1551"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}