# How to add functions in a class and set the object automatically?

**URL:** https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584
**Category:** PXT
**Created:** [June 30, 2020, 12:16pm UTC](https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584 "2020-06-30T12:16:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ameyrr250](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@ameyrr250](https://forum.makecode.com/u/ameyrr250)
#### Post date: [June 30, 2020, 12:16pm UTC](https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584/1 "2020-06-30T12:16:15Z")

</div>

Hi,  
Posting it here as unsure if its more relevant here.  
I want to have all my functions in a class for each main block. Right now what I do is,

namespace ABC{  
//% block=“create lcd settings”  
//% blockSetVariable=“ABCObjSettings”  
//% weight=110  
export function createABCObjSettings(): ABCObjSettings{  
return new ABCObjSettings();  
}  
export class ABC{

…  
…  
…

//% block="$this example $variablePassed  
//% blockId=Example  
//% blockNamespace=ABC  
//% this.shadow=variables\_get  
//% this.defl=“ABCObjSettings”  
//% weight=90  
exampleExposedFunction(variablePassed : number) {

sum=this.variablePassed

}

}

}

In the blocks on the webpage, I have to first place the “set ABCObjSettings to create lcd settings”, then select my exposed function.  
I have to place the “set ABCObjSettings to create lcd settings” at least once to use any exposed

Is there a way to automatically create that Object and use?

Thanks!

---

<div class="post-metadata">

### Author: ![ameyrr250](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@ameyrr250](https://forum.makecode.com/u/ameyrr250)
#### Post date: [July 14, 2020, 1:01am UTC](https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584/2 "2020-07-14T01:01:59Z")

</div>

Any answers?

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [July 14, 2020, 4:44am UTC](https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584/3 "2020-07-14T04:44:16Z")

</div>

To clarify, does the user need to be able to create multiple of these objects, or is this just internal state? If they need to be able to use multiple, they’ll likely need to have a reference to it, so there will need to be a block to create one.

If it’s just internal state needed to keep track of things in your extension, though, you can just store it as a variable within a namespace, and lazily create it whenever they first case a function. For example, something like

```typescript
namespace mycoolextension {
    class CoolExtensionInternalState {
        field: number;
        constructor() { }
    }

    let stateInstance: CoolExtensionInternalState;
    function init() {
        if (!stateInstance) {
            stateInstance = new CoolExtensionInternalState();
        }
    }

    //% block definition stuff here
    export function exposedFunction(variablePassed: number) {
        init();
        stateInstance.field = variablePassed;
    }
}

```

If you keep track of it internally, it’s just state you’re maintaining / not something you’d ever expose to the user. The init function would be called in all functions that need to reference or modify the state to lazily create anything you need.

---

<div class="post-metadata">

### Author: ![ameyrr250](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@ameyrr250](https://forum.makecode.com/u/ameyrr250)
#### Post date: [July 15, 2020, 11:51am UTC](https://forum.makecode.com/t/how-to-add-functions-in-a-class-and-set-the-object-automatically/2584/4 "2020-07-15T11:51:42Z")

</div>

Hi,  
Thanks for the reply. I tried this but it doesn’t work if the exposed function is inside the class. I want all my functions inside the class.
