0

I'm trying to save the angle values where I have two line segments intersecting. I made AngleOne and AngleTwo global variables so that I can refer to them in my SaveElements() method.

When the custom event onDrawingSecondLine is thrown, it checks if the line segments are intersecting and if so, set the angles.

Then I am looping through each line in my lines list and assigning the angle values to be saved in the db.

Here's what I have so far:

string angleOne = "";
string angleTwo = "";

public void onDrawingSecondLine (object sender, LineDrawingEvent e) {

    if (LinesIntersect) {

    angleOne =  CameraObject.GetComponent<DrawLines>().GetAngle(); 
    angleTwo = (180.00 - Double.Parse(angleOne)).ToString();

    intersectingLineOne.lineParams.displayName = "  " + angleTwo + "(" + angleOne + ")";

    string angle = intersectingLineOne.lineParams.displayName;
    string col = Color.cyan.ToString();
    SaveLinesWithAngles(e.lineName, angle, col);
    }
    else
    {
        intersectingLineOne.lineParams.displayName = " ";
        angleOne = "";
        angleTwo = "";
        string angle = intersectingLineOne.lineParams.displayName;
        string col = Color.cyan.ToString();
        SaveLinesWithAngles(e.lineName, angle, col);
    }

}


public void SaveLinesWithAngles(string lineName, string angle, string col)
{
    PlanningParams p;
    List<PlanningParams> planningParamsList = new List<PlanningParams>();
    int lineNumber = 1;
    p = SaveElements();
    if (p != null)
    {
        planningParamsList.Add(p);
    }

    string type = GetLineType(lineName);
    string s = XmlSerializeUtility.ToXml(planningParamsList);
    lineNumber = currentIntersectingLine; 
    Application.ExternalCall("BrowserSide.Planning.SaveToSession", s, ViewType, angle, "LinesWithAngles", col, lineNumber);
}

private PlanningParams SaveElements() {

    PlanningParams planningParams = null;
    planningParams = new PlanningParams();

    foreach (Line line in CameraObject.GetComponent<DrawLines>().GetLinesWithAngles()) 
    {
        LinesWithAnglesData linesWithAnglesData = new LinesWithAnglesData();
        linesWithAnglesData.angleValue1 = float.Parse(angleOne);
        linesWithANglesData.angleValue2 = float.Parse(angleTwo);
        planningParams.LinesWithAnglesData.Add(linesWithAngleData);
    }

}

public List<Line> GetLinesWithAngles()
    {
        List<Line> linesWithAngles = new List<Line>();
        int i = 0;
        if (lines == null)
        {
            return null;
        }
        for (i = 0; i < lines.Count; i++)
        {
            if (lines[i].elementType == ElementType.Line && lines[i].lineParams.lineName.Contains("IntersectingLine"))
            {
                linesWithAngles.Add(lines[i]);
            }
        }

        return linesWithAngles;
    }

Why are the values of Angle One and Angle Two always updated to the new value and don't keep their current values? How can I make it so the values are not updated to new value?

EDIT:

Seems like a similar issue like this guy had: Why does adding a new value to list<> overwrite previous values in the list<>

But I do have my instance inside my foreach loop. Inside the foreach loop, I noticed that the old value is overriden with the new value.

Community
  • 1
  • 1
Kala J
  • 2,040
  • 4
  • 45
  • 85
  • @Sybren: Well, not an `ArrayList` - why would you want to use a non-generic collection? – Jon Skeet Apr 22 '15 at 20:44
  • 1
    It would really help if you would show a short but *complete* program demonstrating the problem. We can't really tell what's going on from just this snippet... – Jon Skeet Apr 22 '15 at 20:44
  • Please show a working set of code, what you have above won't compile, angleOne is not the same as AngleOne. Also you can't float.Parse AngleOne if its set to "Foo". – Ron Beyer Apr 22 '15 at 20:46
  • See update. Sorry about that! – Kala J Apr 22 '15 at 20:52
  • [1.] Find all usages of `angleOne` [2.] See which usages set the variable [3.] Set breakpoints on those lines [4.] Run with the debugger and see how they are being called. – Jeff B Apr 22 '15 at 21:46
  • 1) omg I didn't realize Jon Skeet saw this question! 2) I will double check the breakpoints, it's hard to test this because it's in Unity3D and the controls are wired up in JavaScript :S However, I can use Logs. 3) The reason why I only put the code above is because I thought it could be an issue with the way I'm setting up the angle values as global? – Kala J Apr 23 '15 at 01:23
  • @JonSkeet , I updated my question. Using breakpoints I find the issue in the foreach loop but I'm still confused why the previous value is overriden with the new value? – Kala J Apr 23 '15 at 01:57
  • 2
    The code you posted doesn't make any sense, never mind is it [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably reproduces the problem. In your `SaveElements()` method, you've got a loop in which you create an object, set a couple of fields (or properties?), and then **do nothing at all with the object**. What's the point of that? In `GetLinesWithAngles()`, you seem to be copying elements from `lines` to `linesWithAngles`, but assuming the code compiles as-is, `lines` elements must all be of type `Line`, so how'd any ever _not_ be `ElementType.Line`? – Peter Duniho Apr 23 '15 at 05:01
  • @KalaJ - did you follow the link in Peter's comment? It describes what you need to do to create a MCVE, which is what would actually help here. – Damien_The_Unbeliever Apr 23 '15 at 13:42

0 Answers0