{"id":381,"date":"2014-03-17T13:42:34","date_gmt":"2014-03-17T12:42:34","guid":{"rendered":"https:\/\/www.modelical.com\/en\/?p=381"},"modified":"2022-04-28T17:09:13","modified_gmt":"2022-04-28T15:09:13","slug":"grasshopper-scripting-102-variables-and-assignment","status":"publish","type":"post","link":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/","title":{"rendered":"Grasshopper Scripting 102"},"content":{"rendered":"<h2>Variables and Assignment<\/h2>\n<p>After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables.<\/p>\n<h3>What Is A Variable?<\/h3>\n<p>Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or process it. Variables would be equivalent to parameters in GH and they behave in a very similar way.<\/p>\n<p>Variables in C# can belong to two families: value-type and reference-type. Value-type variables are used to represent basic information such as integer numbers, decimal numbers, strings of characters and boolean states. Reference-type variables are way more flexible and can be used to hold any type of data. In GH we will use \u00a0value-type variables to basically store numbers and booleas and reference-type for surfaces, curves and points.<\/p>\n<h3>Declare and Assign Variables:<\/h3>\n<p>When we need a variable, we have to declare it so the computer can allocate some space for it in memory. We do so with the following statement:<\/p>\n<p><strong>DataType<\/strong> VariableName;<\/p>\n<li>The DataType tells the computer how much space and of what kind will be necessary to hold the variable.<\/li>\n<li>VariableName gives the variable a name that can be called at any time later.<\/li>\n<li>The semicolon marks the end of the statement.<\/li>\n<p>Once your variable is declared you have to fill it with a value. This value can come from another variable or can be created on the fly by calling a function or a number \/ string \/ boolean.<\/p>\n<p>Here are some examples of <strong>variable declaration and assignment:<\/strong><\/p>\n<pre class=\"lang:c# decode:true \">\/\/declare an integer variable and name it myInteger\r\nint myInteger;\r\n\/\/assign 85 to the variable\r\nmyInteger = 85; \r\n\r\n\/\/You can also do the same in just one line.\r\nint myOtherInteger = 74;\r\n\r\n\/\/Declare a boolean\r\nbool myBoolean = true;\r\nbool myOtherBoolean = false;\r\nbool myThirdBoolea = myBolean;\r\n\r\n\/\/Declare some doubles\r\ndouble myDouble = 12.33;\r\ndouble myOtherDouble = 1.0;\r\n\r\n\/\/Strings come within quotes\r\nstring myString = \"Hello World!\";<\/pre>\n<p>You should keep this in mind when naming variables:<\/p>\n<li>C# is Case-Sensitive. This means you have to be careful when naming and calling your variables. <em>myString<\/em> will not be the same as <em>Mystring.<\/em><\/li>\n<li>Variables have to start with underscore (&#8220;_&#8221;) or alphabetic character. Variable names can contain numbers but not as the first character.<\/li>\n<li>It is a good practice to use Camel Case:\u00a0The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: myName, myNameAndSurname.<\/li>\n<h3>Variable Types<\/h3>\n<li><span style=\"color: #0000ff;\">bool<\/span> Used to declare a boolean.<\/li>\n<li><span style=\"color: #0000ff;\">int<\/span> Used to declare a signed integer number.<\/li>\n<li><span style=\"color: #0000ff;\">double<\/span> Used to declare a number with double decimal precision.<\/li>\n<li><span style=\"color: #0000ff;\">string<\/span> Used to declare a chain of characters.<\/li>\n<p>C# is a strongly-typed language, which means you&#8217;ll have to supply a type to every variable.<\/p>\n<h3>Outputting Values In Your Component<\/h3>\n<p>Let&#8217;s see a complete example of variable declaration, assignment and output.<\/p>\n<ol>\n<li>Create a new Custom C# Component by double-clicking on the canvas and look for C#&#8230;<\/li>\n<li>Remove every input parameter by clicking the (-) sign after zooming in the component.<\/li>\n<li>Double-click on the center of the component.<\/li>\n<li>This should be your code:\n<pre class=\"lang:c# decode:true \">  private void RunScript(ref object A)\r\n  {\r\n    int myAge = 30;\r\n    string myName = \"Roberto\";\r\n    string mySurname = \"Molinos\";\r\n    string mySentence = \"Hello!, my name is \" + myName + \" \" + mySurname + \", and my age is \" + myAge.ToString();\r\n    A = mySentence ;\r\n  }<\/pre>\n<p>Keep in mind that you should only edit the region inside the curly brackets, as the rest is controlled by GH itself.<\/li>\n<\/ol>\n<p>In the example above whe are doing the following:<\/p>\n<ol>\n<li>Line 1,2 and 8 are controlled by GH so we cannot change anything from them.<\/li>\n<li>Line 3 declares an integer to store my age and assigns 30.<\/li>\n<li>Line 4 and 5 declare two strings to store my name and surname and those are assigned.<\/li>\n<li>Line 6 declares a string and directly assigns the concatenation of several other strings. Those not being a variable are created by typing within quotes and the parts are joined by using the (+) operator. myAge is converted to a string using the .ToString() function.<\/li>\n<li>Finally, in line 7, the output value A gets a reference to mySentence.<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"800\" height=\"251\" class=\"alignnone size-full wp-image-3930\" alt=\"102-01\" src=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/102-01.jpg\" srcset=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/102-01.jpg 800w, https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/102-01-480x151.jpg 480w, https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/102-01-768x241.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":27195,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[423,418],"class_list":["post-381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guidelines","tag-technology-c","tag-technology-grasshopper"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grasshopper Scripting 102 - Modelical<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grasshopper Scripting 102 - Modelical\" \/>\n<meta property=\"og:description\" content=\"Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/\" \/>\n<meta property=\"og:site_name\" content=\"Modelical\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Modelical\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-03-17T12:42:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-28T15:09:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_37_GH_6.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"375\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Roberto Molinos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@modelical\" \/>\n<meta name=\"twitter:site\" content=\"@modelical\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roberto Molinos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/\"},\"author\":{\"name\":\"Roberto Molinos\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"headline\":\"Grasshopper Scripting 102\",\"datePublished\":\"2014-03-17T12:42:34+00:00\",\"dateModified\":\"2022-04-28T15:09:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/\"},\"wordCount\":580,\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2014\\\/03\\\/Posts_37_GH_6-400x250-1.jpg\",\"keywords\":[\"Tec C#\",\"Tec Grasshopper\"],\"articleSection\":[\"Guidelines\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/\",\"name\":\"Grasshopper Scripting 102 - Modelical\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2014\\\/03\\\/Posts_37_GH_6-400x250-1.jpg\",\"datePublished\":\"2014-03-17T12:42:34+00:00\",\"dateModified\":\"2022-04-28T15:09:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-102-variables-and-assignment\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2014\\\/03\\\/Posts_37_GH_6-400x250-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2014\\\/03\\\/Posts_37_GH_6-400x250-1.jpg\",\"width\":400,\"height\":250},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/\",\"name\":\"Modelical\",\"description\":\"We build information\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\",\"name\":\"Roberto Molinos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"caption\":\"Roberto Molinos\"},\"sameAs\":[\"https:\\\/\\\/www.modelical.com\"],\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/author\\\/roberto\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grasshopper Scripting 102 - Modelical","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/","og_locale":"en_US","og_type":"article","og_title":"Grasshopper Scripting 102 - Modelical","og_description":"Variables and Assignment After the introduction in our latest post now is time to get our feet wet with a fundamental concept: variables. What Is A Variable? Variables are portions of memory that represent a certain entity and that have a name. We use variables to hold information in order to read, write, modify or [&hellip;]","og_url":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/","og_site_name":"Modelical","article_publisher":"https:\/\/www.facebook.com\/Modelical\/","article_published_time":"2014-03-17T12:42:34+00:00","article_modified_time":"2022-04-28T15:09:13+00:00","og_image":[{"width":600,"height":375,"url":"https:\/\/www.modelical.com\/wp-content\/uploads\/Posts_37_GH_6.jpg","type":"image\/jpeg"}],"author":"Roberto Molinos","twitter_card":"summary_large_image","twitter_creator":"@modelical","twitter_site":"@modelical","twitter_misc":{"Written by":"Roberto Molinos","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/#article","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/"},"author":{"name":"Roberto Molinos","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"headline":"Grasshopper Scripting 102","datePublished":"2014-03-17T12:42:34+00:00","dateModified":"2022-04-28T15:09:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/"},"wordCount":580,"image":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/Posts_37_GH_6-400x250-1.jpg","keywords":["Tec C#","Tec Grasshopper"],"articleSection":["Guidelines"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/","url":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/","name":"Grasshopper Scripting 102 - Modelical","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/#primaryimage"},"image":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/Posts_37_GH_6-400x250-1.jpg","datePublished":"2014-03-17T12:42:34+00:00","dateModified":"2022-04-28T15:09:13+00:00","author":{"@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-102-variables-and-assignment\/#primaryimage","url":"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/Posts_37_GH_6-400x250-1.jpg","contentUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/Posts_37_GH_6-400x250-1.jpg","width":400,"height":250},{"@type":"WebSite","@id":"https:\/\/www.modelical.com\/en\/#website","url":"https:\/\/www.modelical.com\/en\/","name":"Modelical","description":"We build information","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.modelical.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5","name":"Roberto Molinos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","url":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","caption":"Roberto Molinos"},"sameAs":["https:\/\/www.modelical.com"],"url":"https:\/\/www.modelical.com\/en\/author\/roberto\/"}]}},"_links":{"self":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":0,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media\/27195"}],"wp:attachment":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}