简体   繁体   中英

Converting a string array from WWW to a class list in Unity3d

I am having some trouble converting a WWW string array to a class list. What I want to do is instantiate a prefab which has a number of text elements in it. Here is the base class

public class VehicleIndex : MonoBehaviour 
{
    public string ID{ get; set;}
    public string Make{ get; set;}
    public string Model{ get; set;}
    public string Year{ get; set;}
    public string Mileage{ get; set;}


    public VehicleIndex(string id, string make, string model, string year, string mileage)
    {
        this.ID = id;
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Mileage = mileage;
    }



}

Here is the function that gets the WWW data from the Mysql database

    public List<VehicleIndex> VehicleIndexList = new List<VehicleIndex> ();
    public string[] VehicleStringArray;

    public IEnumerator GetAllVehicles()
    {
        WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php"); 
        yield return VehicleData;

        Debug.Log (VehicleData.text);
        string TheVehicleData = VehicleData.text;
                              The information comes back just fine.     
        if(TheVehicleData[TheVehicleData.Length - 1] == '/')
        { 
        TheVehicleData.TrimEnd('/');      Here I'm removing the delimiter
        }
        string[] results = TheVehicleData.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
      for (int i = 0; i < results.Length; i++) 
       {
        Debug.Log (results[i]);
       }
        VehicleStringArray = results; The results go back into an array

        VehicleIndexList = VehicleStringArray.Select(sArr => new VehicleIndex 
        {
            ID=VehicleStringArray[0],
            Make=VehicleStringArray[1],
            Model=VehicleStringArray[2],
            Year=VehicleStringArray[3],
            Mileage=VehicleStringArray[4]
        }).ToList();


    }

It looks like it should work only it gives the error: The type VehicleIndex' does not contain a constructor that takes 0' arguments What am I missing? Do I need to add something to the class? A list? an array? Thanks for the help! I REALLY appreciate it.

ADDITIONAL- Added an object initializer

public VehicleIndex Vi;

Then changed the lambda expression to

VehicleIndexList = VehicleStringArray.Select(sArr => new VehicleIndex 
    {
        Vi.ID==VehicleStringArray[0],
        Vi.Make==VehicleStringArray[1],
        Vi.Model==VehicleStringArray[2],
        Vi.Year==VehicleStringArray[3],
        Vi.Mileage==VehicleStringArray[4]
    }).ToList();

The only way to avoid the red line was to use the double equal. I'm not sure if this is right, since it is still not working, I'm guessing not.

You have a constructor that takes all of the properties as arguments; just use that instead of the object initializer:

    VehicleIndexList = VehicleStringArray.Select(sArr => new VehicleIndex 
    (
        VehicleStringArray[0],
        VehicleStringArray[1],
        VehicleStringArray[2],
        VehicleStringArray[3],
        VehicleStringArray[4]
    )).ToList();

Note, however, that contructors typically only need paramaters that are required for the object to function properly. If the property values are not required, there's nothing wrong with removing the constructor and using initialization syntax ( new VehicleIndex { } ) instead.

This is basically the same thing as JavaScript's join()

String.Join Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM