object_set_values.md

April 3, 2025 ยท View on GitHub

Setting Values of an Object

Setting values in an object is a common operation in programming. Our library provides the CHashObject_set_many function to set multiple values in an object at once. Let's explore how to use this function with a detailed example.

Example: Setting Multiple Values in an Object

Here's a step-by-step guide on how to set values in an object:

  1. Include the Necessary Header: Start by including the CHashManipulatorOne.c header file which contains the necessary definitions for our library.

    #include "CHashManipulatorOne.c"
    
  2. Initialize the Namespace and Modules: We need to initialize the CHashNamespace and its modules like object, array, and validator.

    CHashNamespace hash = newCHashNamespace();
    CHashObjectModule obj = hash.object;
    CHashArrayModule array = hash.array;
    CHashValidatorModule validator = hash.validator;
    
  3. Create an Object: Use the newCHashObject function to create an object with initial key-value pairs. In this example, we're creating an object with keys for name, age, height, and marital status.

    CHashObject *create() {
        return newCHashObject(
            "name", hash.newString("aaa"),
            "age", hash.newNumber(26),
            "height", hash.newNumber(20),
            "married", hash.newBool(true)
        );
    }
    
  4. Ensure the Object is Valid: Use validator.ensure_Object to ensure the object is valid before setting values.

    int main() {
        CHashArray *profile = create();
    
        validator.ensure_Object(profile);
    
  5. Check for Errors: Use if (hash.errors(profile)) to check if there were any errors during the validation.

        if (hash.errors(profile)) {
            printf("%s\n", hash.get_error_message(profile));
            hash.free(profile);
            return 1;
        }
    
  6. Set Multiple Values: Use CHashObject_set_many to set multiple values in the object. In this example, we're setting the age to 18 and marital status to false.

        CHashObject_set_many(profile,
            "age", hash.newNumber(18),
            "married", hash.newBool(false)
        );
    
  7. Print the Object: Use hash.print to print the updated object.

        hash.print(profile);
    
  8. Free the Memory: Don't forget to free the memory allocated for the object to prevent memory leaks.

        hash.free(profile);
    }
    

Full Code Example

Here's the complete code example that demonstrates how to set values in an object:

#include "CHashManipulatorOne.c"

CHashNamespace hash;
CHashObjectModule obj;
CHashArrayModule array;
CHashValidatorModule validator;

CHashObject *create() {
    return newCHashObject(
        "name", hash.newString("aaa"),
        "age", hash.newNumber(26),
        "height", hash.newNumber(20),
        "married", hash.newBool(true)
    );
}

int main() {
    hash = newCHashNamespace();
    obj = hash.object;
    array = hash.array;
    validator = hash.validator;

    CHashArray *profile = create();

    validator.ensure_Object(profile);
    if (hash.errors(profile)) {
        printf("%s\n", hash.get_error_message(profile));
        hash.free(profile);
        return 1;
    }

    CHashObject_set_many(profile,
        "age", hash.newNumber(18),
        "married", hash.newBool(false)
    );

    hash.print(profile);

    hash.free(profile);
}

By following these steps, you can easily set values in an object using our library. Remember to always check for errors and manage memory properly to ensure your program runs smoothly.