Quantcast
Channel: SCN : All Content - PowerBuilder Developer Center
Viewing all articles
Browse latest Browse all 2935

.NET assembly returning wrong object type

$
0
0

I am running into a problem with a .NET assembly created in Powerbuilder 12.5 classic. The code below illustrates my issue but my actual situation is more complex than this.

 

I have the following objects:

n_cst_base inherits from nonvisualobject

n_cst_child_a inherits from n_cst_base

n_cst_child_b inherits from n_cst_base

n_cst_grandchild_a inherits from n_cst_child_a

 

n_cst_base has a public function of_myname, which is overridden by each child.  Here is the code in that function:

n_cst_base: return 'I am base'

n_cst_child_a: return Super::of_myname() + ' and I am child A'

n_cst_child_b: return Super::of_myname() + ' and I am child B'

n_cst_grandchild_a:  return Super::of_myname() + ' and I am grandchild A'

 

I also have object n_cst_object_factory from nonvisualobject with the following method:

 

public function n_cst_base of_create_dynamic (string asv_childobject);

n_cst_base lnv_object

lnv_object = CREATE using asv_childobject

return lnv_object

 

This function returns n_cst_base but can create any object. (There is no error checking for the illustration.)

 

I have a .NET assembly target that exposes all these methods in namespace PJS.Test. In my Visual Studio C# project, I can add a reference. The object browser shows the inheritance correctly.

public class n_cst_base

public class n_cst_child_a : n_cst_base

public class n_cst_child_b : n_cst_base

public class n_cst_grandchild_a : n_cst_child_a

 

I have the following test method in C#:

public void TestObjects()

{

n_cst_object_factory oObjectFactory = new n_cst_object_factory();

n_cst_base oChildBaseB = oObjectFactory.of_create_dynamic("n_cst_child_b");

string sBName = oChildBaseB.of_myname();

Boolean IsChildB = oChildBaseB is n_cst_child_b ? true : false;

n_cst_child_b oChildB = (n_cst_child_b)oChildBaseB;

 

When I debug this, I can see the value of sBName = "I am base and I am child B" as expected.  However the type of oChildBaseB is n_cst_base when I expect that it would be n_cst_child_b.  The value of IsChildB is false and the cast throws an InvalidCastException. The result of of_myname indicates that the object is indeed n_cst_child_b. Why does is it typed as n_cst_base?

 

Again in C#, I can do the following

n_cst_base oChildBaseB = new n_cst_child_b();

string sBName = oChildBaseB.of_myname();

Boolean IsChildB = oChildBaseB is n_cst_child_b ? true : false;

n_cst_child_b oChildB = (n_cst_child_b)oChildBaseB;

 

In this scenario, the type of oChildBaseB is n_cst_child_b.  The IsChildB is true and the cast is successful. Why is it different when it comes out of the Powerbuilder assembly?

 

Is there some other solution to this? As stated above, my real application is more complex than this and there will be a large number of descendants, so it is impractical to write separate methods (of_create_child_a, of_create_child_b, etc) that return each type explicitly.


Viewing all articles
Browse latest Browse all 2935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>