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 mmangual_83 · Jan 03, 2014 at 01:38 PM · c#extension-methods

How to use extension classes

I have a class that I am using as my extension:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class Extensions {
     /// <summary>
 /// Converts a list to a string
 /// </summary>
     public static string ConvertToString(this List<object> list)
     {
         var output = string.Empty;
         foreach (var item in list)
         {
             output += item.ToString() + ",";
         }
         return output;
     }
     
 }

This is inside a folder called General Scripts.

In another folder called GUI Scripts I have a class that is trying to access this class.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Cube_GesturePreference : MonoBehaviour {
     string[] gestureOptions = {   Cube_DemoPhase.selection.ConvertToString(),
                                   Cube_DemoPhase.selection.ConvertToString(),
                                   Cube_DemoPhase.selection.ConvertToString()
                               }; 
     public void Start(){}
     public void Update(){}
 
 }

But it gives me this error:

 Error    2    Instance argument: cannot convert from 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<object>'
 Error    1    'System.Collections.Generic.List<string>' does not contain a definition for 'ConvertToString' and the best extension method overload 'Extensions.ConvertToString(System.Collections.Generic.List<object>)' has some invalid arguments

 

Can anyone help me figure out why my extension method is not showing up when I try to access it?

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
1
Best Answer

Answer by robhuhn · Jan 03, 2014 at 01:42 PM

Looks like Cube_DemoPhase.selection is of type List<string> but the extension method is for List<object>. Just change that to

 public static string ConvertToString(this List<string> list)
Comment
Add comment · Show 7 · 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 mmangual_83 · Jan 03, 2014 at 01:44 PM 0
Share

Wow, did not notice that! Thank you!

avatar image HappyMoo · Jan 03, 2014 at 01:48 PM 0
Share

Oh, I thought you want your method to work on all kinds of lists...

avatar image mmangual_83 · Jan 03, 2014 at 01:59 PM 0
Share

That was the idea. But since it did not wan to work, then I did not know what top do anymore.

avatar image robhuhn · Jan 03, 2014 at 02:01 PM 0
Share

oh ok, then you should try Happy$$anonymous$$oos approach

avatar image mmangual_83 · Jan 03, 2014 at 02:07 PM 0
Share

Yep, I just did.

Show more comments
avatar image
3

Answer by HappyMoo · Jan 03, 2014 at 01:47 PM

Does this work?

 public static string ConvertToString<T>(this List<T> list)
Comment
Add comment · Show 7 · 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 mmangual_83 · Jan 03, 2014 at 02:03 PM 0
Share

It does as well, but my list does not output correctly. It appears to be empty.

avatar image HappyMoo · Jan 03, 2014 at 02:13 PM 0
Share

That's strange, because it works for me...

     public static class Ex
     {
         public static string ConvertToString<T>(this List<T> list)
         {
             var output = string.Empty;
             foreach (var item in list)
             {
                 output += item.ToString() + ",";
             }
             return output;
         }
     }
 
 ....
 ....
 
         [Context$$anonymous$$enu("test something")]
         public void test()
         {
             List<string> str = new List<string>();
             str.Add("aaa");
             str.Add("bbb");
 
             Debug.Log(str.ConvertToString());
         }


This prints out aaa,bbb,

avatar image HappyMoo · Jan 03, 2014 at 02:16 PM 0
Share

Also, while we're at it...

 foreach (var item in list)
 {
   if (output != string.Empty) output += ", ";
   output += item.ToString();
 }
avatar image Hoeloe · Jan 03, 2014 at 02:33 PM 2
Share

This is the better solution. It's virtually identical to the other one, except that this uses generics, allowing for lists of any type rather than just of type string - that is the point of generics.

avatar image mmangual_83 · Jan 03, 2014 at 07:03 PM 0
Share

@Happy$$anonymous$$oo I guess it works when everything is in the same class. Because I am adding the contents of the new list in one class then I am using list.ConvertToString() in another class and when I place debug.log and see what the selection list has its empty and yet when I do debug log in the class before this one it it shows the contents.

Show more comments

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

21 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

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Distribute terrain in zones 3 Answers

More ways to use Trail Renderer 1 Answer

Restricition of Objects. 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