Boolean as methods parameter is not changing state
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
add a comment |
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
Pass the boolean usingref
, 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
add a comment |
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
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
c# methods boolean
asked Jan 1 at 20:41
MujMuj
185
185
Pass the boolean usingref
, 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
add a comment |
Pass the boolean usingref
, 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
add a comment |
3 Answers
3
active
oldest
votes
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.
1
Thank you very much sir I actually really didn't know about the ref and out. Appreciated
– Muj
Jan 1 at 21:06
add a comment |
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);
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
add a comment |
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;
Thank you, ref solved it for me.
– Muj
Jan 1 at 21:07
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
Thank you very much sir I actually really didn't know about the ref and out. Appreciated
– Muj
Jan 1 at 21:06
add a comment |
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.
1
Thank you very much sir I actually really didn't know about the ref and out. Appreciated
– Muj
Jan 1 at 21:06
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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;
Thank you, ref solved it for me.
– Muj
Jan 1 at 21:07
add a comment |
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;
Thank you, ref solved it for me.
– Muj
Jan 1 at 21:07
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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