Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gumboots · Mar 26, 2015 at 05:36 AM · methodparametersmerge

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gumboots · Mar 26, 2015 at 10:05 PM 0
Share

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?

avatar image Huacanacha · Mar 26, 2015 at 10:30 PM 0
Share

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
avatar image Huacanacha · Mar 26, 2015 at 10:34 PM 0
Share

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?

avatar image gumboots · Mar 26, 2015 at 10:37 PM 0
Share

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!

avatar image Huacanacha · Mar 26, 2015 at 10:52 PM 0
Share

Ok I added that helper function method back in for completeness.

Glad I could help!

Show more comments
avatar image
0

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;
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gumboots · Mar 26, 2015 at 06:50 AM 0
Share

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?

avatar image Hrungdak · Mar 26, 2015 at 07:29 AM 0
Share

I see no way in this direction. Perhaps if you would describe the problem in a more detailed manner.

avatar image Huacanacha · Mar 26, 2015 at 07:30 AM 1
Share

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.

avatar image Hrungdak · Mar 26, 2015 at 07:53 AM 0
Share

Thanks for your comment, Huacanacha. You are absolutely right.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges