locked
Entity Created() method throws undefined on class members RRS feed

  • Question

  • I am trying, from an entity's created method, to access a composed class (another entity) to see how many there are via .count() The relationship is:

    ClassA ->(1 to many) -> ClassB

    So on ClassB.Created() I want to do ClassB.ClassAs.ClassB.Count(); but ClassA is undefined.

    Where would I set ClassA as there's no constructor.

    Friday, February 20, 2015 3:14 PM

Answers

  • You were forcing us to guess too much. So it is C# and the desktop client from your code snippets.

    Client-side Silverlight vs HTML/JavaScript vs server-side makes quite some difference!

    The *_Created event fires when an object is instantiated, it does not have properties set. One use is to set default values for properties. So it is the wrong event to be attempting to use properties, they are not initialised. The desktop client is hiding a lot of work from you.

    In you code you probably don't want to keep recounting and updating ClassB every time you click back on the row. Should you delete some ClassB (if allowed) wont it screw-up the 'Order' and the counting, perhaps if that applies you may want to use something different, like 'maximum Order + 1'.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Marked as answer by Lynart Friday, February 20, 2015 7:39 PM
    Friday, February 20, 2015 7:32 PM

All replies

  • Normally you would retrieve a ClassA entity, or new a ClassA entity, then assign it to the parent property on your new ClassB entity. That would be like 'myClassB.setClassA(myClassA).

    After 'new'ing an instance of an entity you need to at least set all the mandatory properties (except the Id :)

    You need to give us a bit of a clue as to C#/VB/JavaScript code and show your sample code if you need further help.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    • Edited by Xpert360 Friday, February 20, 2015 6:12 PM
    Friday, February 20, 2015 6:11 PM
  • Normally you would retrieve a ClassA entity, or new a ClassA entity, then assign it to the parent property on your new ClassB entity. That would be like 'myClassB.setClassA(myClassA).

    After 'new'ing an instance of an entity you need to at least set all the mandatory properties (except the Id :)

    You need to give us a bit of a clue as to C#/VB/JavaScript code and show your sample code if you need further help.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.


    The code isn't the problem, it's a sequence of events.

    I have a ClassA screen that has a grid/list of ClassB's. When the user adds a new ClassB (either double clicking in the grid or on the + button), I need execute the following line:

    ClassB.Order = ClassB.ClassA.ClassBs.Count();

    I figured putting this in ClassB's Created() method would work but when Created() is called, ClassA is undefined, an Exception is thrown. This is all part of Lightswitch's framework unless I'm mistaken.

    I found a workaround by doing the following on the screen instead:

    partial void ClassB_SelectionChanged()

      if(this.ClassB.SelectedItem.Details.EntityState == EntityAdded)

        this.ClassB.SelectedItem.Order = this.ClassB.ClassA.Count();

    Was I going about it the wrong way using Created() ?

    Also how do I wrap the above code in a code block or something?

    Thank you!

    • Edited by Lynart Friday, February 20, 2015 7:16 PM
    Friday, February 20, 2015 7:15 PM
  • You were forcing us to guess too much. So it is C# and the desktop client from your code snippets.

    Client-side Silverlight vs HTML/JavaScript vs server-side makes quite some difference!

    The *_Created event fires when an object is instantiated, it does not have properties set. One use is to set default values for properties. So it is the wrong event to be attempting to use properties, they are not initialised. The desktop client is hiding a lot of work from you.

    In you code you probably don't want to keep recounting and updating ClassB every time you click back on the row. Should you delete some ClassB (if allowed) wont it screw-up the 'Order' and the counting, perhaps if that applies you may want to use something different, like 'maximum Order + 1'.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Marked as answer by Lynart Friday, February 20, 2015 7:39 PM
    Friday, February 20, 2015 7:32 PM
  • You were forcing us to guess too much. So it is C# and the desktop client from your code snippets.

    Client-side Silverlight vs HTML/JavaScript vs server-side makes quite some difference!

    The *_Created event fires when an object is instantiated, it does not have properties set. One use is to set default values for properties. So it is the wrong event to be attempting to use properties, they are not initialised. The desktop client is hiding a lot of work from you.

    In you code you probably don't want to keep recounting and updating ClassB every time you click back on the row. Should you delete some ClassB (if allowed) wont it screw-up the 'Order' and the counting, perhaps if that applies you may want to use something different, like 'maximum Order + 1'.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thanks for the response. The deletion logic is already in place. The only problem left was what I posted here. 

    Forgive my ignorance with regards to the different Client types!

    Friday, February 20, 2015 7:39 PM
  • No problem! For people helping it is always great to know the VS version, coding language and LightSwitch client type (not particularly to Lynart but anyone else new reading this :)

    Glad to hear you are on top of the other potential issues already.

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Friday, February 20, 2015 7:46 PM
  • Forgot to say, you may consider putting your counting logic in the {entity}_Inserting event just before it is saved.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Friday, February 20, 2015 7:51 PM