0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

ตัวแปรและชนิดข้อมูล

สำรวจชนิดข้อมูลพื้นฐานของ Solidity ได้แก่ uint, address, bool และ bytes รวมถึงวิธีประกาศและใช้ตัวแปรภายในสัญญาอัจฉริยะ

ตัวแปรและชนิดข้อมูล เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Variables & Data Types: The Basics

Welcome to Lesson 2! In Solidity, just like other programming languages, we need ways to store and manage information.

This is where variables and data types come in handy. They are fundamental for building any smart contract.

  • Variables: Think of them as containers that hold a specific piece of data.
  • Data Types: Define what kind of data a variable can hold (e.g., numbers, text, true/false).

Storing Whole Numbers: `uint`

The uint data type is used to store unsigned integers. 'Unsigned' means they can only hold positive whole numbers (including zero).

  • uint can range from uint8 (8 bits) up to uint256 (256 bits).
  • uint256 is the default if you just write uint.
  • These are perfect for token balances, counts, or IDs.

`uint` Variable Example

Let's see how to declare and use uint variables in a simple contract. Notice how we use public to make them accessible from outside the contract.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyNumbers {
    uint public myFavoriteNumber; // Default uint256
    uint8 public smallCount;     // uint with 8 bits

    constructor() {
        myFavoriteNumber = 42;
        smallCount = 100;
    }

    function getFavoriteNumber() public view returns (uint) {
        return myFavoriteNumber;
    }

    function getSmallCount() public view returns (uint8) {
        return smallCount;
    }
}

True or False: The `bool` Type

The bool data type is simple but powerful. It can only hold one of two values: true or false.

  • Booleans are essential for conditional logic, like checking if a user has permission or if a process is complete.
  • By default, a bool variable is initialized to false if not explicitly set.

`bool` Variable Example

Here's a contract demonstrating how to declare and manipulate bool variables. We can set their initial state and update them later.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyFlags {
    bool public isActive;  // Default false, set to true in constructor
    bool private isAdmin; // Only accessible within the contract

    constructor() {
        isActive = true;
        isAdmin = false;
    }

    function getIsActive() public view returns (bool) {
        return isActive;
    }

    function setIsAdmin(bool _isAdmin) public {
        isAdmin = _isAdmin;
    }

    function checkAdmin() public view returns (bool) {
        return isAdmin;
    }
}

Handling Accounts: The `address` Type

The address data type is unique to blockchain development. It stores a 20-byte value, which represents an Ethereum account or contract address.

  • Every user wallet and smart contract on Ethereum has an address.
  • There's also address payable, which is an address that can receive Ether. You'll learn more about this later!
  • address types are crucial for managing ownership, permissions, and interactions between accounts.

`address` Variable Example

This example shows how a contract can store and retrieve addresses. msg.sender is a special global variable that always refers to the address that initiated the current transaction.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract AccountManager {
    address public owner; // Stores the contract deployer's address
    address public lastInteractedUser;

    constructor() {
        owner = msg.sender; // Set owner to the deployer
    }

    function setLastInteractedUser() public {
        lastInteractedUser = msg.sender; // Update with current caller
    }

    function getOwner() public view returns (address) {
        return owner;
    }

    function getLastInteractedUser() public view returns (address) {
        return lastInteractedUser;
    }
}

Raw Data: `bytes` & `bytesN`

When you need to store raw byte sequences, Solidity provides the bytes and bytesN types.

  • bytes: A dynamically-sized array of bytes. Use it when the size of the data isn't fixed.
  • bytes1 to bytes32: Fixed-size byte arrays, where N specifies the exact number of bytes (e.g., bytes32 for a cryptographic hash).
  • These are often used for cryptographic hashes, arbitrary binary data, or short strings where gas efficiency is critical.

`bytes` Variable Example

Here, we store a fixed-size hash using bytes32 and a dynamic sequence of bytes using bytes. Notice the hexadecimal literal for the hash and the hex"..." prefix for byte values.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DataStorage {
    bytes32 public documentHash; // Fixed 32-byte hash
    bytes public dynamicData;    // Dynamic byte array

    constructor() {
        // Example hash (0x prefixed hex literal)
        documentHash = 0x123456789012345678901234567890123456789012345678901234567890abcd;
        // Example dynamic data (hex string "hello")
        dynamicData = hex"68656c6c6f"; 
    }

    function updateDynamicData(bytes memory _newData) public {
        dynamicData = _newData;
    }

    function getDocumentHash() public view returns (bytes32) {
        return documentHash;
    }

    function getDynamicData() public view returns (bytes memory) {
        return dynamicData;
    }
}

Data Type Challenge

Choosing the right data type is crucial for efficiency and security in smart contracts. Test your knowledge!

Variables & Types: Key Takeaways

Great job! You've learned the fundamental data types in Solidity:

  • uint: For positive whole numbers (balances, counts).
  • bool: For true/false values (flags, conditions).
  • address: For Ethereum accounts and contract addresses.
  • bytes/bytesN: For raw, uninterpreted byte sequences (hashes, arbitrary data).

Choosing the correct data type is vital for writing efficient and secure smart contracts. Next, we'll explore how to define functions and control the flow of your contract's logic!

คำถามที่พบบ่อย

บทเรียน “ตัวแปรและชนิดข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวแปรและชนิดข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวแปรและชนิดข้อมูล”

สำรวจชนิดข้อมูลพื้นฐานของ Solidity ได้แก่ uint, address, bool และ bytes รวมถึงวิธีประกาศและใช้ตัวแปรภายในสัญญาอัจฉริยะ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน

บทเรียน “ตัวแปรและชนิดข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่ Solidity
  2. ตัวแปรและชนิดข้อมูล
  3. ฟังก์ชันและโครงสร้างควบคุม
← กลับไปที่ Web3 & DApp Development Fundamentals