0

I've the below simple code, in which I have 2 variables defines in the Main method, and I need to access them from another method.

but I got the variables are not defined, though I defined them in the declerations/main.d.ts file as:

declare let voiceReady:any;
declare let x:any;

the main.ts file is;

/// <reference path="../declerations/main.d.ts" />
namespace CORE{
    export class Program {
      public static Main(): void {
            var voiceReady = new CORE.Listen(CORE.commands).commands;
            console.log(voiceReady);
            var x = 560;
            Program.execute('anything');
     }


        public static execute(spokenText:string):void{
            console.log('123');
            console.log(x);
            console.log(voiceReady);
       }
    }
}

UPDATE If I removed the deceleration from the .d.ts file, and added them to the class itself, I get compilation error as shown below:

enter image description here

0

1 Answer 1

1

Declare them as properties of the Program class.

namespace CORE{
export class Program {

    //Declare the properties here and access them in the methods
    static voiceReady: any;       

    public static Main(): void {
      this.voiceReady = new CORE.Listen(CORE.commands).commands;
      console.log(this.voiceReady);
      Program.execute('anything');
    }

    public static execute(spokenText:string):void{
        console.log(this.voiceReady);
     }
  }
}

If you want to reference from another class, import it outside your module like

import { Program } from "./Program"; 

and use it in your class like Program.Main();.

Sign up to request clarification or add additional context in comments.

5 Comments

I got compilation error, I updated my question with this error, as I can not upload pic here, so I uploaded it in the question itself.
Updated my answer!
Thanks. what if I need to call it from another Class?
Added that as well!
Thanks a lot Daniel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.