Boolean as methods parameter is not changing state












1















I want to change the state of a boolean in a function. My function has 4 parameters and the fourth one is a bool which is by default true, but I want to change it's state to false inside the function .



I am calling my methode is following,



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, GameControl.control.stone_9);


GameControl.control.stone_9 is by default true. It should become false once visibilty is set to false ..but that is not happing... stone_9 stays true.



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,bool Visibility)
{
if (DragHandler2.itemBegingDragged.name.Contains(gemColor) && DragHandler2.itemBegingDragged.transform.parent.name == slotColor)
{
Debug.Log(DragHandler2.itemBegingDragged.name);
Visibility=false;
puzzleStuk.SetActive(visibility);
Debug.Log(GameControl.control.stone_9); //true
DragHandler2.itemBegingDragged.SetActive(false);

}


I expect GameControl.control.stone_9 to change it's state to false because I am changing the state of the parameter (Visibility) to false but GameControl.control.stone_9 stays true.










share|improve this question























  • Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

    – Frontear
    Jan 1 at 20:47


















1















I want to change the state of a boolean in a function. My function has 4 parameters and the fourth one is a bool which is by default true, but I want to change it's state to false inside the function .



I am calling my methode is following,



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, GameControl.control.stone_9);


GameControl.control.stone_9 is by default true. It should become false once visibilty is set to false ..but that is not happing... stone_9 stays true.



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,bool Visibility)
{
if (DragHandler2.itemBegingDragged.name.Contains(gemColor) && DragHandler2.itemBegingDragged.transform.parent.name == slotColor)
{
Debug.Log(DragHandler2.itemBegingDragged.name);
Visibility=false;
puzzleStuk.SetActive(visibility);
Debug.Log(GameControl.control.stone_9); //true
DragHandler2.itemBegingDragged.SetActive(false);

}


I expect GameControl.control.stone_9 to change it's state to false because I am changing the state of the parameter (Visibility) to false but GameControl.control.stone_9 stays true.










share|improve this question























  • Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

    – Frontear
    Jan 1 at 20:47
















1












1








1








I want to change the state of a boolean in a function. My function has 4 parameters and the fourth one is a bool which is by default true, but I want to change it's state to false inside the function .



I am calling my methode is following,



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, GameControl.control.stone_9);


GameControl.control.stone_9 is by default true. It should become false once visibilty is set to false ..but that is not happing... stone_9 stays true.



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,bool Visibility)
{
if (DragHandler2.itemBegingDragged.name.Contains(gemColor) && DragHandler2.itemBegingDragged.transform.parent.name == slotColor)
{
Debug.Log(DragHandler2.itemBegingDragged.name);
Visibility=false;
puzzleStuk.SetActive(visibility);
Debug.Log(GameControl.control.stone_9); //true
DragHandler2.itemBegingDragged.SetActive(false);

}


I expect GameControl.control.stone_9 to change it's state to false because I am changing the state of the parameter (Visibility) to false but GameControl.control.stone_9 stays true.










share|improve this question














I want to change the state of a boolean in a function. My function has 4 parameters and the fourth one is a bool which is by default true, but I want to change it's state to false inside the function .



I am calling my methode is following,



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, GameControl.control.stone_9);


GameControl.control.stone_9 is by default true. It should become false once visibilty is set to false ..but that is not happing... stone_9 stays true.



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,bool Visibility)
{
if (DragHandler2.itemBegingDragged.name.Contains(gemColor) && DragHandler2.itemBegingDragged.transform.parent.name == slotColor)
{
Debug.Log(DragHandler2.itemBegingDragged.name);
Visibility=false;
puzzleStuk.SetActive(visibility);
Debug.Log(GameControl.control.stone_9); //true
DragHandler2.itemBegingDragged.SetActive(false);

}


I expect GameControl.control.stone_9 to change it's state to false because I am changing the state of the parameter (Visibility) to false but GameControl.control.stone_9 stays true.







c# methods boolean






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 20:41









MujMuj

185




185













  • Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

    – Frontear
    Jan 1 at 20:47





















  • Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

    – Frontear
    Jan 1 at 20:47



















Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

– Frontear
Jan 1 at 20:47







Pass the boolean using ref, The reason you need to do this is so c# has referenced access, and can modify the actual boolean, not a cloned parameter.

– Frontear
Jan 1 at 20:47














3 Answers
3






active

oldest

votes


















3














I believe you are under the impression that changing the value of an argument will be reflected to the caller (your first code block). This is not the case unless you use ref or out for the parameter/argument. This will work so long as GameControl.control.stone_9 is a field and not a property.



In short, arguments are passed by value unless using those keywords. (for reference types this is true as well, but it's a bit more complicated as what's copied is the reference, not the actual object itself)



Other answers have explained the syntax (needing to use ref both for the argument and the parameter.)



Also, one bit of advice, never capitalize your variables or parameters as when you do most C# readers will see them as properties in the containing class, causing confusion.






share|improve this answer



















  • 1





    Thank you very much sir I actually really didn't know about the ref and out. Appreciated

    – Muj
    Jan 1 at 21:06



















3














If you want to change the value of a variable inside a Method, you should define it by ref:



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,ref bool Visibility)
{
//method stuff
Visibility = false;
}


and then call your method like:



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, ref GameControl.control.stone_9);





share|improve this answer
























  • well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

    – Ashkan Mobayen Khiabani
    Jan 1 at 20:53






  • 1





    Yep, you right, deleted my comment.

    – Dmitry
    Jan 1 at 20:58











  • Thanks ref solved it for me,

    – Muj
    Jan 1 at 21:08



















0














1- In SlotCheck the four parameter should be ref bool Visibility



Or



Inside the if statement you should do this



GameControl.control.stone_9 = false;





share|improve this answer
























  • Thank you, ref solved it for me.

    – Muj
    Jan 1 at 21:07











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%2f53998784%2fboolean-as-methods-parameter-is-not-changing-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I believe you are under the impression that changing the value of an argument will be reflected to the caller (your first code block). This is not the case unless you use ref or out for the parameter/argument. This will work so long as GameControl.control.stone_9 is a field and not a property.



In short, arguments are passed by value unless using those keywords. (for reference types this is true as well, but it's a bit more complicated as what's copied is the reference, not the actual object itself)



Other answers have explained the syntax (needing to use ref both for the argument and the parameter.)



Also, one bit of advice, never capitalize your variables or parameters as when you do most C# readers will see them as properties in the containing class, causing confusion.






share|improve this answer



















  • 1





    Thank you very much sir I actually really didn't know about the ref and out. Appreciated

    – Muj
    Jan 1 at 21:06
















3














I believe you are under the impression that changing the value of an argument will be reflected to the caller (your first code block). This is not the case unless you use ref or out for the parameter/argument. This will work so long as GameControl.control.stone_9 is a field and not a property.



In short, arguments are passed by value unless using those keywords. (for reference types this is true as well, but it's a bit more complicated as what's copied is the reference, not the actual object itself)



Other answers have explained the syntax (needing to use ref both for the argument and the parameter.)



Also, one bit of advice, never capitalize your variables or parameters as when you do most C# readers will see them as properties in the containing class, causing confusion.






share|improve this answer



















  • 1





    Thank you very much sir I actually really didn't know about the ref and out. Appreciated

    – Muj
    Jan 1 at 21:06














3












3








3







I believe you are under the impression that changing the value of an argument will be reflected to the caller (your first code block). This is not the case unless you use ref or out for the parameter/argument. This will work so long as GameControl.control.stone_9 is a field and not a property.



In short, arguments are passed by value unless using those keywords. (for reference types this is true as well, but it's a bit more complicated as what's copied is the reference, not the actual object itself)



Other answers have explained the syntax (needing to use ref both for the argument and the parameter.)



Also, one bit of advice, never capitalize your variables or parameters as when you do most C# readers will see them as properties in the containing class, causing confusion.






share|improve this answer













I believe you are under the impression that changing the value of an argument will be reflected to the caller (your first code block). This is not the case unless you use ref or out for the parameter/argument. This will work so long as GameControl.control.stone_9 is a field and not a property.



In short, arguments are passed by value unless using those keywords. (for reference types this is true as well, but it's a bit more complicated as what's copied is the reference, not the actual object itself)



Other answers have explained the syntax (needing to use ref both for the argument and the parameter.)



Also, one bit of advice, never capitalize your variables or parameters as when you do most C# readers will see them as properties in the containing class, causing confusion.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 20:51









Kirk WollKirk Woll

61.7k16159173




61.7k16159173








  • 1





    Thank you very much sir I actually really didn't know about the ref and out. Appreciated

    – Muj
    Jan 1 at 21:06














  • 1





    Thank you very much sir I actually really didn't know about the ref and out. Appreciated

    – Muj
    Jan 1 at 21:06








1




1





Thank you very much sir I actually really didn't know about the ref and out. Appreciated

– Muj
Jan 1 at 21:06





Thank you very much sir I actually really didn't know about the ref and out. Appreciated

– Muj
Jan 1 at 21:06













3














If you want to change the value of a variable inside a Method, you should define it by ref:



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,ref bool Visibility)
{
//method stuff
Visibility = false;
}


and then call your method like:



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, ref GameControl.control.stone_9);





share|improve this answer
























  • well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

    – Ashkan Mobayen Khiabani
    Jan 1 at 20:53






  • 1





    Yep, you right, deleted my comment.

    – Dmitry
    Jan 1 at 20:58











  • Thanks ref solved it for me,

    – Muj
    Jan 1 at 21:08
















3














If you want to change the value of a variable inside a Method, you should define it by ref:



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,ref bool Visibility)
{
//method stuff
Visibility = false;
}


and then call your method like:



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, ref GameControl.control.stone_9);





share|improve this answer
























  • well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

    – Ashkan Mobayen Khiabani
    Jan 1 at 20:53






  • 1





    Yep, you right, deleted my comment.

    – Dmitry
    Jan 1 at 20:58











  • Thanks ref solved it for me,

    – Muj
    Jan 1 at 21:08














3












3








3







If you want to change the value of a variable inside a Method, you should define it by ref:



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,ref bool Visibility)
{
//method stuff
Visibility = false;
}


and then call your method like:



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, ref GameControl.control.stone_9);





share|improve this answer













If you want to change the value of a variable inside a Method, you should define it by ref:



public void SlotCheck(string gemColor,string slotColor,GameObject puzzleStuk,int scoreGem,ref bool Visibility)
{
//method stuff
Visibility = false;
}


and then call your method like:



SlotCheck("Red", "red_small_c", puzzle_9, GameControl.control.scoreRedGems, ref GameControl.control.stone_9);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 20:46









Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani

20.7k1667119




20.7k1667119













  • well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

    – Ashkan Mobayen Khiabani
    Jan 1 at 20:53






  • 1





    Yep, you right, deleted my comment.

    – Dmitry
    Jan 1 at 20:58











  • Thanks ref solved it for me,

    – Muj
    Jan 1 at 21:08



















  • well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

    – Ashkan Mobayen Khiabani
    Jan 1 at 20:53






  • 1





    Yep, you right, deleted my comment.

    – Dmitry
    Jan 1 at 20:58











  • Thanks ref solved it for me,

    – Muj
    Jan 1 at 21:08

















well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

– Ashkan Mobayen Khiabani
Jan 1 at 20:53





well I just said what you are saying, the variable name does not matter, name the parameter whatever you like

– Ashkan Mobayen Khiabani
Jan 1 at 20:53




1




1





Yep, you right, deleted my comment.

– Dmitry
Jan 1 at 20:58





Yep, you right, deleted my comment.

– Dmitry
Jan 1 at 20:58













Thanks ref solved it for me,

– Muj
Jan 1 at 21:08





Thanks ref solved it for me,

– Muj
Jan 1 at 21:08











0














1- In SlotCheck the four parameter should be ref bool Visibility



Or



Inside the if statement you should do this



GameControl.control.stone_9 = false;





share|improve this answer
























  • Thank you, ref solved it for me.

    – Muj
    Jan 1 at 21:07
















0














1- In SlotCheck the four parameter should be ref bool Visibility



Or



Inside the if statement you should do this



GameControl.control.stone_9 = false;





share|improve this answer
























  • Thank you, ref solved it for me.

    – Muj
    Jan 1 at 21:07














0












0








0







1- In SlotCheck the four parameter should be ref bool Visibility



Or



Inside the if statement you should do this



GameControl.control.stone_9 = false;





share|improve this answer













1- In SlotCheck the four parameter should be ref bool Visibility



Or



Inside the if statement you should do this



GameControl.control.stone_9 = false;






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 20:47









Fredy Adriano Jimenez MartinezFredy Adriano Jimenez Martinez

30417




30417













  • Thank you, ref solved it for me.

    – Muj
    Jan 1 at 21:07



















  • Thank you, ref solved it for me.

    – Muj
    Jan 1 at 21:07

















Thank you, ref solved it for me.

– Muj
Jan 1 at 21:07





Thank you, ref solved it for me.

– Muj
Jan 1 at 21:07


















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%2f53998784%2fboolean-as-methods-parameter-is-not-changing-state%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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$