- Home /
Generic Variable Parameters
Hi there,
I'm merging two instances of a custom class, and currently I go through every variable like so (There's many variables.):
if (c.characterPortraitWidth != null) { characterPortraitWidth = c.characterPortraitWidth; }
if (c.characterPortraitHeight != null) { characterPortraitHeight = c.characterPortraitHeight; }
etc.
I was wondering if there's a way to make a function along the lines of:
void MergeVariable(var newValue, var originalValue){
if(newValue != null) { originalValue = newValue; }
}
Any help appreciated!
Answer by Huacanacha · Mar 26, 2015 at 07:52 AM
Use a ref
parameter. This passes the variable by reference so you can change it's assignment. You'll have to use nullable types such as float?
if you want to check for a value type being null.
void AssignIfNotNull<T>(T? newValue, ref T receiver) where T : struct {
receiver = newValue ?? receiver;
}
Then call it like this:
float original = 3;
float? newValue = 7;
AssignIfNotNull(newValue, ref original); // original is set to 7
original = 3;
newValue = null;
AssignIfNotNull(newValue, ref original); // original stays 3
If you have classes rather than value types it's even easier as you could use the null coalescing operator, or choose to use a helper function:
// Direct way
receiver = newObj ?? receiver;
// OR you can use a function
void AssignIfNotNull<T>(T newValue, ref T receiver) where T : class {
receiver = newValue ?? receiver;
}
This will cause receiver and newObj to point to the same object if the assignment is successful.
Thanks for this! The variables I'm using are my own custom class, so I end up with this error:
The type `X$$anonymous$$LSimpleInt' must be a non-nullable value type in order to use it as type parameter `T' in the generic type or method
Is there a constraint I can put on that will let me use this function with my class rather than something like a float?
I updated the answer with method to handle classes. It's cleaner than nullable types as both parameters are the same type.
If you just want to assign the value encapsulated by your X$$anonymous$$LSimpleInt class it's even easier:
X$$anonymous$$LSimpleInt a = ...;
X$$anonymous$$LSimpleInt b = ...;
a.value = b.value; // The value of a now matches b, but they are still two different objects
If they are objects you can use the null coalescing operator directly. I just updated my answer when I realised the method is unnecessary.
Are you saying that the null coalescing operator isn't working for you?
Oh, I liked the method you posted, it worked perfectly! I know there's a bit of overhead using a function, but this only runs once and I find the code much cleaner to look at only writing each variable name once. (They're very long variable names, e.g. menuButtonPromptDistanceBetweenGraphicAndText)
But changing the constraint to being a class mad the null coalescing operator work perfectly fine!
Ok I added that helper function method back in for completeness.
Glad I could help!
Answer by Hrungdak · Mar 26, 2015 at 06:39 AM
In C# you can make use of some operators:
// null-coalescing operator:
// as Huacanacha said in comment, this will only work if newVar is nullable.
// returns oldvar, if oldvar != null, else returns the value after the ??, 15 in this case:
float newVar = oldvar ?? 15;
// conditional operator
// if oldvar is null, newVar gets the value 1, else newvar gets the value of oldvar.
float newVar = oldvar == null ? 1 : oldvar;
Thanks for these. I knew about the conditional operator, but the null-coalescing looks useful. But there's no way to do this in a function that I can pass variable references to? So I only have to have the null-coalescing operator written once?
I see no way in this direction. Perhaps if you would describe the problem in a more detailed manner.
Note that this code won't compile unless oldvar is a nullable float, i.e. float? oldvar;
. Value types can't be compared to null.
Thanks for your comment, Huacanacha. You are absolutely right.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why cant I see more than one perameter in gameobjects 1 Answer
I basically don't understand how to use the AddTorque method and its parameters. 0 Answers
Inventroy Problem- Using different parameters in inherited classes 1 Answer
Call a method by string with parameters 0 Answers
'NullReferenceException: Object reference not set of an object' When calling a method. 1 Answer