مشخصات مقاله
-
0.0
-
768
-
0
-
0
آموزش متغیرها در سالیدیتی
Solidity از سه نوع متغیر پشتیبانی میکند.
State Variables
متغیرهایی که مقادیر آنها به طور دائمی ذخیره میشود.
State Variables
متغیرهایی که مقادیر آنها به طور دائمی ذخیره میشود.
Local Variables
متغیرهایی که مقادیر آنها تا زمان اجرای تابع موجود است.
Global Variables
متغیرهای ویژهای که برای دریافت اطلاعات در مورد بلاکچین استفاده میشود.
Solidity یکزبان استاتیک است، به این معنی که حالت یا نوع متغیر محلی باید در هنگام اعلان مشخص شود. هر متغیر اعلام شده همیشه یک مقدار پیشفرض بر اساس نوع خود دارد. هیچ مفهوم و تعریفی برای "undefined" یا "null" وجود ندارد.
مثال State Variables
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10; // Using State variable
}
}
مثال Local Variables
pragma solidity ^0.5.0;
contract SolidityTest {
uint storedData; // State variable
constructor() public {
storedData = 10;
}
function getResult() public view returns(uint){
uint a = 1; // local variable
uint b = 2;
uint result = a + b;
return result; //access the local variable
}
}
خروجی
0: uint256: 10
مثال Global Variables
متغیرهای خاصی هستند که در فضای Global وجود دارند و اطلاعاتی در مورد بلاکچین و ویژگیهای تراکنش ارائه میدهند.
Returns
Name
Hash of the given block - only works for 256 most recent, excluding current, blocks
blockhash(uint blockNumber) returns (bytes32)
Current block miner's address
block.coinbase (address payable)
Current block difficulty
block.difficulty (uint)
Current block gaslimit
block.gaslimit (uint)
Current block number
block.number (uint)
Current block timestamp as seconds since unix epoch
block.timestamp (uint)
Remaining gas
gasleft() returns (uint256)
Complete calldata
msg.data (bytes calldata)
Sender of the message (current caller)
msg.sender (address payable)
First four bytes of the calldata (function identifier)
msg.sig (bytes4)
Number of wei sent with the message
msg.value (uint)
Current block timestamp
now (uint)
Gas price of the transaction
tx.gasprice (uint)
Sender of the transaction
tx.origin (address payable)
نام گذاری متغیرها Solidity
هنگام نامگذاری متغیرهای خود در Solidity، قوانین زیر را در نظر داشته باشید:
- شما نباید از کلمات کلیدی رزرو شده Solidity به عنوان نام متغیر استفاده کنید. به عنوان مثال، نام متغیرهای break یا boolean معتبر نیستند.
- نام متغیرهای Solidity نباید با یک عدد (0-9) شروع شود. به عنوان مثال، 123test یک نام متغیر نامعتبر است اما _123test یک نام معتبر است.
- نام متغیرهای Solidity به حروف بزرگ و کوچک حساس هستند. برای مثال Name و name دو متغیر متفاوت هستند.