- Home /
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?
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)
Oh, I thought you want your method to work on all kinds of lists...
That was the idea. But since it did not wan to work, then I did not know what top do anymore.
oh ok, then you should try Happy$$anonymous$$oos approach
Answer by HappyMoo · Jan 03, 2014 at 01:47 PM
Does this work?
public static string ConvertToString<T>(this List<T> list)
It does as well, but my list does not output correctly. It appears to be empty.
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,
Also, while we're at it...
foreach (var item in list)
{
if (output != string.Empty) output += ", ";
output += item.ToString();
}
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.
@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.
Your answer
Follow this Question
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