انواع داده در سالیدیتی (solidity)

انواع داده در سالیدیتی (solidity) نشان دهنده نوع داده ای است که برنامه باید استفاده کند. نوع می تواند عددی، الفبایی، اعشاری، bool و غیره باشد. این ویژگی ها به کامپایلر برنامه می گویند که چگونه برنامه نویس قصد دارد از داده ها با محدود کردن مقادیری که یک متغیر یا یک تابع ممکن است استفاده کند. در زیر انواع داده های رایجی که در زبان های برنامه نویسی مختلف استفاده می شوند، آورده شده است.
نمونه هایی از انواع داده ها در زبان های برنامه نویسی
DATA TYPE | EXAMPLE VALUES |
---|---|
Intiger | 123456789 |
String | Hello World |
Boolean | True |
انواع داده در سالیدیتی (Solidity)
Solidity یک زبان استاتیکی است که به این معنی است که نوع هر متغیر باید در کد شما مشخص شود. انواع داده ها به کامپایلر دستور می دهد تا میزان استفاده از متغیرها را در قرارداد شما بررسی کند. انواع داده های اعلام شده دارای مقادیر پیش فرض هستند که به عنوان حالت صفر (0) شناخته می شوند. برای مثال مقدار پیشفرض یک مقدار boolبرابر با False و مقدار پیشفرض یک integer برابر با 0 است.
مفهوم مقادیر تعریفنشده یا تهی در سالیدیتی به معنای وجود ندارد است. بنابراین نوع های داده ای می توانند چندین مقدار به عنوان مثال (- hi، 123، true، و غیره) یا 0 به عنوان پیش فرض داشته باشند. زبان برنامه نویسی Solidity از انواع مختلف داده پشتیبانی می کند.
نمونه هایی از این انواع را می توان در شبکه زیر مشاهده کرد.
DATA TYPE | NAME USED IN CODE | EXAMPLE VALUE | BYTES |
---|---|---|---|
Boolean | bool | True or False | 1 |
String | string | Hello | 32 |
Integers – signed and unsigned integers of various sizes. Specify sizes in steps of 8 from 8 to 256. For example uint8, uint16, uint256. uint and int are aliases for uint256 and int256 | int uint | 12345 | 32 but can make is smaller by specifying. uint128 – 16 bytes uint96 – 12 bytes uint64 – 8 bytes |
Address – Address payable includes the members transfer and send so it can receive Ether | address address payable | 0x1f9840a85d5aF5bf1D… | 20 |
Fixed Point Number (signed and unsigned) | fixed ufixed | 25.50 (fixed number of decimal places) | |
Byte – A single byte. You can also define arrays of 1–32 bytes using the type bytes2, byte3, up to bytes32 | byte | byte singleChar = ‘h’ bytes16 msgHello = ‘Hello, world!’; | 1 but can make larger by specifying |
توجه: هنگامی که میخواهید فضای ذخیرهسازی را در قرارداد خود بهینه کنید، اطلاعات بایت مفید است. فقط از آنچه نیاز دارید استفاده کنید زیرا ذخیره سازی روی بلاک چین گران است.
اپراتورهای Booleans
در قرارداد هوشمند خود می توانید یک bool که حاوی مقادیر درست یا نادرست است را اعلام کنید. سپس از عملگرهای زیر برای ارزیابی داده های خود در عبارات مختلف استفاده کنید.
OPERATOR NAME | DENOTATION |
---|---|
! | Negative |
&& | Conjunction, “and”. Short circuit – If part of the expression evaluates to true the remaining part of the expression will not be evaluated |
|| | Disjunction, “or”. Short circuit – If part of the expression evaluates to true the remaining part of the expression will not be evaluated |
== | Equality |
!= | Inequality |
عملگرها برای اعداد صحیح
در قرارداد هوشمند خود می توانید اعداد صحیح مختلفی را اعلام کنید سپس از عملگرهای زیر برای ارزیابی داده های خود در عبارات مختلف استفاده کنید.
OPERATOR NAME | DENOTATION |
---|---|
< | Less then. Evaluates to true or false |
<= | Less then or equal to. Evaluates to true or false |
> | Greater then. Evaluates to true or false |
>= | Greater then or equal to. Evaluates to true or false |
== | Equals. Evaluates to true or false |
!= | Not equal. Evaluates to true or false |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo – the remainder after dividing one number by another |
** | Exponentiation |
++ | Increase the integer value by 1 |
– | Decrease the integer value by 1 |
اپراتورهای اعداد نقطه ثابت
در قرارداد هوشمند خود می توانید اعداد نقطه ثابت مختلفی را اعلام کنید سپس از عملگرهای زیر برای ارزیابی داده های خود در عبارات مختلف استفاده کنید.
OPERATOR NAME | DENOTATION |
---|---|
< | Less then. Evaluates to true or false |
<= | Less then or equal to. Evaluates to true or false |
> | Greater then. Evaluates to true or false |
>= | Greater then or equal to. Evaluates to true or false |
== | Equals. Evaluates to true or false |
!= | Not equal. Evaluates to true or false |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo – the remainder after dividing one number by another |
اپراتورها برای آدرس ها
برای ارزیابی داده های آدرس در عبارات مختلف از عملگرهای زیر استفاده کنید. عملگرهای زیر را می توان با این نوع استفاده کرد.
OPERATOR NAME | DENOTATION |
---|---|
< | Less then. Evaluates to true or false |
<= | Less then or equal to. Evaluates to true or false |
> | Greater then. Evaluates to true or false |
>= | Greater then or equal to. Evaluates to true or false |
== | Equals. Evaluates to true or false |
!= | Not equal. Evaluates to true or false |
توجه: شاید لازم نباشد به تمایز بین آدرس و آدرس قابل پرداخت اهمیت دهید. شما فقط می توانید از آدرس در کد خود استفاده کنید. به عنوان مثال، اگر از الگوی برداشت استفاده می کنید، می توانید (و باید) خود آدرس را به عنوان آدرس ذخیره کنید، زیرا تابع انتقال را در msg.sender فراخوانی می کنید، که قبلاً روی آدرس قابل پرداخت تنظیم شده است.
انواع داده در کجای قرارداد استفاده می شود؟
انواع داده ها در سراسر قرارداد هوشمند شما اعلام شده و استفاده می شود. برخی از نمونه هایی که در آن یک نوع داده را اعلام می کنید عبارتند از:
- متغیرهای حالت: در ابتدای قرارداد خود برای اعلام اطلاعاتی که می خواهید در زنجیره بلوک ذخیره کنید
- توابع: داده هایی برای استفاده در یک تابع
قرارداد نمونه زیر چندین نوع داده مختلف را اعلام می کند.
pragma solidity ^0.6.0;
//different data types
contract MyContract {
//state variables at the beginning of the contract are declared and stored on the blockchain
//each variable needs to have a data type
//data type string called myString
string public myString = "hello";
//data type bytes32 is encoded and used for performance
bytes32 public myBytes32 = "hello world";
//data type int called myInt
int public myInt = 1;
//data types uint called myUint
uint public myUint = 2;
//data types uint256 called myUint256
uint256 public myUint256 = 3;
uint8 public myUint8 = 4;
//data type address called myAddress
address public myAddress = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;
//function with local variable are stored in memory.
//variables within functions have a declared data type
function getValue() public pure returns(uint) {
uint value = 6;
return value;
}
اپراتورهای واگذاری
این عملگرها برای تخصیص مقدار به یک متغیر هستند. عملوند سمت چپ متغیر است در حالی که عملوند سمت راست مقدار است. Solidity از عملگرهای حسابی زیر پشتیبانی می کند.
OPERATOR NAME | DENOTATION |
---|---|
= | Simply assigns the value at the right side to the operand at the left side |
+= | Adds operand at the right side to operand at the left side and assigns the value to left operand |
-= | Subtracts operand at the right side from operand at the left side and assigns the value to left operand |
*= | Multiplies both the operands and assigns the value to the left operand |
/= | Divides operand at the left side by operand at the right side and assigns the value to left operand |
%= | Divides operand at the left side by operand at the right side and assign the remainder to left operand |
pragma solidity ^0.8.7;
// Creating a contract to demonstrate how assignment operators work
contract SolidityOperatorExample {
// variables are assigned a value
// function preforms operation and changes values of variables
// after deployment execute getResults function
// review values of each variable
// Declaring variables
uint public variable_add = 70;
uint public variable_sub = 50;
uint public variable_mul = 25;
uint public variable_div = 80;
uint public variable_mod = 68;
// function that demonstrates how assignment operators work
function getRusults() public{
variable_add += 25;
variable_sub -= 30;
variable_mul *= 25;
variable_div /= 5;
variable_mod %= 60;
return ;
}
}
این کد فقط برای اهداف آموزشی و سرگرمی است. کد ممیزی نشده است و با مسئولیت خود استفاده کنید. به یاد داشته باشید که قراردادهای هوشمند آزمایشی هستند و ممکن است حاوی اشکال باشند.

در صورتی که تجربه خاصی در خصوص برنامهنویسی ندارید میتوانید از دورههای رایگان سایت ما “فرازمان“، استفاده کنید. همچنین اگر به دورههای پیشرفتهتری در این خصوص نیاز داشته باشید، ما با آموزش های حرفه ای که در سایتمان قرار دادیم می توانید به سطح دلخواهتان با تلاش و پشتکار برسید.
نقشه راه
راهنما آکادمی فرازمان
برای یادگیری برنامه نویسی بلاکچین…
در این باره بیشتر بخوانید
2 Comments
به گفتگوی ما بپیوندید و دیدگاه خود را با ما در میان بگذارید.
Thank for great information
Thank you for your consideration