# Properties and Events (class)

Properties and events are allowed in classes.

## Defining properties and events

When property **Name** is associated with event **NameChanged**, magic doesn't happen automatically, the implementation of **SetName** should trigger **NameChanged** approppriately.

The property could also be readonly, or not associated with an event.

Auto-property is very useful to avoid boiler-plate code.

Auto-property must be initialized.

### Full-featured property

```
class WithProp
{
    prop Name: string = "" {}
}
```
 which means:
```
class WithProp
{
    var name: string = "";

    event NameChanged();

    func GetName(): string
    {
        return name;
    }

    func SetName(value: string): void
    {
        if (name != value)
        {
            name = value;
            NameChanged();
        }
    }

    prop Name: string {GetName, SetName : NameChanged}
}
```
 Note that **name** is not accessible if this variable is generated by auto-property.

### Readonly property

```
class WithProp
{
    prop Name: string = "" {const}
}
```
 which means:
```
class WithProp
{
    var name: string = "";

    event NameChanged();

    func GetName(): string
    {
        return name;
    }

    func SetName(value: string): void
    {
        if (name != value)
        {
            name = value;
            NameChanged();
        }
    }

    prop Name: string {GetName : NameChanged}
}
```
 Note that **name** is not accessible if this variable is generated by auto-property.

Even this property is readonly, but **SetName** is still generated, because this is the only way to modify the property. **SetName** becomse **protected** in generated C++ code, so calling this function outside of this class will result in wrong C++ code.

### Without event

```
class WithProp
{
    prop Name: string = "" {not observe}
}
```
 which means:
```
class WithProp
{
    var name: string = "";

    func GetName(): string
    {
        return name;
    }

    func SetName(value: string): void
    {
        name = value;
    }

    prop Name: string {GetName, SetName}
}
```
 Note that **name** is not accessible if this variable is generated by auto-property.

### Readonly property without event

```
class WithProp
{
    prop Name: string = "" {const, not observe}
}
```
 which means:
```
class WithProp
{
    var name: string = "";

    func GetName(): string
    {
        return name;
    }

    func SetName(value: string): void
    {
        name = value;
    }

    prop Name: string {GetName}
}
```
 Note that **name** is not accessible if this variable is generated by auto-property.

Even this property is readonly, but **SetName** is still generated, because this is the only way to modify the property. **SetName** becomes **protected** in generated C++ code, so calling this function outside of this class will result in wrong C++ code.

## Using events

Events in classes behaves exactly the same as [events in interfaces.](../.././workflow/lang/interface_prop.md)

