Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] State Variables in Solidity

#1
State Variables in Solidity

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;765838&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-52-1024x576.png" alt="" class="wp-image-766451" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-52-1024x576.png 1024w, https://blog.finxter.com/wp-content/uplo...00x169.png 300w, https://blog.finxter.com/wp-content/uplo...68x432.png 768w, https://blog.finxter.com/wp-content/uplo...age-52.png 1378w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>In this article, I’ll be going over the different types of state variables in <a href="https://blog.finxter.com/solidity/" data-type="post" data-id="37379" target="_blank" rel="noreferrer noopener">Solidity</a> and how to use them. State variables are one of the most important parts of any <a href="https://blog.finxter.com/top-solidity-smart-contract-examples-for-learning/" data-type="post" data-id="663675" target="_blank" rel="noreferrer noopener">smart contract</a>, as they allow us to store data that can change over time.</p>
<p>This article is mainly focused on <strong>value types of state variables</strong>, but I’ll be continuing with another two articles on reference and complex types as well as data location. Let’s dive in!</p>
<h2>Basics – A Quick Review</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="626" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-53-1024x626.png" alt="" class="wp-image-766459" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-53-1024x626.png 1024w, https://blog.finxter.com/wp-content/uplo...00x184.png 300w, https://blog.finxter.com/wp-content/uplo...68x470.png 768w, https://blog.finxter.com/wp-content/uplo...age-53.png 1378w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Smart contracts are pieces of code that are deployed in blockchain nodes. They are immutable, meaning they cannot be changed once they have been deployed. This can make it necessary to redeploy the code as a new smart contract or redirect calls from an old contract to new ones.</p>
<p>A smart contract is initiated by a message embedded in a transaction. <a href="https://blog.finxter.com/smart-contracts-and-evm/" data-type="post" data-id="92507" target="_blank" rel="noreferrer noopener">Ethereum</a> enables these transactions, which may carry out more sophisticated operations like conditional transfers.</p>
<p>A conditional transfer, such as one that depends on the age of the buyer or the value of their bid, could be required. </p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Example</strong>: If the buyer is over 21 and their bid is greater than the minimum bid, then accept the bid. Otherwise reject it.</p>
<p>Smart contracts are executed when predetermined conditions are met to automate the execution of an agreement so that all parties can be immediately certain of the outcome without the need for an intermediary.</p>
<h2>How Do You Write a Smart Contract?</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="768" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-54-1024x768.png" alt="" class="wp-image-766464" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-54-1024x768.png 1024w, https://blog.finxter.com/wp-content/uplo...00x225.png 300w, https://blog.finxter.com/wp-content/uplo...68x576.png 768w, https://blog.finxter.com/wp-content/uplo...age-54.png 1251w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Smart contracts are similar to a <strong><em>class definition</em></strong> in an<a href="https://blog.finxter.com/introduction-to-python-classes/" data-type="post" data-id="30596" target="_blank" rel="noreferrer noopener"> object-oriented programming language</a>. </p>
<p>The smart contracts are: </p>
<ul>
<li>data (its state); </li>
<li>a collection of code (its functions or methods with modifiers public or private with getter and set functions).</li>
</ul>
<h3>What is the structure of a smart contract?</h3>
<p>As we have seen in <a href="https://blog.finxter.com/solidity-crash-course/" data-type="post" data-id="445146" target="_blank" rel="noreferrer noopener">other articles</a> in Finxter, the structure of a smart contract is as follows:</p>
<ul>
<li>Contract in the Ethereum blockchain has <code><a href="https://blog.finxter.com/solidity-layout-pragmas-importing-and-comments/" data-type="post" data-id="716191" target="_blank" rel="noreferrer noopener">pragma</a></code> directive;</li>
<li>Name of the contract;</li>
<li>Data or the state variable that define the state of the contract;</li>
<li>Collection of functions to carry out the intent of a smart contract;</li>
</ul>
<p>Note that the identifiers representing these elements are restricted to the ASCII character set. Make sure you select meaningful identifiers and follow camel case convention in naming them.</p>
<h2>Variable Declaration</h2>
<p>To declare a variable in Solidity, you must first specify its data type. This is followed by an access modifier and the variable name.</p>
<h3>Structure</h3>
<pre class="wp-block-preformatted"><code>&lt;type> &lt;access modifier> &lt;variable name> ; </code></pre>
<p>Example:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="544" height="161" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-48.png" alt="" class="wp-image-765861" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-48.png 544w, https://blog.finxter.com/wp-content/uplo...300x89.png 300w" sizes="(max-width: 544px) 100vw, 544px" /></figure>
</div>
<h2>What Categories of Variables Exist in Solidity?</h2>
<p>Solidity supports three categories of variables:</p>
<h3>(1) State Variables</h3>
<p class="has-global-color-8-background-color has-background">State variables are variables whose values are permanently stored in a contract storage. </p>
<p><strong>What does this mean?</strong></p>
<p>State variables are an essential part of any contract. They are variables whose values are permanently stored in the contract storage. They can be thought of as a single slot in a database that you can query and alter by calling functions of the code that manages the database. The <code>set</code> and <code>get</code> functions can be used to modify and retrieve the value of the variables.</p>
<p>In other words, the data (state variables) are stored contiguously item after item starting with the first state variable, stored in slot 0. For each variable, the size in bytes is determined according to its type. Several contiguous items  that require less than 32 bytes are packed into a single storage slot if possible.</p>
<p>To make it easier, if you use other languages and want to store user information for a long time, you would connect your application to a database server and then store the information in the database. In Solidity, however, you do not need to connect, you can simply store the data permanently using state variables.</p>
<h3>(2) Local Variables</h3>
<p class="has-global-color-8-background-color has-background">Local variables are variables whose values exist until the function is executed; the context of local variables is within the function and cannot be accessed outside. </p>
<p>Typically, these variables are used to hold temporary values for processing or computing something. In the following example, “temp” is a local variable that cannot be used outside the “set” function.</p>
<h3>(3) Global Variables</h3>
<p class="has-global-color-8-background-color has-background">Global variables are variables whose values exist in the global namespace to obtain information about the blockchain.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="932" height="294" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-49.png" alt="" class="wp-image-765870" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-49.png 932w, https://blog.finxter.com/wp-content/uplo...300x95.png 300w, https://blog.finxter.com/wp-content/uplo...68x242.png 768w" sizes="(max-width: 932px) 100vw, 932px" /></figure>
</div>
<p>Each function has its own scope, but state variables should always be defined outside the scope, like the attributes of a class. </p>
<p>They are permanently stored in the <a href="https://blog.finxter.com/blockchain-analytics-the-basics/" data-type="post" data-id="605837" target="_blank" rel="noreferrer noopener">Ethereum blockchain</a>, more precisely in the storage <a href="https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/" data-type="URL" data-id="https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/" target="_blank" rel="noreferrer noopener">Merkle-Patricia tree</a>, which is part of the information that forms the state of an account (that’s why we call them state variables).</p>
<h2>What Types of Valid State Variables Exist?</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: Solidity is a statically typed language, meaning each variable’s type must be specified at the time of its declaration. </p>
<p>“Undefined” or “null” values do not exist in Solidity, but newly declared variables always have a default value depending on their type, typically called “zero- state”. </p>
<p>For example, the default value for <code>bool</code> is <code>false</code>.</p>
<p>As in other languages (not <a href="https://blog.finxter.com/python-developer-income-and-opportunity/" data-type="post" data-id="189354" target="_blank" rel="noreferrer noopener">Python</a> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f600.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> ), there are two types in Solidity: value types and reference types. </p>
<ul>
<li>The <strong>value type</strong> is a variable that stores its value or its own data directly; it is a value type. If the variable contains a location of the data – it is a reference type. </li>
<li>The <strong>reference types</strong> are discussed in a separate article.</li>
</ul>
<p>For example, consider the integer variable <code>int i = 100;</code></p>
<p>The system stores <code>100</code> in the memory location allocated for the variable i. The following image shows how 100 is stored in a hypothetical location in memory (<code>0x239110</code>) for “i”:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="482" height="164" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-50.png" alt="" class="wp-image-765888" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-50.png 482w, https://blog.finxter.com/wp-content/uplo...00x102.png 300w" sizes="(max-width: 482px) 100vw, 482px" /></figure>
</div>
<h2>What are the Modifiers for the State Variables?</h2>
<h3>Visibility – access modifiers</h3>
<p>Access modifiers are the keywords used to specify the declared accessibility of a state variable and functions.</p>
<p>Variables in Solidity have three types of visibility: <strong>public</strong>, <strong>private</strong>, and  <strong>internal</strong>. If visibility is not explicitly declared, the compiler considers it internal.</p>
<p>For variables of type public, the compiler automatically creates a method to retrieve them through a call. This does not apply to private or internal variables.</p>
<p>Example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">uint256 public a; is actually exactly the same thing as : uint256 private a;
function a() public view returns(uint256) {
return a;
}
</pre>
<p>When you create a public variable, it is stored the same way as a private variable, but the compiler automatically creates a getter function for it.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The difference between private and internal variables is that internal variables are inherited by child contracts, while private variables are not.</p>
<p>To learn more about private variables:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract Addition { uint x; //internal variable uint public y; // contract Child is Addition{ //no need to define x since the child contract inherits the variable //uintx function setX(uint _x) public { x =_x; function getX() public view returns (uint) { return x; }
}
</pre>
<p>Note that the data location (memory, storage, and call data) must be specified for variables of reference type. This is necessary when function arguments are involved. We will cover this in an article on data location.</p>
<h3>Other keywords</h3>
<p>The following keywords can be used for state variables to restrict changes to their state.</p>
<h4>Constant (replaced by “view” and “pure” in functions)</h4>
<p>Constant disallows assignment (except at initialization), i.e. they cannot be changed after initialization, but must be initialized at the time of their declaration.</p>
<p>Example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">uint private constant t = 40;</pre>
<p>The variable <code>t</code> has been declared once and therefore cannot be changed.</p>
<p>It is interesting to note that the declaration of a constant variable without initialization is forbidden and the compiler displays an error, e.g.:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Contract Addition { uint private x; uint public y; uint private constant z; //gives an error because constant variables must be initialized when declared.
</pre>
<h4>Immutable </h4>
<p>These variables can be declared without being initialized, but the assignment, which is only one, must be done in the constructor. After that, the variable is constant thereafter.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> uint private immutable w; //now we declare a constructor for the contract, using the function constructor constructor() { w = 20; //initiate variable }
</pre>
<h4>Override&nbsp;</h4>
<p>This keyword states that the public state variables change the behavior of a function.</p>
<h2>Value Types</h2>
<p class="has-global-color-8-background-color has-background">These variables are passed by value. That is, they are copied when they are used either in an assignment or in a function argument. </p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If this sentence is not clear, you can check <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/6158907/what-does-python-treat-as-reference-types" data-type="URL" data-id="https://stackoverflow.com/questions/6158907/what-does-python-treat-as-reference-types" target="_blank">here</a>.</p>
<p>Here we will see the basic value types.</p>
<p>Value types are booleans, integers, addresses, enums, and bytes.</p>
<h3>Booleans</h3>
<p>Boolean values can be true or false</p>
<p>An example of a boolean type:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract ExampleBool { // example of a bool value type in solidity bool public IsVerified = false; bool public IsSent = true; }
</pre>
<h3>Integers</h3>
<p>There are <code>int/uint</code> (signed and unsigned integers) types of various sizes. It stores the values in a range of 8, <code>int16</code>, …up to <code>int256</code>. Int256 is the same as <code>int</code>, same for <code>uint8</code>, and <code>uint256</code>. </p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: <code>uint256</code> is the same as <code>uint</code>.</p>
<p>The type <code>uint</code> stands for positive integers. The type int stands for both positive and negative integers.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/solidity-boolean-and-integer-types-a-helpful-guide-with-video/" data-type="post" data-id="728241" target="_blank" rel="noreferrer noopener">Solidity Data Types – Integer and Boolean</a></p>
<p>The type <code>uint8</code> (has 8 bits, which corresponds to 1 byte. This means that it accepts numbers between 0 and 255; bit is a binary digit. So one byte can hold 2 (binary) ^ 8 numbers from 0 to 2^8-1 = 255. This is the same as asking why a three-digit decimal number can represent the values 0 to 999.</p>
<p>The type <code>uint256</code> accepts numbers between 0 and 2^256.</p>
<p>If we try to assign the value 256 to a variable of type <code>uint8</code>, the compiler will print an error.</p>
<p>The best practice for integers is to specify the value of the bits at the declaration stage to use as little space as possible and reduce the cost of storage. So use <code>uint8</code> or <code>uint16</code> instead of always using <code>int</code> (<code>uint256</code>).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract SimpleContract{ uint32 public uidata = 1234567; //un-signed integer int32 public idata = -1234567; //signed integer }
</pre>
<h3>Fixed Point Numbers&nbsp;&nbsp;&nbsp;</h3>
<p>According to the Solidity documents, <a rel="noreferrer noopener" href="https://blog.finxter.com/data-types-in-solidity-smart-contract-part-20-fixed-point-numbers-address-and-address-type-members/" data-type="post" data-id="738934" target="_blank">fixed-point numbers</a> are the type for floating-point numbers. However, the official document states that <strong><em>“Fixed point numbers are not yet fully supported by Solidity”</em></strong>. They can be declared, but cannot be added to or derived from.</p>
<p>However, you can use floating point numbers for calculations, but the value resulting from the calculation should be an integer.</p>
<p>Here is an example,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract additionContract{ uint8 result; function Addition(uint) public { result = 2/3; //error result = 3.5 + 1.5; // final result will be an integer } }
</pre>
<p>Let’s do a subtle change,</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="853" height="221" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-51.png" alt="" class="wp-image-766388" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-51.png 853w, https://blog.finxter.com/wp-content/uplo...300x78.png 300w, https://blog.finxter.com/wp-content/uplo...68x199.png 768w" sizes="(max-width: 853px) 100vw, 853px" /></figure>
</div>
<h3>Address</h3>
<p>The <code><a href="https://blog.finxter.com/data-types-in-solidity-smart-contract-part-20-fixed-point-numbers-address-and-address-type-members/" data-type="post" data-id="738934">address</a></code> data type is very specific to Solidity. </p>
<p>On the <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-smart-contracts-and-solidity-part-3-blockchain-basics/" data-type="post" data-id="537705" target="_blank">Ethereum blockchain</a>, every account and smart contract has an address that is used to send and receive <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-ethereums-gas-in-solidity-development/" data-type="post" data-id="37644" target="_blank">Ether</a> from one account to another. </p>
<p><strong>This is your public identity on the blockchain. </strong></p>
<p>Also, when you <a href="https://blog.finxter.com/how-to-deploy-smart-contract-to-polygon-in-brownie/" data-type="post" data-id="141343" target="_blank" rel="noreferrer noopener">deploy</a> a smart contract on the blockchain, that contract is assigned an address that you can use to identify and call the smart contract.</p>
<p>There are two variants for the address type, which are identical:</p>
<ul>
<li><code>address</code> – stores a 20-byte value (the size of an Ethereum address or account). The default value for the address is 0x…followed by 40 0’s, or 20 bytes of 0’s.</li>
<li><code>address payable</code> – like address, but transfer and send with the additional members.</li>
</ul>
<p>The idea behind this distinction is that the address payable is an address you can send Ether to, while you should not send Ether to a plain address, as it could be a smart contract that was not built to accept Ether.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> contract ExampleAddress { address public myAddress = 0xc895t6ea1bc39595cf849612ffta7427f5792987</pre>
<h3>Enums&nbsp;</h3>
<p>What stands for enumerable is a user-defined data type that restricts the variable to have only one of the predefined values. </p>
<p>These values listed in the enumerated list are called <em><strong>enums</strong></em>, and internally these enums are treated like numbers (<a href="https://www.educative.io/answers/what-are-enums-in-solidity" data-type="URL" data-id="https://www.educative.io/answers/what-are-enums-in-solidity" target="_blank" rel="noreferrer noopener">resource</a>). This makes the contract more readable and maintainable.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">contract SampleEnum{ //Creating an enumerator enum animal_classes { Mammals, Fish, Amphibians, Reptiles, Birds } function getFirstEnum() public pure returns(animal_classes){ return animal_classes.Mammals; } // result: // 0: uint8: 0 }
</pre>
<p>With enums, we can also set a default value;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">animal_classes constant defaultValue = animal_classes.Reptiles; function getDefaultValue() public pure returns(animal_classes) { return defaultValue; } } //result // result: // 0: uint8: 2 </pre>
<h3>Bytes and Strings</h3>
<p>A byte refers to signed 8-bit integers. Everything in memory is stored in bits with binary values 0 and 1.</p>
<p>Solidity supports string literals that use both double quotes (<code>"</code>) and single quotes (<code>'</code>). It provides String as a data type to declare a variable of type String.</p>
<p>Strings are unique in Solidity compared to <a href="https://blog.finxter.com/python-crash-course/" data-type="post" data-id="3951" target="_blank" rel="noreferrer noopener">Python</a> or other programming languages in that there are no functions for manipulating strings, except that you can concatenate strings. The reason for this is that storing strings in a blockchain is very expensive.</p>
<p>Bytes and strings are easy to handle in Solidity because Solidity treats them similarly to an array. The two are very similar. (See Arrays in the Reference Type article).</p>
<h2>Conclusion</h2>
<p>Smart contracts reside at a specific address in the Ethereum blockchain. In this article, we learned about state variables in Solidity. </p>
<p>We looked at state, local variables, and the different types with a value type. </p>
<p>We tried to understand Boolean, Integers, Enums, Addresses, Bytes, and Strings (although the last ones are treated with more depth in reference types)</p>
<h2>Bibliography</h2>
<ul>
<li><a href="https://docs.soliditylang.org/en/develop/introduction-to-smart-contracts.html" target="_blank" rel="noreferrer noopener">https://docs.soliditylang.org/en/develop/introduction-to-smart-contracts.html</a></li>
<li><a href="https://www.tutorialspoint.com/solidity/solidity_variables.htm" target="_blank" rel="noreferrer noopener">https://www.tutorialspoint.com/solidity/solidity_variables.htm</a></li>
<li><a href="https://medium.com/coinmonks/learn-solidity-lesson-5-uint-types-and-visibility-fbebb221c311" target="_blank" rel="noreferrer noopener">https://medium.com/coinmonks/learn-solidity-lesson-5-uint-types-and-visibility-fbebb221c311</a></li>
<li><a href="https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type" target="_blank" rel="noreferrer noopener">https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type</a></li>
<li><a href="https://www.geeksforgeeks.org/solidity-arrays/" target="_blank" rel="noreferrer noopener">https://www.geeksforgeeks.org/solidity-arrays/</a></li>
<li><a href="https://jeancvllr.medium.com/solidity-tutorial-all-about-bytes-9d88fdb22676" target="_blank" rel="noreferrer noopener">https://jeancvllr.medium.com/solidity-tutorial-all-about-bytes-9d88fdb22676</a></li>
<li><a href="https://www.thecrosschain.co/post/bytes-in-solidity" target="_blank" rel="noreferrer noopener">https://www.thecrosschain.co/post/bytes-in-solidity</a></li>
<li><a href="https://www.c-sharpcorner.com/article/reference-types-in-solidity/" target="_blank" rel="noreferrer noopener">https://www.c-sharpcorner.com/article/reference-types-in-solidity/</a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
</div>


https://www.sickgaming.net/blog/2022/10/...-solidity/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016