Set variable from function










0















This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:



errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;

return null;



Besides the errorText I want the function to also set the variable errorVar to true and to false if the function returns null.










share|improve this question
























  • And what is your problem?

    – Daniel Hilgarth
    Nov 14 '18 at 9:15











  • If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

    – PhilipK
    Nov 14 '18 at 10:47
















0















This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:



errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;

return null;



Besides the errorText I want the function to also set the variable errorVar to true and to false if the function returns null.










share|improve this question
























  • And what is your problem?

    – Daniel Hilgarth
    Nov 14 '18 at 9:15











  • If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

    – PhilipK
    Nov 14 '18 at 10:47














0












0








0








This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:



errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;

return null;



Besides the errorText I want the function to also set the variable errorVar to true and to false if the function returns null.










share|improve this question
















This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:



errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;

return null;



Besides the errorText I want the function to also set the variable errorVar to true and to false if the function returns null.







angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:48







PhilipK

















asked Nov 14 '18 at 9:14









PhilipKPhilipK

238




238












  • And what is your problem?

    – Daniel Hilgarth
    Nov 14 '18 at 9:15











  • If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

    – PhilipK
    Nov 14 '18 at 10:47


















  • And what is your problem?

    – Daniel Hilgarth
    Nov 14 '18 at 9:15











  • If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

    – PhilipK
    Nov 14 '18 at 10:47

















And what is your problem?

– Daniel Hilgarth
Nov 14 '18 at 9:15





And what is your problem?

– Daniel Hilgarth
Nov 14 '18 at 9:15













If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

– PhilipK
Nov 14 '18 at 10:47






If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable

– PhilipK
Nov 14 '18 at 10:47













2 Answers
2






active

oldest

votes


















1














You can do something like this:



 errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;


this.errorVar = false;
return null;






share|improve this answer

























  • Nope, tried that first of course. It comes back "undefined is not an object"

    – PhilipK
    Nov 14 '18 at 10:39











  • Is errorVar defined within the context of a class?

    – Hayden Hall
    Nov 14 '18 at 10:48











  • yes, it is - it's a reactive form and the variable definition and function is inside the export class

    – PhilipK
    Nov 14 '18 at 10:52



















0














I think there are several problems



  • your errorVar should be declared using var or let so you can set it in the function via a closure

  • your errorMessageText variable must be set using = and not :

like this:



var errorVar: boolean = false;

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;

errorVar = false;
return null;






share|improve this answer























  • Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

    – PhilipK
    Nov 15 '18 at 8:08











  • Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

    – XouDo
    Nov 15 '18 at 9:12












  • Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

    – XouDo
    Nov 15 '18 at 9:22












  • Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

    – PhilipK
    Nov 17 '18 at 10:28












  • Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

    – XouDo
    Nov 18 '18 at 6:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296586%2fset-variable-from-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can do something like this:



 errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;


this.errorVar = false;
return null;






share|improve this answer

























  • Nope, tried that first of course. It comes back "undefined is not an object"

    – PhilipK
    Nov 14 '18 at 10:39











  • Is errorVar defined within the context of a class?

    – Hayden Hall
    Nov 14 '18 at 10:48











  • yes, it is - it's a reactive form and the variable definition and function is inside the export class

    – PhilipK
    Nov 14 '18 at 10:52
















1














You can do something like this:



 errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;


this.errorVar = false;
return null;






share|improve this answer

























  • Nope, tried that first of course. It comes back "undefined is not an object"

    – PhilipK
    Nov 14 '18 at 10:39











  • Is errorVar defined within the context of a class?

    – Hayden Hall
    Nov 14 '18 at 10:48











  • yes, it is - it's a reactive form and the variable definition and function is inside the export class

    – PhilipK
    Nov 14 '18 at 10:52














1












1








1







You can do something like this:



 errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;


this.errorVar = false;
return null;






share|improve this answer















You can do something like this:



 errorVar: boolean = false

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;


this.errorVar = false;
return null;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:25









Oram

810518




810518










answered Nov 14 '18 at 9:19









Hayden HallHayden Hall

24136




24136












  • Nope, tried that first of course. It comes back "undefined is not an object"

    – PhilipK
    Nov 14 '18 at 10:39











  • Is errorVar defined within the context of a class?

    – Hayden Hall
    Nov 14 '18 at 10:48











  • yes, it is - it's a reactive form and the variable definition and function is inside the export class

    – PhilipK
    Nov 14 '18 at 10:52


















  • Nope, tried that first of course. It comes back "undefined is not an object"

    – PhilipK
    Nov 14 '18 at 10:39











  • Is errorVar defined within the context of a class?

    – Hayden Hall
    Nov 14 '18 at 10:48











  • yes, it is - it's a reactive form and the variable definition and function is inside the export class

    – PhilipK
    Nov 14 '18 at 10:52

















Nope, tried that first of course. It comes back "undefined is not an object"

– PhilipK
Nov 14 '18 at 10:39





Nope, tried that first of course. It comes back "undefined is not an object"

– PhilipK
Nov 14 '18 at 10:39













Is errorVar defined within the context of a class?

– Hayden Hall
Nov 14 '18 at 10:48





Is errorVar defined within the context of a class?

– Hayden Hall
Nov 14 '18 at 10:48













yes, it is - it's a reactive form and the variable definition and function is inside the export class

– PhilipK
Nov 14 '18 at 10:52






yes, it is - it's a reactive form and the variable definition and function is inside the export class

– PhilipK
Nov 14 '18 at 10:52














0














I think there are several problems



  • your errorVar should be declared using var or let so you can set it in the function via a closure

  • your errorMessageText variable must be set using = and not :

like this:



var errorVar: boolean = false;

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;

errorVar = false;
return null;






share|improve this answer























  • Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

    – PhilipK
    Nov 15 '18 at 8:08











  • Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

    – XouDo
    Nov 15 '18 at 9:12












  • Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

    – XouDo
    Nov 15 '18 at 9:22












  • Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

    – PhilipK
    Nov 17 '18 at 10:28












  • Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

    – XouDo
    Nov 18 '18 at 6:36















0














I think there are several problems



  • your errorVar should be declared using var or let so you can set it in the function via a closure

  • your errorMessageText variable must be set using = and not :

like this:



var errorVar: boolean = false;

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;

errorVar = false;
return null;






share|improve this answer























  • Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

    – PhilipK
    Nov 15 '18 at 8:08











  • Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

    – XouDo
    Nov 15 '18 at 9:12












  • Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

    – XouDo
    Nov 15 '18 at 9:22












  • Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

    – PhilipK
    Nov 17 '18 at 10:28












  • Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

    – XouDo
    Nov 18 '18 at 6:36













0












0








0







I think there are several problems



  • your errorVar should be declared using var or let so you can set it in the function via a closure

  • your errorMessageText variable must be set using = and not :

like this:



var errorVar: boolean = false;

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;

errorVar = false;
return null;






share|improve this answer













I think there are several problems



  • your errorVar should be declared using var or let so you can set it in the function via a closure

  • your errorMessageText variable must be set using = and not :

like this:



var errorVar: boolean = false;

function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;

errorVar = false;
return null;







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:12









XouDoXouDo

6916




6916












  • Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

    – PhilipK
    Nov 15 '18 at 8:08











  • Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

    – XouDo
    Nov 15 '18 at 9:12












  • Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

    – XouDo
    Nov 15 '18 at 9:22












  • Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

    – PhilipK
    Nov 17 '18 at 10:28












  • Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

    – XouDo
    Nov 18 '18 at 6:36

















  • Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

    – PhilipK
    Nov 15 '18 at 8:08











  • Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

    – XouDo
    Nov 15 '18 at 9:12












  • Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

    – XouDo
    Nov 15 '18 at 9:22












  • Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

    – PhilipK
    Nov 17 '18 at 10:28












  • Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

    – XouDo
    Nov 18 '18 at 6:36
















Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

– PhilipK
Nov 15 '18 at 8:08





Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.

– PhilipK
Nov 15 '18 at 8:08













Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

– XouDo
Nov 15 '18 at 9:12






Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.

– XouDo
Nov 15 '18 at 9:12














Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

– XouDo
Nov 15 '18 at 9:22






Concerning the "pointer" thing, what you can do is pass an object errorMessageText:"" as parameter, and modify its property inside the function.

– XouDo
Nov 15 '18 at 9:22














Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

– PhilipK
Nov 17 '18 at 10:28






Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!

– PhilipK
Nov 17 '18 at 10:28














Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

– XouDo
Nov 18 '18 at 6:36





Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

– XouDo
Nov 18 '18 at 6:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296586%2fset-variable-from-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo