c# Can't add to a list when list is within a list of classes ??

  • Thread starter Thread starter dal
  • Start date Start date

dal

dal

Associate
Joined
10 Sep 2005
Posts
905
Location
Lincolnshire
Hi all,
The title might look a bit confusing so I'll post the relevant code :

Code:
public class Taxiways
{
    public string Name;
    public Vector3 EndPosition;
    public List<float> ControlPointInterpolation = new List<float>();


}

// IN ANOTHER CLASS
public List <Taxiways> TaxiwaysList = new List<Taxiways>();




void InitialiseNewTaxiClass (CurvySpline NewSpline)
    {
        Vector3 endpos = NewSpline.LastVisibleControlPoint.transform.position;

        List <float> Templist = new List<float> ();
        Templist.Add (NewSpline.Length);

        TaxiwaysList.Add (new Taxiways {
            Name = NewSpline.name,
            EndPosition = endpos,
            ControlPointInterpolation.add(NewSpline.Length)   // This line gives an ERROR
                                                              

        });



    }

I get : error CS0747: Inconsistent `object initializer' member declaration

I can get around it by changing the line that gives the error to
Code:
ControlPointInterpolation = Templist.ToList()

Then I have to mess around making a temporary list, just wondered if there was another way.

Cheers
 
Not sure your default in the class will work





Code:
private class Taxiways
{
    public string Name { get; set; }
    public int EndPosition { get; set; }
    public List<float> ControlPointInterpolation { get; set; }
}


List <Taxiways> TaxiwaysList = new List<Taxiways>();
           var x  = new Taxiways
            {
                Name = "test",
                EndPosition = 1,
                ControlPointInterpolation = new List<float> { NewSpline.Length }
            };

TaxiwaysList.Add(x);  // can combine these into less lines
 
@chroniclard is correct although I don't think the default value matters, you can't call a method (.Add) when you are initialising the property on the Taxiways object

Yes it was because I was trying to call a method when initialising the property. And Chroniclard code did work so thanks for the help guys.
I was also able to get it working by adding to the list of the newly created object after i created it :

Code:
Taxiways T = TaxiwaysList [TaxiwaysList.Count-1];
        T.ControlPointInterpolation.Add (NewSpline.Length);
 
Back
Top Bottom