0 Replies Latest reply: Aug 30, 2007 10:13 PM by jfillman RSS

    There Must Be An Easier Way!?!?!

    jfillman Community Member
      Maybe it's the lack of good sleep for the past few weeks, or just staring at the code to long, but it seems like there should be an easier way to do this, but it escapes me. Below is sample code. I have 48 initial IF statements, with nested IF statements. The first outter IF statement has 47 nested IF statements, the second outter IF statement has 46 nested IF statements, etc. I've abbreviated the code for readability.

      if (thisItem.p0 == true){
      P00.addItem(thisItem);

      if (thisItem.ItemSlots >= 1){
      thisItem.p1 = true;
      P01.addItem(thisItem);

      if (thisItem.ItemSlots >= 2){
      thisItem.p2 = true;
      P02.addItem(thisItem);

      if (thisItem.ItemSlots >= 3){
      thisItem.p3 = true;
      P03.addItem(thisItem);

      if (thisItem.ItemSlots >= 4){
      thisItem.p4 = true;
      P04.addItem(thisItem);

      if (thisItem.ItemSlots >= 5){
      thisItem.p5 = true;
      P05.addItem(thisItem);
      }
      }
      }
      }
      }
      }
      if (thisItem.p1 == true){
      P01.addItem(thisItem);

      if (thisItem.ItemSlots >= 1){
      thisItem.p2 = true;
      P02.addItem(thisItem);

      if (thisItem.ItemSlots >= 2){
      thisItem.p3 = true;
      P03.addItem(thisItem);

      if (thisItem.ItemSlots >= 3){
      thisItem.p4 = true;
      P04.addItem(thisItem);

      if (thisItem.ItemSlots >= 4){
      thisItem.p5 = true;
      P05.addItem(thisItem);
      }
      }
      }
      }
      }
        • 1. Re: There Must Be An Easier Way!?!?!
          ctzn99 Community Member
          Well i can think of a few things that would help but without seeing more of your code it's hard to make suggestions.

          For example: if P01 and P02, etc. are derived (somewhere along the line) from DisplayObject and you know they all have the "addItem" method then you could do something like:

          // code
          var numSlots:String = thisItem.ItemSlots.toString();
          this["P0" + numSlots"].addItem(thisItem);
          // end code

          this "this" keyword represents the application and you can use the applications indexer and pass in a string parameter to get a handle to a variable within the application scope that has that "id". You would then have to make sure the objects P00 or P01 (etc.) had their id property set to P00 or P01 (etc.) respectively. I think that would take care of your inner if statements and get you thinking about how to get rid of your outer if statements

          If that doesn't make sense i can try and make an example for you.

          Good Luck,
          Zach
          • 2. There Must Be An Easier Way!?!?!
            levancho Community Member
            this -------->

            if (thisItem.ItemSlots >= 1){
            thisItem.p1 = true;
            P01.addItem(thisItem);

            if (thisItem.ItemSlots >= 2){
            thisItem.p2 = true;
            P02.addItem(thisItem);

            if (thisItem.ItemSlots >= 3){
            thisItem.p3 = true;
            P03.addItem(thisItem);

            if (thisItem.ItemSlots >= 4){
            thisItem.p4 = true;
            P04.addItem(thisItem);

            if (thisItem.ItemSlots >= 5){
            thisItem.p5 = true;
            P05.addItem(thisItem);

            ------------------------

            and this -------->

            for(var i:int=0;i<=thisItem.ItemSlots;i++){
            thisItem[("p"+i)]=true;
            this[("P0"+i)].addItem(thisItem);
            }

            should be same, (I have not tested the syntaxt throughoutly, but should work)
            • 3. Re: There Must Be An Easier Way!?!?!
              dimival Community Member
              Can't you use the switch conditional statement???
              • 4. Re: There Must Be An Easier Way!?!?!
                ctzn99 Community Member
                quote:

                Originally posted by: dimival
                Can't you use the switch conditional statement???


                You could but then your trading 26 if statements for 26 case statements.
                • 5. Re: There Must Be An Easier Way!?!?!
                  dimival Community Member
                  quote:

                  Originally posted by: ctzn99
                  quote:

                  Originally posted by: dimival
                  Can't you use the switch conditional statement???


                  You could but then your trading 26 if statements for 26 case statements.



                  True true :P never mind then
                  • 6. Re: There Must Be An Easier Way!?!?!
                    jfillman Community Member
                    Thank you for the help. The best option seems to be levancho's suggestion.