- Home /
What is the best way to convert a string list to a class list?
I've retrieved WWW data and put it into a string array, removed the delimiter. Now I want to put it into a list an instantiate a prefab. I used linq to make it into a list. Strange thing is, there are three items taken from the database and the count reads as four.
WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php");
yield return VehicleData;
Debug.Log (VehicleData.text);
string TheVehicleData = VehicleData.text;
string[] VehiclesArray = TheVehicleData.Split ('/');
VehicleIndexList = VehiclesArray.ToList();
Debug.Log (VehicleIndexList.Count);
The other issue is an error
Cannot implicitly convert type `System.Collections.Generic.List<string>' to `System.Collections.Generic.List'
I've tried using cast, I've tried using as and the standard ()
VehicleIndexList = (VehicleIndex)VehiclesArray.ToList() as VehicleIndex;
This give the error Cannot convert type `System.Collections.Generic.List' to VehicleIndex'
VehicleIndexList = VehiclesArray.Cast<VehicleIndex>().ToList();
This gives the error InvalidCastException: Cannot cast from source type to destination type. Which means the parameters aren't right
I've also tried using just the array, only I don't know how to instantiate a prefab from an array. I hope someone can help me out of this jam. Thanks!
What exactly are you trying to do? How do you want to convert a string to a class? That doesn't make much sense? Does the constructor of the class take in the string or something? Then you could do something like:
var VehicleIndexList = VehiclesArray.Select(f => new VehicleIndex(f)).ToList();
Can you show us the VehicleIndex class?
Answer by fafase · Dec 11, 2017 at 09:08 PM
First you have one extra because your data most likely looks like:
/data/data/data/
so it cuts it in 4.
You could check if the last is / and trim:
if(data[data.Length - 1] == '/'){ data.TrimEnd('/'); }
Second you should show what type is VehicleIndexList. Sounds like you are not using a generic version of it.
List<string>VehicleIndexList = new List<string>( VehiclesArray);
But if you do not plan on changing the collection, then there is little point in doing so. If the purpose was to remove the last empty entry, using trimming from above will do it.
The compiler doesn't like TrimEnd(). So after changing the array to a list, how do I convert that to a class list which I need to instantiate the prefab?
Found something that the compiler likes: VehiclesArray = VehiclesArray.Where (x => !string.IsNullOrEmpty (x)).ToArray ();
But it's still saying there are four in the length.
TrimEnd happens on the initial string, not on the split strings which is an array. You need to print the content so you know what your splits are.
But do you need the list? You have the collection from the split.
string data= GetDataFromServer();
if(data[data.Length - 1] == '/'){ data.TrimEnd('/'); } // Here parameter was missing...
string[] results = data.Split('/', StringSplitOptions.RemoveEmptyEntries);
foreach(string str in results){ Debug.Log(str); }
What is a class list? Do you mean non-generic ArrayList? You should not use those anymore. They are from the dawn of C# days, only legacy code makes use of those.
It spit out two errors: The best overloaded method match for string.Split(params char[])' has some invalid arguments and cannot convert
System.StringSplitOptions' expression to type `char'
Any thoughts?
The class list has ID, $$anonymous$$ake, $$anonymous$$odel, Year. I want to instantiate the prefab to display those elements.
Your answer
Follow this Question
Related Questions
instantiate problem help? 1 Answer
How to put gameObjects to the list? 4 Answers
Prefabs instantiated from an array are keeping their public int value 1 Answer
Parenting an instantiated prefab. 1 Answer
Array of prefabs 0 Answers