2 Replies Latest reply on Oct 21, 2014 6:30 AM by sbcs

    Object oriented programming

    sbcs Level 1

      I am unable to pass data from one view to another using objects.

       

      I have 2 views named FirstView.mxml and SecondView.mxml.

      I have 1 actionscript file named Pass.as

       

      Any help is much appreciated.

       

       

      Pass.as

       

      package components

      {

             public class Pass

             {

       

                  [Bindable]

                  public var first:String;

                  public var second:String;

       

                  public function Pass()

                  {

                  }

       

                  public function setFirst(first:String):void

                  {

                  this.first = first;

                  }

       

                  public function setSecond(second:String):void

                  {

                  this.second = second;

                  }

       

                  public function getFirst():String

                  {

                  return first;

                  }

       

                  public function getSecond():String

                  {

                  return second;

                  }

       

                  public function getAll():String

                  {

                  return first + " " + second;

                  }

       

             }

      }

       

       

      FirstView.mxml

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

                                 xmlns:s="library://ns.adobe.com/flex/spark"

                                 title="FirstView"  

                                     xmlns:components="components.*">

      <fx:Script>

        <![CDATA[

       

                       import components.Pass;

       

                       [Bindable]

                       public var pass:Pass;

       

                       protected function handleClick(event:MouseEvent):void

                       {

       

                       pass = new Pass();

       

                       pass.setSecond(inputbox2.text);

                       pass.setFirst(inputbox1.text);

       

                       navigator.pushView(SecondView);

       

                       }

       

        ]]>

        </fx:Script>

       

        <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

        </fx:Declarations>

       

        <s:Label text="Enter your student identification number" x="10" y="10">

        </s:Label>

       

        <s:TextInput id="inputbox1" x="10" y="33">

        </s:TextInput>

       

        <s:TextInput id="inputbox2" x="10" y="145">

        </s:TextInput>

       

        <s:Button label="Search" click="handleClick(event)" x="10" y="362">

        </s:Button>

      </s:View>

       

       

      SecondView.mxml

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

                        xmlns:s="library://ns.adobe.com/flex/spark"

                        title="SecondView"

                        xmlns:components="components.*">

       

        <fx:Script>

        <![CDATA[

       

                       import components.Pass;

       

                       [Bindable]

                       public var passData:Pass = new Pass();

       

                       protected function handleClick(event:MouseEvent):void

                       {

       

                       label3.text = passData.getAll();

       

                       }

       

        ]]>

        </fx:Script>

       

       

        <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

        </fx:Declarations>

       

       

        <s:Label text="{passData.getFirst()}" x="10" y="45">

        </s:Label>

       

        <s:Label text="{passData.getSecond()}" x="10" y="145">

        </s:Label>

       

        <s:Label id="label3" x="10" y="245">

        </s:Label>

       

       

        <s:Button label="Search" click="handleClick(event)" x="10" y="362">

        </s:Button>

       

       

      </s:View>

        • 1. Re: Object oriented programming
          _spoboyle Level 4

          you've created 2 instances of Pass here.

           

          in FirstView instead of calling

           

          navigator.pushView(SecondView);

           

          call

           

          navigator.pushView(SecondView, pass);

           

          then in SecondView instead of creating a new instance of Pass you can access the pass obect created in FirstView  through the data property in Secondview

           

          e.g.

           

          SecondView.mxml

           

          <?xml version="1.0" encoding="utf-8"?>

           

          <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="library://ns.adobe.com/flex/spark"

              title="SecondView"

              xmlns:components="components.*">

           

              <fx:Script>

           

              <![CDATA[

                  import components.Pass;

                  [Bindable]

                  protected function handleClick(event:MouseEvent):void

                  {

                      label3.text = (data as Pass).getAll();

                  }

              ]]>

           

              </fx:Script>

           

              <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

              </fx:Declarations>

           

              <s:Label text="{(data as Pass).getFirst()}" x="10" y="45">

              </s:Label>

           

              <s:Label text="{(data as Pass).getSecond()}" x="10" y="145">

              </s:Label>

           

              <s:Label id="label3" x="10" y="245">

              </s:Label>

           

              <s:Button label="Search" click="handleClick(event)" x="10" y="362">

              </s:Button>

          </s:View>

          • 2. Re: Object oriented programming
            sbcs Level 1

            Good day,

             

            Thanks for the quick response.

             

            Enjoy the rest of your day.

             

            Kind regards.