Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by lvictorino · Dec 05, 2015 at 12:35 PM · c#abstractpolymorphism

C# Abstract methods with default parameters

I wrote an abstract class CompositeConnector declaring abstract methods. One of them, DrawContent has a default string parameter.

 public abstract class CompositeConnector : ScriptableObject
 {
 // ... 
     public abstract void DrawContent(GUISkin skin, float zoom_factor, string control_name = "");
 // ...
 }

Other classes inheriting from CompositeConnector also declare a default parameter.

 public class InputTextConnector : CompositeConnector
 {
 // ...
     public override void DrawContent(GUISkin skin, float zoom_factor, string control_name = "")
     {
     // ...
     }
 // ...
 }

My problem comes when I call this method without the latest parameter.

 // Produces the error.
 _input_connector.DrawContent(_skin, _zoom_factor);
 // No error.
 _input_connector.DrawContent(_skin, _zoom_factor, "");

I get an error such as (not at runtime):

Unhandled Exception: System.ArgumentException: Key duplication when adding: Void DrawContent(UnityEngine.GUISkin, Single, System.String)

As you can see if I set the parameter and don't rely with the default value I don't get the error. I tried a lot of various combination but the only solution I found was to not use default parameters.

Is this a known issue? Am I trying to do something wrong here? As it seems to be valid in C# is this coming from Unity?

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

1 Reply

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

Answer by Bunny83 · Dec 05, 2015 at 02:09 PM

Well, i don't get that error in Unity 5.2.1f1. Are you sure:

  • you have the InputTextConnector class in a seperate file called "InputTextConnector.cs"?

  • you create an instance with the CreateInstance method of ScriptableObject?

Apart from that i don't see any reason why specifying two default parameters. Default parameters are just syntactic sugar and are actually implemented different depending on the compiler.

For example if you declare your methods like this;

 public abstract void DrawContent(GUISkin skin, float zoom_factor, string control_name = "base");
 // ...
 public override void DrawContent(GUISkin skin, float zoom_factor, string control_name = "derived")
 {
     Debug.Log("DrawContent: " + zoom_factor + ", " + control_name);
 }

You probably can't guess what the result is in the following cases:

     // in Unity
     InputTextConnector i = ScriptableObject.CreateInstance<InputTextConnector>();
     CompositeConnector c = i;
     i.DrawContent(null, 0f);
     c.DrawContent(null, 0f);

In Unity you will get two times

 "DrawContent: 0, base"
 "DrawContent: 0, base"

Doing the same thing in normal C# (of course removing the ScriptableObject inheritance)

     InputTextConnector i = new InputTextConnector();
     CompositeConnector c = i;
     i.DrawContent(null, 0f);
     c.DrawContent(null, 0f);

In a C# console applciation we get this:

 "DrawContent: 0, derived"
 "DrawContent: 0, base"

Default parameters are just syntactic sugar. The compiler literally adds the default parameter to the call. Now it depends on how the compiler determines the default parameter. C# clearly uses the variable type while Unity's Mono compiler uses the "first declared" default value.

So it's quite dangerous to rely on different default values for different derived classes. You should only specify one default value for the same parameter.

Comment
Add comment · Show 1 · 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 lvictorino · Dec 07, 2015 at 08:41 AM 0
Share

Thanks for your answer!

To answer the first part of your answer: yes, I have a separate file for InputTextConnector and I use CreateInstance. So... I don't know where the problem's from :/ Also not that I'm on Unity 5.2.3f1 (but I don't think the problem comes from here).

About the second part, it was not clear in the code but I don't need different default parameters. Using "" as default would be fine. However when I don't specify a default parameter in the override method I get some errors.

Anyway, thanks again for your long and clear answer.

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

32 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiation Of Abstract Classes 1 Answer

How can i get component as an abstract base class? 1 Answer

Decorator Design Pattern - Not extending object behavior 1 Answer

RPG Picking up items with polymorphism - probably quick :) 1 Answer

Best way to implement scriptable objects and SOLID code when working with objects of same general type, but each have their own set unique attributes? 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