Variable is null, but right hand is not null












0















While debugging I noticed something weird:



enter image description here



the result var is shown as null, both in mouse hover and in the watch, but the right hand side is actually returning non-null value as you can see in the watch.



Any ideas why?



EDIT: the result variable is also declared in the IF statement, something like this:



if (somethingIsTrue) { var result = xxx; }
else { var result = yyy; }


As soon as I renamed the second result to something else, all started showing correctly.










share|improve this question

























  • Clean and rebuild?

    – Michael Randall
    Nov 20 '18 at 3:37






  • 1





    Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

    – Ian Mercer
    Nov 20 '18 at 3:42











  • actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

    – Veselin Vasilev
    Nov 20 '18 at 3:49






  • 1





    Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

    – P.Brian.Mackey
    Nov 20 '18 at 3:50











  • @pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

    – Sнаđошƒаӽ
    Nov 20 '18 at 4:11


















0















While debugging I noticed something weird:



enter image description here



the result var is shown as null, both in mouse hover and in the watch, but the right hand side is actually returning non-null value as you can see in the watch.



Any ideas why?



EDIT: the result variable is also declared in the IF statement, something like this:



if (somethingIsTrue) { var result = xxx; }
else { var result = yyy; }


As soon as I renamed the second result to something else, all started showing correctly.










share|improve this question

























  • Clean and rebuild?

    – Michael Randall
    Nov 20 '18 at 3:37






  • 1





    Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

    – Ian Mercer
    Nov 20 '18 at 3:42











  • actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

    – Veselin Vasilev
    Nov 20 '18 at 3:49






  • 1





    Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

    – P.Brian.Mackey
    Nov 20 '18 at 3:50











  • @pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

    – Sнаđошƒаӽ
    Nov 20 '18 at 4:11
















0












0








0


0






While debugging I noticed something weird:



enter image description here



the result var is shown as null, both in mouse hover and in the watch, but the right hand side is actually returning non-null value as you can see in the watch.



Any ideas why?



EDIT: the result variable is also declared in the IF statement, something like this:



if (somethingIsTrue) { var result = xxx; }
else { var result = yyy; }


As soon as I renamed the second result to something else, all started showing correctly.










share|improve this question
















While debugging I noticed something weird:



enter image description here



the result var is shown as null, both in mouse hover and in the watch, but the right hand side is actually returning non-null value as you can see in the watch.



Any ideas why?



EDIT: the result variable is also declared in the IF statement, something like this:



if (somethingIsTrue) { var result = xxx; }
else { var result = yyy; }


As soon as I renamed the second result to something else, all started showing correctly.







c# visual-studio debugging visual-studio-2017






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 6:34









Jon Skeet

1088k68979408439




1088k68979408439










asked Nov 20 '18 at 3:35









Veselin VasilevVeselin Vasilev

2,36411015




2,36411015













  • Clean and rebuild?

    – Michael Randall
    Nov 20 '18 at 3:37






  • 1





    Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

    – Ian Mercer
    Nov 20 '18 at 3:42











  • actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

    – Veselin Vasilev
    Nov 20 '18 at 3:49






  • 1





    Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

    – P.Brian.Mackey
    Nov 20 '18 at 3:50











  • @pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

    – Sнаđошƒаӽ
    Nov 20 '18 at 4:11





















  • Clean and rebuild?

    – Michael Randall
    Nov 20 '18 at 3:37






  • 1





    Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

    – Ian Mercer
    Nov 20 '18 at 3:42











  • actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

    – Veselin Vasilev
    Nov 20 '18 at 3:49






  • 1





    Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

    – P.Brian.Mackey
    Nov 20 '18 at 3:50











  • @pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

    – Sнаđошƒаӽ
    Nov 20 '18 at 4:11



















Clean and rebuild?

– Michael Randall
Nov 20 '18 at 3:37





Clean and rebuild?

– Michael Randall
Nov 20 '18 at 3:37




1




1





Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

– Ian Mercer
Nov 20 '18 at 3:42





Maybe a case of multiple evaluation? First call gets null, puts it in result. Second call (to display it in the Watch) gets a value. What does GetCachedData() do?

– Ian Mercer
Nov 20 '18 at 3:42













actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

– Veselin Vasilev
Nov 20 '18 at 3:49





actually in the main if block the result variable is also defined, so probably that's what's causing it. if (something) { var result = xxx; } else { var result = yyy; } as soon as I renamed it to something else in the Else block - all is shown properly.

– Veselin Vasilev
Nov 20 '18 at 3:49




1




1





Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

– P.Brian.Mackey
Nov 20 '18 at 3:50





Meh. You are probably just running up against deferred execution. blogs.msdn.microsoft.com/charlie/2007/12/10/…

– P.Brian.Mackey
Nov 20 '18 at 3:50













@pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

– Sнаđошƒаӽ
Nov 20 '18 at 4:11







@pbrian Not convinced the issue here could be deferred execution. Look at the watch, the right hand expression does return an object instance, not some IQueryable or something. It seems more like what mercer mentioned.

– Sнаđошƒаӽ
Nov 20 '18 at 4:11














1 Answer
1






active

oldest

votes


















2














When your code is like this



if (somethingIsTrue) { var result = xxx; } else { var result = yyy; }


Then that 'result' variable is (re)declared within the scope of that if-statement. That means the value is only available within that block.



Solution: declare the variable outside of the block (if you haven't done that already) and remove the 'var's inside the 'if' and 'else' blocks






share|improve this answer























    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%2f53385843%2fvariable-is-null-but-right-hand-is-not-null%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    When your code is like this



    if (somethingIsTrue) { var result = xxx; } else { var result = yyy; }


    Then that 'result' variable is (re)declared within the scope of that if-statement. That means the value is only available within that block.



    Solution: declare the variable outside of the block (if you haven't done that already) and remove the 'var's inside the 'if' and 'else' blocks






    share|improve this answer




























      2














      When your code is like this



      if (somethingIsTrue) { var result = xxx; } else { var result = yyy; }


      Then that 'result' variable is (re)declared within the scope of that if-statement. That means the value is only available within that block.



      Solution: declare the variable outside of the block (if you haven't done that already) and remove the 'var's inside the 'if' and 'else' blocks






      share|improve this answer


























        2












        2








        2







        When your code is like this



        if (somethingIsTrue) { var result = xxx; } else { var result = yyy; }


        Then that 'result' variable is (re)declared within the scope of that if-statement. That means the value is only available within that block.



        Solution: declare the variable outside of the block (if you haven't done that already) and remove the 'var's inside the 'if' and 'else' blocks






        share|improve this answer













        When your code is like this



        if (somethingIsTrue) { var result = xxx; } else { var result = yyy; }


        Then that 'result' variable is (re)declared within the scope of that if-statement. That means the value is only available within that block.



        Solution: declare the variable outside of the block (if you haven't done that already) and remove the 'var's inside the 'if' and 'else' blocks







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 6:31









        Hans KestingHans Kesting

        29.3k55893




        29.3k55893
































            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%2f53385843%2fvariable-is-null-but-right-hand-is-not-null%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini