web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Downcasting is not working

(0) ShareShare
ReportReport
Posted on by 657

Hi,

I'm trying to create a custom webservice in D365 Platform U12.
I wanted to use a base response class and derive every other class from that base class.

So the base class is as follows :

/// <summary>
/// Base Response DataContract
/// </summary>
[DataContractAttribute]
class PWBMobServiceBaseResponse
{
boolean _callSucceeded;
str _errorMessage;
boolean _logonSucceeded;
str _logonErrorMessage;

/// <summary>
/// Datamember callsucceeded
/// </summary>
/// <param name = "callSucceeded"></param>
/// <returns></returns>
[DataMemberAttribute("CallSucceeded")]
public boolean parmCallSucceeded(boolean callSucceeded = _callSucceeded)
{

_callSucceeded = callSucceeded;
return _callSucceeded;

}

/// <summary>
/// Datamember errorMessage
/// </summary>
/// <param name = "errorMessage"></param>
/// <returns></returns>
[DataMemberAttribute("ErrorMessage")]
public str parmErrorMessage(str errorMessage = _errorMessage)
{

_errorMessage = errorMessage;
return _errorMessage;

}

/// <summary>
/// Datamember logonSucceeded
/// </summary>
/// <param name = "logonSucceeded"></param>
/// <returns></returns>
[DataMemberAttribute("LogonSucceeded")]
public boolean parmLogonSucceeded(boolean logonSucceeded = _logonSucceeded)
{

_logonSucceeded = logonSucceeded;
return _logonSucceeded;

}

/// <summary>
/// Datamember logonErrorMessage
/// </summary>
/// <param name = "logonErrorMessage"></param>
/// <returns></returns>
[DataMemberAttribute("LogonErrorMessage")]
public str parmLogonErrorMessage(str logonErrorMessage = _logonErrorMessage)
{

_logonErrorMessage = logonErrorMessage;
return _logonErrorMessage;

}

}

While the derived class just derives but has no extra fields :


/// <summary>
/// ValidateUser Response DataContract
/// </summary>
[DataContractAttribute]
class PWBMobServiceValidateUserResponse extends PWBMobServiceBaseResponse
{
}

I got a function that validates the user and returns an object of type PWBMobServiceBaseResponse.


PWBMobServiceBaseResponse baseResponse = cls.GetBaseResponse(request);

When I test the next line, it skips the info so telling me that the downcast is invalid.

if (baseResponse is PWBMobServiceValidateUserResponse)
info("base is derived");


The 'As' keyword is invented to downcast ( from base -> derived ) but yet it does not work here :-(
Any Idea's, am I missing something ?

Regards,
Sven Peeters

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Daniel Weichsel Profile Picture
    on at

    Hi Sven,

    Look closely at the code you refer to by: "I got a function that validates the user and returns an object of type PWBMobServiceBaseResponse.".  The object being returned must have been constructed as a PWBMobServiceValidateUserResponse object (or a further derived class).  Downcasting with "as" does not magically take an object and convert it to a more derived type; the object must already have been created with that type.  The debugger can show you the object types at runtime.

  • Sven Peeters Prodware Profile Picture
    657 on at

    Mmmm, now I get it. It's not as flexible as in C#.

    I created a method on the base class, no more downcasting necessary ...

  • Martin Dráb Profile Picture
    239,647 Most Valuable Professional on at

    It would be the same in C# or any other strongly-typed object-oriented language. Casting changes the type of the reference; it doesn't change the type of the object itself on the heap.

    To create a DerivedClass object from a BaseClass object, you would have to create a new object, initialize DerivedClass's fields etc. It's not a mere change to a more specific reference type.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 24

#2
Michel ROY Profile Picture

Michel ROY 14

#3
Jagadabi Profile Picture

Jagadabi 6

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans