Question
· Jun 4, 2019

Object Dispatch error when using $G or $D

Hello,

I assume there is a simple explanation for this, but I do get <OBJECT DISPATCH> error, when I am trying to set a global to a value.

My example is huge, but I reproduced it using Samples namespace:  

First I delete the Title from ##class(Cinema.Film)  - 3 

Secondly:

SAMPLES>s ref=##class(Cinema.Film).%OpenId(3)
SAMPLES>w ref.Title

SAMPLES>set ^AK(1)=$G(ref.Title)

SET ^AK(1)=$G(ref.Title)
^
<OBJECT DISPATCH> *Property 'Title' in class 'Cinema.Film' must be MultiDimensional

$D also does not work, same error, however this works fine: if ref.Title S ^AK(1)=ref.Title

I am almost 100 % certain it's working as it should, but wanted to double check. 

Thank you

Discussion (4)1
Log in or sign up to continue

Docs of $G say

variable                            A local variable, global variable, or process-private global variable, subscripted or unsubscripted. The variable may be undefined. variable may be specified as a multidimensional object property with the syntax obj.property.

similar Docs of $D say

variable                            The variable whose status is to be checked. A local or global variable, subscripted or unsubscripted. The variable may be undefined. You cannot specify a simple object property reference as variable; you can specify a multidimensional property reference as variable with the syntax obj.property.

but Title in your case is $li(^CinemaooFilmD(3),1). So neither $G() nor $D() is approriate.

You can't use $get or $data on an object property. What you should do is as follows,

If $IsObject(ref) {set ^AK(1)=ref.Title}

Else {set ^AK(1)=""} // or whatever you want to do if ref is not an object

You could also do this:

If $IsObject(ref),$l(ref.Title) {set ^AK(1)=ref.Title}

Else {set ^AK(1)=""} // or whatever you want to do if ref is not an object or ref is an object but the property title is null