{"id":2846,"date":"2011-06-15T15:58:12","date_gmt":"2011-06-15T13:58:12","guid":{"rendered":"https:\/\/ingmarverheij.com\/?p=2846"},"modified":"2011-06-15T16:18:51","modified_gmt":"2011-06-15T14:18:51","slug":"compute-numeric-values-in-arrays-with-system-linq-namespace","status":"publish","type":"post","link":"https:\/\/ingmarverheij.com\/en\/compute-numeric-values-in-arrays-with-system-linq-namespace\/","title":{"rendered":"Compute numeric values in arrays with System.Linq namespace"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Mathematics\" border=\"0\" alt=\"Mathematics\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/Mathematics.jpg\" width=\"95\" height=\"76\" \/><\/p>\n<p>Microsoft .NET Framework can store values in an <strong>array<\/strong> which makes it easy to use in methods and properties. An array can contain any kind of object, but I most commonly use it to store <strong>numeric<\/strong> value. <\/p>\n<p>A greate property of numeric values is that you can <strong>compute<\/strong> them to a different value, and use that value to derive conclusions of any kind (I might sound like a madman , but figures somehow interest me).<\/p>\n<p>A simple example of computing numeric values is <strong>deriving<\/strong> the lowest number is a range of number or the average of all numbers. <strong>.NET<\/strong> has a method that can compute this easily for you, but it\u2019s a bit hidden)<\/p>\n<p>Let\u2019s take the following range of numbers: 0,1, 2, 3, 4. The lowest number in this range is 0 and the average is 2 (0+1+2+3+4 \/ 5). That\u2019s <strong>easy<\/strong>.<\/p>\n<p><!--more--><\/p>\n<h4>Example in VB.Net<\/h4>\n<p>In VB.Net the range of number can be placed in an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Array\" target=\"_blank\">array<\/a>, in this example I\u2019ll be using an array of <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.double(v=VS.90).aspx\" target=\"_blank\">Double<\/a> . I\u2019m looking for the lowest, highest and average of the range of numbers.<\/p>\n<p>So I created a new VB.Net project, which has target framework of <a href=\"https:\/\/msdn.microsoft.com\/en-us\/netframework\/aa731542.aspx\" target=\"_blank\">.NET Framework 2.0<\/a> by default, and wrote the following lines of code:<\/p>\n<pre lang=\"VBNET\">     Dim dblResults() As Double = {0, 1, 2, 3, 4}\r\n\r\n     Debug.Print(&quot;The lowest result is : &quot; & dblResults.Min.ToString)\r\n     Debug.Print(&quot;The highest result is : &quot; & dblResults.Max.ToString)\r\n     Debug.Print(&quot;The average result is : &quot; & dblResults.Average.ToString)<\/pre>\n<h6>\u2019\u2026\u2019 is not a member of&#160; \u2018System.Array\u2019<\/h6>\n<p>But, like I said in the introduction, these computations seem to be unavailable as a method. They would result in the error \u2018Min\u2019 is not a member of \u2018System.Array\u2019.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_14-59-34.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"\u2018Min\u2019 is not a member of \u2018System.Array\u2019\" border=\"0\" alt=\"\u2018Min\u2019 is not a member of \u2018System.Array\u2019\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_14-59-34_thumb.png\" width=\"599\" height=\"131\" \/><\/a><\/p>\n<h6>&#160;<\/h6>\n<h6>Requirements<\/h6>\n<p>The methods are published as part of the namespace \u2018<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.linq(v=VS.90).aspx\" target=\"_blank\">System.Linq<\/a>\u2019, but only in .NET Framework 3.5. <\/p>\n<h6>&#160;<\/h6>\n<h6>Upgrade target framework<\/h6>\n<p>So the first step is to upgrade the target framework from \u2018.NET Framework 2.0\u2019 to \u2018NET Framework 3.5\u2019. This can be achieved by opening the properties of the project and selecting the \u2018Advanced Compile Settings\u2019 in the Compile tab. <\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Compile &gt; Advanced Compile Options\" border=\"0\" alt=\"Compile &gt; Advanced Compile Options\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image_thumb6.png\" width=\"599\" height=\"417\" \/><\/a><\/p>\n<p>In the \u2018Advanced Compile Options\u2019 select the correct .NET Framework and press OK. Confirm in the next dialog that you wan\u2019t to upgrade the the target framework.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Advanced Compile Options\" border=\"0\" alt=\"Advanced Compile Options\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image_thumb7.png\" width=\"254\" height=\"214\" \/><\/a><\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/Target-Framework-Change_2011-06-15_15-00-27.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Target Framework Change\" border=\"0\" alt=\"Target Framework Change\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/Target-Framework-Change_2011-06-15_15-00-27_thumb.png\" width=\"254\" height=\"171\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h6>&#160;<\/h6>\n<h6>Reference namespace \u2018System.Linq\u2019<\/h6>\n<p>Next step is to reference the namespace \u2018<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.linq(v=VS.90).aspx\" target=\"_blank\">System.Linq<\/a>\u2019, which can be done by clicking \u2018Add\u2019 on the tab \u2018References\u2019 and selecting the \u2018<strong>System.Xml.Linq<\/strong>\u2019 reference.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Reference &gt; Add\" border=\"0\" alt=\"Reference &gt; Add\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image_thumb8.png\" width=\"254\" height=\"171\" \/><\/a><\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Add Reference\" border=\"0\" alt=\"Add Reference\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image_thumb9.png\" width=\"254\" height=\"211\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h6>Import System.Linq in code<\/h6>\n<p>Finally import the namespace in the code so the methods can be accessed in the code.<\/p>\n<pre lang=\"VBNET\">   Imports System.Linq<\/pre>\n<h6>&#160;<\/h6>\n<h6>Running demo<\/h6>\n<p>Now the code is accepted and the demo application works as expected.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_15-10-28.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Error List\" border=\"0\" alt=\"Error List\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_15-10-28_thumb.png\" width=\"599\" height=\"91\" \/><\/a><\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_15-11-15.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Immediate Window\" border=\"0\" alt=\"Immediate Window\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/WindowsApplication1-Microsoft-Visual-Studio_2011-06-15_15-11-15_thumb.png\" width=\"599\" height=\"91\" \/><\/a><\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"Source code in IDE\" border=\"0\" alt=\"Source code in IDE\" align=\"left\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2011\/06\/image_thumb10.png\" width=\"599\" height=\"194\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h6>Conclusion<\/h6>\n<p>Computing numeric values in arrays is available in .NET Framework but a bit hidden in the \u2018System.Linq\u2019 namespace. Although I can imagine why you want to use this methods when using Linq, I can\u2019t imagin why you Microsoft didn\u2019t import them in the base of .NET Framework.<\/p>\n<p>Computing numeric values, as showed, are simple functions that can be created easily. But i\u2019d rather use functions supplied by the .NET Framework and focus on my main objective. But referencing a namespace, like System.Linq, does have an impact on the baseline of the application (and therefore performance footprint).<\/p>\n<p>&#160;<\/p>\n<p>Ingmar Verheij<\/p>","protected":false},"excerpt":{"rendered":"<p>Microsoft .NET Framework can store values in an array which makes it easy to use in methods and properties. An array can contain any kind of object, but I most commonly use it to store numeric value. A greate property of numeric values is that you can compute them to a different value, and use [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[312],"tags":[343,237,345,344,342],"class_list":["post-2846","post","type-post","status-publish","format-standard","hentry","category-vb-net-scripting","tag-linq","tag-net","tag-compute-numeric-values","tag-hidden","tag-syste"],"_links":{"self":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/2846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/comments?post=2846"}],"version-history":[{"count":5,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"predecessor-version":[{"id":4674,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/2846\/revisions\/4674"}],"wp:attachment":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}