1
votes

in Flex I have something like that:

  var dg:DataGrid = new DataGrid();    
  if (something) dg = dg1 else if (something_2) dg = dg2;
  dg.dataProvider.getItemAt(3).id;    

and dg is ALWAYS pointing at DataGrid (even if dg1 has name DataGrid_test and dg2 = DataGrid_test2) and finally action is made on my first DataGrid (DataGrid_test). Why? How can I pass dg1 or dg2 to dg?

Here is pasted almost full code of this part of application. I edited it to make that more clear.

    var dg:DataGrid = null; 
    if ( currentState == "state1" ) { //if this condition is true then app. go into if and
        dg = dataGrid_first; // make dg = DataGrid (1)
        test.text = "inco";  // shows "inco" in "test" label
    } else if ( currentState == "state2" ) { // if this is true then app. go..
        dg = dataGrid_second; //here and set dg as DataGrid (exactly!) (2)
        test.text = "outgo"; // and change test label into blank text (earlier text disapears)
    }
    search(dg);

It is modified with advice of '@splash' Still not working.

EDIT: I made this sceond edit to answer for all You who are helping me with that :) I think that it will be the best way. In codeblock above I added comments. (please read now comments and after that come back here :) ) Now I will explain exactly what happens. I debug it many times and here are results: dg is pointing at DataGrid (as component in flex, not as my dataGrid_first), I needed to extend DataGrid so now it is ColorColumn component (I don't know if I called it properly), not DataGrid. And dg is pointing at ColorColumn not at dataGrid_first or dataGrid_second. I even tried today the same thing what suggest @splash:

if ( currentState == "state1" ) { 
    test.text = "inco"; 
    search(dataGrid_first); 
} else if ( currentState == "state2" ) {
    test.text = "outgo";
    search(dataGrid_second);
}

and search still points at ColorColumn :/ My problem is really easy- I just want to pass to search different dataGrid on each state. If You have other ideas how I can do that in right way then I will pleased to hear about it. :) But still I don't understand why it doesn't work. My search function uses algorhitm Boyer-Moor for searching through dataGrid.dataProvider for some text. If it find something then it is pushed into new array and after passing whole dataProvider I colorize rows with searched word.

2
I'm not sure I get what you're asking. Of course it's pointing to 'DataGrid' that's the class... Also, I don't understand your 'logic'. Why would you ever create a DataGrid if you're just going to switch it anyways? Extremely convoluted code..J_A_X
I write an application which has 2 different DataGrid's in states. In first state DataGrid has id= "dg1" in second id="dg2". I have also button which has an action "Search". That button is in both states. Now I try to do action that pressing "Search" in first state pass to search function dg1, pressing Search in second state passing dg2. It's easy, I'm sure, but I'm missing something... :/ You wrote: "Why would you ever create a DataGrid if you're just going to switch it anyways?". What better way You suggest to do that?Gut6
I would love to suggest better, but I can't suggest on a technical question like that. What are you trying to accomplish by doing this?J_A_X
I answer similar question after this post so I will past it here to: I want do that search function searches through different dataGrid.dataProvider in different states. In state1 search function should search for text in dg1 and in state2 search function should search in dg2.Gut6
But why do you need states? What are the search for? Why are there 2?J_A_X

2 Answers

1
votes

If dg is never pointing to dg1 and dg2 then your (something) expressions may be evaluate to false. Check the value of your if-conditions - this should be easy to debug.

This should work:

var dg:DataGrid = null;    
if (something) 
    dg = dg1;
else if (something_2) 
    dg = dg2;
if (dg)
{
    // do something with dg
}

[Update]

I still can't see why your code isn't working, but you could simplify it like this:

if ( currentState == "state1" ) { 
    test.text = "inco"; 
    search(dataGrid_first); 
} else if ( currentState == "state2" ) {
    test.text = "outgo";
    search(dataGrid_second);
}
1
votes

I'd propose to write this - since I guess either dg1 or dg2 should be assigned:

if (something) {

    dg = dg1;

} else {

    dg = dg2;

}

There may be cases, where if () {} else () {} neither executes the first or the second conditional block.

Finally a small hint, which structurally eliminates unwanted assignments in if conditions: Always write the literal left of the comparison operation: if ( "state1" == currentState ). If you accidentally typed = instead of ==, the flex compiler emits an error. The other notation silently assigns a value.

Additionally: Did you single-stepped through your code and watched the variables dg1, dg2 and dg? If not, set a breakpoint a few line before the if-statement and run the code step by step from there on. What do you see?

Here's a another tip: Use assertions to check for inconistencies:

package my.company.utilities {

  public function assert(expression:Boolean):void {

      // probably conditionally compile this statement
      if (!expression) {
          throw new Error("Assertion failed!");
      }

  } // assert

}

Use it e.g. at the beginning of a method like this:

public function doTransaction( fromAccount:int, toAccount:int ) {

  assert( 0 < fromAccount );
  assert( 0 < toAccount );

}

A typically good use of assert is to check variables regarding their range. As of the above example, fromAccount and toAccount should always be positive. Due to a bug, bad values might get passed to doTransaction(). In this case, the assertion fires an error.