- Home /
C# - parsed strings not changing in function
I have a function, for the simplicity and density of this question, lets say it is
void DoSomething (int number, string word) {
number = 10;
word = "hello";
}
when i do this, the int argument variable is changed, but the string argument never gets changed. I've tracked it down to this function. Its been happening to me a lot, but i usually find a work-around. Is it normal that strings don't get changed from being parsed? if so.... can someone explain why?
I think you need to show us a bit more. In the code you've posted, clearly both the variables will change when you set them, and there's nothing that could suggest otherwise since they're local and thus disappear as soon as the function finishes.
Answer by NoseKills · Sep 25, 2016 at 09:05 PM
Either I don't understand the question or the example is flawed.
Int is a value type and string acts like a value type in this respect so in this example you can't change either of the ORIGINAL variables by passing them into a method like this.
void Awake() {
int myNumber = 1;
string myString = "goodbye";
DoSomething(myNumber, myString);
Debug.Log(myNumber + ", " + myString);
// Logs out "1, goodbye"
}
void DoSomething (int number, string word) {
number = 10;
word = "hello";
}
When you call a method with value type parameters, the value of the passed in variables gets copied and passed into the function, so inside the function you are changing just a copy of the original variables.
When you call a method with a reference type parameter such as GameObject, a reference to the original GameObject is passed into the function, so if you for example change the name of the GameObject, you'll see the change wherever you have a reference to the same GameObject
String is actually a reference type but in this respect it acts like a value type.
This is actually not a matter of value type vs reference type but a matter of passing by value vs passing by reference. Both examples you've given are actually passed by value which is the default for all method calls unless specified otherwise.
It is true that you can change a reference type object through a copied reference, but you can't replace the reference itself since the "reference" is the "value" of a reference type variable.
That's what the keywords "ref" and "out" are good for. They actually pass the variable by reference, no matter what type the variable has:
void DoSomething(ref int number, ref string word)
{
number = 10;
word = "hello";
}
void Awake() {
int myNumber = 1;
string myString = "goodbye";
DoSomething(ref myNumber, ref myString);
Debug.Log(myNumber + ", " + myString);
// Logs out "10, hello"
}
This is very good to know, thank you. I was actually passing an int array and a string variable, i'm guessing arrays are reference types and not value types, therefore my array was getting changed but my string wasn't. Thanks, (I didn't post my whole slab of code because it was literally a 300 line function for randomly generating item stats, the string was just the name of the item.)
Ah, yes, arrays are passed by reference, that explains it.
For future reference, when showing people the code that's causing a problem, it's best not to simplify it too much. Changing the type of a variable that you're asking about (an int is quite different to an arrays of int) is too much. It could have prevented anyone from diagnosing the issue.
What's puzzling me is the OP's saying that things are working differently for the int
compared to the string
. $$anonymous$$aybe that's just a mistaken observation.
Yes, for sure ^^. As i said the variable type doesn't matter. If the parameter is not a "ref" parameter, a method can't change the variable itself that is passed to the method. Llike Nose$$anonymous$$ills said, parameters are local variables to the method and only contain a copy of the "value" of the passed variable.
Further more non-ref-parameters can be literal values (constants). So the original method could be called like this: DoSomething(5, "FooBar");
Here it seems quite clear that the value is just copied into the local parameter variable when the method is called.
A ref parameter on the other hand always needs to be a variable. "ref" parameters don't pass the value of a variable along, but a managed pointer to the actual memory where the variable is located. So non-ref-parameters always pass along the "content" or a variable while ref-parameters allow to pass the variable itself.