Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

using "object" in x++

(1) ShareShare
ReportReport
Posted on by 1,145
Hi,

is there a way i could avoid using "object"?

I made this method, where i had to make the return type as object, as class AA extends class AAExtra
    public static Object getValue(Field1 _field1, Field2 _field2, Id _Id, NoYesId _more = NoYes::No)
    {
        Object                      value;
        Table1                      table1;
        Table2                      table2;
        Table3                      table3;
        


        select table1.......etc with joins

        if(table1)
        {
            if(_more)
            {
                value = AAExtra();
                value.zz(table1.FieldZ);
            }
            else
            {
                value = new AA();
            }
            value.Field1(table3.Field1);
            value.Field2(table1.Field2);
            value.TypeId(table3.Type);
            value.Format(table3.Format);
        }    
        return value;
    }

Then when calling the method, i had also to use "object"  -- is this a good approach?
        List list = new List(Types::Class);

        while select Field1, Field2, Field3 from randomTable
            group by Field1, Field2, Field3
            where randomTable.Id == _header.Id
                && randomTable.Field1 != ''
        {

                Object value = TableX::getValue(randomTable.Field1,randomTable.Field2, _Id);
                if(value)
                {
                    list.addEnd(value);
                }
            }
        }
 
Categories:
  • .. Profile Picture
    .. 1,145 on at
    using "object" in x++
    Hi,
     
    I marked the answer posted at 8:75 as verified. I'm not sure who removed it and marked another answer as verified.
     
  • .. Profile Picture
    .. 1,145 on at
    using "object" in x++
    Hi Martin,


    Thank you.. my business event is working fine. I will close this thread
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    using "object" in x++
    Yes, the list contains the right objects and we fixed your wrong design with the object type. In the last reply, I was reacting to your previous remark about the business events. Anyway, let's close this thread and you can create a new one when you run into problem with your business event.
  • .. Profile Picture
    .. 1,145 on at
    using "object" in x++
    Hi Martin,

    i got confused, as I said what i did already works perfectly with your guidance

    if _more is true, then the AAExtra is returned and i can see fieldZ in the list as expected:
    "List":
    [
    {"FieldZ":"Yes","Field1":"field1","Field2":"Field2","TypeId":"type","Format":"format"}
    ,{"FieldZ":"Yes","Field1":"field11","Field2":"Field22","TypeId":"type1","Format":"format1"}
    ]

    if _more is false, then the AA is returned and fieldZ doesn't appear in the list as expected:
    "List":
    [
    ,{"Field1":"field1","Field2":"Field2","TypeId":"type","Format":"format"}
    ,{"Field1":"field11","Field2":"Field22","TypeId":"type1","Format":"format1"}
    ]



    Does your last reply mean that i need to do sth else?
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    using "object" in x++
    If you want a business event capable of returning FieldZ, you must use AAExtra class as the contract. If you want a list, you need a data contract containing the list of AAExtra contracts. You can't change the type of the contract at runtime.
     
    If you want to use business events with two different contracts, then you need two business events, each with its own contract.
  • .. Profile Picture
    .. 1,145 on at
    using "object" in x++
    Hi Martin,

    I don't have a mix (at least for now)
    it's either returns AA or AAExtra (depending from where it is called)

    I don't want to loop through the list. I just want to send this list via business events.
    So if they type was AA, then the list in the business event shouldn't show fieldZ, and it it was AAExtra, then it should show it.

    I did what you advised me for the first part and it's working fine. Thank you.
    Now, for the 2nd part, i did this and it's also working fine. It's returning fieldZ based on _more without the need to do any casting here. And even if there was a mix it's also working fine with the code below
    AA value = TableX::getValue(randomTable.Field1,randomTable.Field2, _Id);
    if(value)
    {
      list.addEnd(value);
    }
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    using "object" in x++
    "I want the list to automatically return fieldZ" doesn't make a good sense. You have a list of objects (instances of AA or AAExtra classes), therefore that's what you'll get from it. The only way how to access fieldZ is through the instance of AAExtra class; it has nothing to do with the list as such.
     
    Let me also show you how you could get data from the list:
    ListEnumerator enumerator = list.getEnumerator();
    while (enumerator.moveNext())
    {
        AA value = enumerator.current();
        value.field1();
        
        AAExtra extraValue = value as AAExtra;
        if (extraValue)
        {
            extraValue.zz();
        }
    }
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    using "object" in x++
    You're right; I made a mistake when rewriting your code. Sorry about that. Here is the fixed code:
    public static AA getValue(Field1 _field1, Field2 _field2, Id _id, boolean _more = false)
    {
        AA value; // AA, not object
        ...
        if (_more)
        {
            AAExtra valueExtra = new AAExtra();
            valueExtra.zz(table1.FieldZ);
            value = valueExtra; // You can assign an instance of AAExtra to the variable of AA type
        }
        else
        {
            value = new AA();
        }
        
        value.Field1(table3.Field1);
        value.Field2(table1.Field2);
        value.TypeId(table3.Type);
        value.Format(table3.Format);
                
        return value;
    }
    
    Your sentence "if i will put list.AddEnd(AA); then fieldZ will never appear even if _more was true" shows that you still don't understand what objects are and how to work with them. If you have an instance of AAExtra class in value variable and you call list.addEnd(value), the AAExtra object is put into the list. It doesn't matter whether the type of value variable is AA, AAExtra or object; the contents of the variable is still the same.
     
    In the debugger, look at what you have the in the list. You'll see that it contains AA and AAExtra objects (depending on how you called getValue().
     
    Your statement "FieldZ will never appear" suggests that your problem isn't with adding to the list, but that you don't know how to read the data. I'm assuming that you don't know how to cast the value to the right type; that's why I showed it to you in my previous reply.
  • .. Profile Picture
    .. 1,145 on at
    using "object" in x++
    Hi Martin,


    I got the error below at this line "value.zz(table1.FieldZ);"
    value = new AAExtra(); // In your code you forgot to put "new" but i think u forgot it by mistake
    value.zz(table1.FieldZ);

    ​​​​​​​Class 'AA' does not contain a definition for method 'zz' and no extension method 'zz' accepting a first argument of type 'AA' is found on any extension class.   
    it seems even though we said "value=AAExtra();" it sill sees the declaration as "AA value;"?


    Now for the 2nd part, i think i didn't understand it fully

    So if i will put:  AA value = TableX::getValue(randomTable.Field1, randomTable.Field2, _id);
    and if i will put list.AddEnd(AA); then fieldZ will never appear even if _more was true.

    I want the list to automatically return fieldZ if the return type was AAExtra, and not return it if it was AA

    maybe i will understand better if the first part works first
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    using "object" in x++
    Not at all! You achieve nothing useful by declaring the variables as object and you just make code much worse.
     
    Also, stop talking about a list of fields; what you have list of objects: instances of either AA or AAExtra classes. This fact is true regardless of whether you assign the value to a variable of type AA or object.
     
    Let me show you how the correct implementation could look like and how you can then use the objects. Please let me know if you don't understand some parts.
    // We declare the return type as AA, therefore the method can return either AA or any of
    // is descendants. In this case, it means either AA or AAExtra.
    // If you declare it as object, it could return anything, e.g. an instance of Query.
    // Consumers of you method wouldn't know what to expect.
    public static AA getValue(Field1 _field1, Field2 _field2, Id _id, boolean _more = false)
    {
        AA value; // AA, not object
        ...
        if (_more)
        {
            value = AAExtra(); // You can assign an instance of AAExtra to the variable of AA type
            value.zz(table1.FieldZ);
        }
        else
        {
            value = new AA();
        }
        
        value.Field1(table3.Field1);
        value.Field2(table1.Field2);
        value.TypeId(table3.Type);
        value.Format(table3.Format);
                
        return value;
    }
    
    void demoOfProcessing()
    {
        AA value = TableX::getValue(randomTable.Field1, randomTable.Field2, _id);
        // Now you can access all fields defined in class AA
        
        // If you want members of AAExtra class, cast the object from AA to AAExtra
        AAExtra extraValue = value as AAExtra;
        
        // If value is an instance of AAExtra, you'll get it in extraValue variable.
        // If it's an instance of AA, extraValue will be null.
        if (extraValue)
        {
            extraValue.zz(); // Now you can access methods of AAExtra through extraValue variable.
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans