internal override XElement CreateXml()
        {
            XElement xRoot;

            if (Set == false)
            {
                xRoot = new XElement(XmlConstants.UserVariable, Name);
                Set   = true;
                XmlParserTempProjectHelper.currentVariableNum++;

                ObjectNum   = XmlParserTempProjectHelper.currentObjectNum;
                ScriptNum   = XmlParserTempProjectHelper.currentScriptNum;
                BrickNum    = XmlParserTempProjectHelper.currentBrickNum;
                VariableNum = XmlParserTempProjectHelper.currentVariableNum;
            }
            else if (Set)
            {
                XmlUserVariableReference userVariableReference = new XmlUserVariableReference();
                userVariableReference.UserVariable = this;
                userVariableReference.LoadReference();
                xRoot = userVariableReference.CreateXml();
            }
            else
            {
                xRoot = new XElement("XmlUserVarialbe.cs Error");
            }

            return(xRoot);
        }
 internal override void LoadFromXml(XElement xRoot)
 {
     if(xRoot.HasAttribute(XmlConstants.Reference))
     {
         XmlUserVariableReference bufferReference = new XmlUserVariableReference(xRoot);            
         Name = bufferReference.UserVariable.Name;
     }
     else
         Name = xRoot.Value;
 }
 internal override void LoadFromXml(XElement xRoot)
 {
     if (xRoot.HasAttribute(XmlConstants.Reference))
     {
         XmlUserVariableReference bufferReference = new XmlUserVariableReference(xRoot);
         Name = bufferReference.UserVariable.Name;
     }
     else
     {
         Name = xRoot.Value;
     }
 }
        private static XmlObjectNode GetUserVariableObject(XmlUserVariableReference xmlUserVariableReference, string reference)
        {
            XElement currentElement = xmlUserVariableReference._xRoot;
            string[] array = reference.Split(new char[] { '/'}, StringSplitOptions.RemoveEmptyEntries);

            var matchQuery = from word in array
                             where word.ToLowerInvariant() == ".."
                             select word;

            int stepsDown = matchQuery.Count();

            for(int i=0; i < stepsDown; i++)
            {
                currentElement = currentElement.Parent;
            }

            for(int i=stepsDown; i<array.Length; i++)
            {
                //TODO: throws exception
                string type = array[i].Split('[')[0];
                int number = GetElementNumber(array[i]);

                currentElement = currentElement.Elements(type).ElementAt(number);
            }

            return new XmlUserVariable(currentElement);
        }
        private static string GetVariableReferenceString(XmlUserVariableReference xmlUserVariableReference)
        {
            XmlUserVariable var = xmlUserVariableReference.UserVariable;
            if(XmlParserTempProjectHelper.inObjectVarList)
                return GetStepDownString(5) + XmlConstants.ObjectList + "/"
                    + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                    + XmlConstants.ScriptList + "/"
                    + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                    + XmlConstants.BrickList + "/"
                    + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                    + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);

            else if (XmlParserTempProjectHelper.inProgramVarList)
                return GetStepDownString(3) + XmlConstants.ObjectList + "/"
                    + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                    + XmlConstants.ScriptList + "/"
                    + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                    + XmlConstants.BrickList + "/"
                    + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                    + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);

            else if (xmlUserVariableReference.UserVariable.ObjectNum != XmlParserTempProjectHelper.currentObjectNum)
                return GetStepDownString(6) + XmlConstants.Object + GetReferenceNumeration(var.ObjectNum) + "/"
                    + XmlConstants.ScriptList + "/"
                    + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                    + XmlConstants.BrickList + "/" + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                    + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);

            else if (xmlUserVariableReference.UserVariable.ScriptNum != XmlParserTempProjectHelper.currentScriptNum)
                return GetStepDownString(4) + XmlConstants.Script + GetReferenceNumeration(var.ScriptNum) + "/"
                    + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                    + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);

            else if (xmlUserVariableReference.UserVariable.BrickNum != XmlParserTempProjectHelper.currentBrickNum)
                return GetStepDownString(2) + XmlConstants.Brick + GetReferenceNumeration(var.BrickNum) + "/"
                    + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);

            else if (xmlUserVariableReference.UserVariable.VariableNum != XmlParserTempProjectHelper.currentVariableNum)
                return GetStepDownString(1) + XmlConstants.UserVariable + GetReferenceNumeration(var.VariableNum);
            else
                return "the ReferenceHelper is not working properly!"; 
        }
        public void XmlObjectUservariableReferenceTest()
        {
            XmlProgram programm = new XmlProgram()
            {
                SpriteList = new XmlSpriteList()
                {
                    Sprites = new List<XmlSprite>(),
                },
                VariableList = new XmlVariableList()
                {
                    ObjectVariableList = new XmlObjectVariableList()
                    {

                    }
                }
            };

            TextReader sr = new StringReader("<objectList>" +
                                            "  <object name=\"Hintergrund\">" +
                                            "  </object>" +
                                            "  <object name=\"FlaPacMan\">" +
                                            "  <scriptList>" +
                                            "	<script type=\"BroadcastScript\">" +
                                            "     <brickList>" +
                                            "		<brick type=\"SetVariableBrick\">" +
                                            "            <formulaList>" +
                                            "              <formula category=\"VARIABLE\">" +
                                            "                <type>NUMBER</type>" +
                                            "                <value>1</value>" +
                                            "              </formula>" +
                                            "            </formulaList>" +
                                            "            <userVariable>paclives</userVariable>" +
                                            "          </brick>" +
                                            "          <brick type=\"SetVariableBrick\">" +
                                            "            <formulaList>" +
                                            "              <formula category=\"VARIABLE\">" +
                                            "                <type>NUMBER</type>" +
                                            "                <value>0.0</value>" +
                                            "              </formula>" +
                                            "            </formulaList>" +
                                            "            <userVariable>change anim</userVariable>" +
                                            "          </brick>" +
                                            "          <brick type=\"SetVariableBrick\">" +
                                            "            <formulaList>" +
                                            "              <formula category=\"VARIABLE\">" +
                                            "                <type>NUMBER</type>" +
                                            "                <value>1</value>" +
                                            "              </formula>" +
                                            "            </formulaList>" +
                                            "            <userVariable>animframe</userVariable>" +
                                            "          </brick>" +
                                            "       </brickList>" +
                                            "     </script>" +
                                            "   </scriptList>" +
                                            "  </object>" +
                                            "  <object name =\"handmade referenceobject\">" +
                                            "  <scriptList>" +
                                            "	<script type=\"BroadcastScript\">" +
                                            "     <brickList>" +
                                            "       <brick type =\"ChangeVariableBrick\">" +
                                            "            <formulaList>" +
                                            "              <formula category=\"VARIABLE_CHANGE\">" +
                                            "                <type>NUMBER</type>" +
                                            "                <value>7</value>" +
                                            "              </formula>" +
                                            "            </formulaList>" +
                                            "            <userVariable reference=\"../../../../../../object[2]/scriptList/script/brickList/brick[3]/userVariable\" />" +
                                            "       </brick>" +
                                            "      </brickList > " +
                                            "     </script>" +
                                            "   </scriptList>" +
                                            "  </object>" + 
                                            "</objectList>");
            var objectList = XElement.Load(sr);

            programm.SpriteList.LoadFromXml(objectList);
            
            XmlParserTempProjectHelper.Program = programm;

            //change these 3 to get differnt deep references as you are specifying the "iterator" in the imaginary XML-Docucment
            XmlParserTempProjectHelper.currentObjectNum = 3; 
            XmlParserTempProjectHelper.currentBrickNum = 1;
            XmlParserTempProjectHelper.currentVariableNum = 1;

            //the variable you wanna reference to from the position of the "iterator"  - ! it has to exist in the above XML-snippet but you can also modify this if you know what you are doing! 
            var userVariableRefTest = new XmlUserVariableReference(); 
            userVariableRefTest.UserVariable = ((XmlSetVariableBrick) programm.SpriteList.Sprites[1].Scripts.Scripts[0].Bricks.Bricks[2]).UserVariable;
            userVariableRefTest.UserVariable.ObjectNum = 2;
            userVariableRefTest.UserVariable.ScriptNum = 1;
            userVariableRefTest.UserVariable.BrickNum = 3;
            userVariableRefTest.UserVariable.VariableNum = 1;

            String testString = userVariableRefTest.CreateXml().ToString();

            //string of variable reference from xml-snippet
            //be in mind to adapt the deep accordingly to your changes above
            //if you change the refence be in mind that in the Catrobat XML language (at leats until 0.94) counting starts at 1 not at 0 and [1]'s are not shown/written 
            var referenceXElement = objectList.Descendants(XmlConstants.UserVariable).Where(uservar => uservar.HasAttributes).First();
            String referenceString = referenceXElement.ToString();//should be "<userVariable reference=\"../../../../../../object[2]/scriptList/script/brickList/brick[3]/userVariable\" />";
            Assert.AreEqual(referenceString, testString, "XmlObjectUservariableReferenceTest failed at the CreateXML part");

            var userVariableRefLoaded = new XmlUserVariableReference();
            userVariableRefLoaded.LoadFromXml(referenceXElement);
            userVariableRefLoaded.UserVariable.ObjectNum = 2;
            userVariableRefLoaded.UserVariable.ScriptNum = 1;
            userVariableRefLoaded.UserVariable.BrickNum = 3;
            userVariableRefLoaded.UserVariable.VariableNum = 1;

            Assert.AreEqual(userVariableRefLoaded.UserVariable, userVariableRefTest.UserVariable, "XmlObjectUservariableReferenceTest failed at the LoadXML part");
            


        }
        internal override XElement CreateXml()
        {
            XElement xRoot;

            if (Set == false)
            {
                xRoot = new XElement(XmlConstants.UserVariable, Name);
                Set = true;
                XmlParserTempProjectHelper.currentVariableNum++;

                ObjectNum = XmlParserTempProjectHelper.currentObjectNum;
                ScriptNum = XmlParserTempProjectHelper.currentScriptNum;
                BrickNum = XmlParserTempProjectHelper.currentBrickNum;
                VariableNum = XmlParserTempProjectHelper.currentVariableNum;

            }
            else if(Set)
            {
                XmlUserVariableReference userVariableReference = new XmlUserVariableReference();
                userVariableReference.UserVariable = this;
                userVariableReference.LoadReference();
                xRoot = userVariableReference.CreateXml();
            }
            else
                xRoot = new XElement("XmlUserVarialbe.cs Error");

            return xRoot;
        }