How to Use .GetType in a Function ?

Using (or providing) Microsoft.NET Classes

How to Use .GetType in a Function ?

Postby PGilbert on Sat Aug 07, 2010 6:01 pm

If I create a .Net Object for example here a Rectangle:

R←⎕NEW Rectangle (10,10,10,10)

How can I use the .GetType property to verify if it's a Rectangle Object programmatically ?

R.GetType
System.Drawing.Rectangle

⍝ How to make work the following line ?
'System.Drawing.Rectangle' ≡ R.GetType
0

Thanks in advance,

Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Use .GetType in a Function ?

Postby RossHale on Sat Aug 07, 2010 7:23 pm

In the wonderful world of .NET objects
R.GetType is again an instance of a .NET object with attendant properties and methods.
(R.GetType).GetType
System.RuntimeType ⍝ visible result coerced by application of its method ToString

R.GetType
System.Drawing.Rectangle ⍝ visible result coerced by application of its method ToString

'System.Drawing.Rectangle' ≡ R.GetType.ToString ⍝ explicit excavation to string
1

Never leave well enough alone, on to Moscow ---
you can simply leave off the quotes on the left hand side to specify a System.RuntimeType of System.Drawing.Rectangle
and test it for equivalence with the System.RuntimeType produced by R.GetType without harming or abusing any strings ...

System.Drawing.Rectangle ≡ R.GetType ⍝ explicit matching of System.RuntimeType(s)

Hope this helps
Ross Hale
User avatar
RossHale
 
Posts: 31
Joined: Thu Jun 18, 2009 9:08 pm
Location: Bellevue, Washington, USA

Re: How to Use .GetType in a Function ?

Postby PGilbert on Sat Aug 07, 2010 8:52 pm

Thanks RossHale for your answer, the .GetType.ToString is doing the trick for me.

I have this follow-up question: If R becomes NULL, when I do a .GetType I get a VALUE ERROR. What do I need to do to test if a .Net object is NULL ? ⎕null ≡ R does not work.

Code: Select all
      R
(NULL)

      ⎕null ≡ R
0


Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Use .GetType in a Function ?

Postby RossHale on Sat Aug 07, 2010 9:29 pm

R
(NULL)

'(NULL)'≡⍕R
1

R←⎕new Rectangle (10 10 10 10)
'(NULL)'≡⍕R
0

I usually put guards or error trapping to handle this sort of thing, but this should fly.

Ross
User avatar
RossHale
 
Posts: 31
Joined: Thu Jun 18, 2009 9:08 pm
Location: Bellevue, Washington, USA

Re: How to Use .GetType in a Function ?

Postby PGilbert on Sat Aug 07, 2010 9:36 pm

Many thanks again RossHale, '(NULL)'≡⍕R is what I was looking for.

Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Use .GetType in a Function ?

Postby DanB|Dyalog on Sun Aug 08, 2010 7:06 pm

In general, using thorn is the same as using the .ToString method.
Thus
'System.Drawing.Rectangle' ≡ R.GetType.ToString
is the same as
'System.Drawing.Rectangle' ≡ ⍕R.GetType
/Dan
DanB|Dyalog
 


Return to Microsoft.NET

Who is online

Users browsing this forum: No registered users and 1 guest