Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- OOP Need help getting methods to return objects.

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

OOP Need help getting methods to return objects.

Mar 14, 2019 7:24 PM
(354 views)

I am working on a class of vectors in JSL. I would like to have a method that would return a normalized version of the vector. Below is my script and error message. Thanks!

```
Clear Log();
Clear Globals();
Define Class(
"vector",
x = 0;
y = 0;
z = 0;
_init_ = Method( {a, b, c = 0},
x = a;
y = b;
z = c;
);
_show_ = Method( {},
Return( x, y, z )
);
mag = Method( {},
magnitude = Sqrt( x ^ 2 + y ^ 2 + z ^ 2 )
);
normalize = Method( {},
nv = vector(0,0,0);
length = Sqrt( x ^ 2 + y ^ 2 + z ^ 2 );
show(length);
nv:x = x / length;
nv:y = y / length;
nv:z = z / length;
nv;
);
);
bob = New Object( vector( 4, 2 ) );
Show( bob );
bob<<normalize();
```

Name Unresolved: vector in access or evaluation of 'vector' , vector( 0, 0, 0 )

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: OOP Need help getting methods to return objects.

I am a novice when it comes to classes in general and JMP Classes. This works:

```
Clear Log();
Clear Globals();
Define Class(
"vector",
x = 0;
y = 0;
z = 0;
_init_ = Method( {a, b, c = 0},
x = a;
y = b;
z = c;
);
_show_ = Method( {},
Return( x, y, z )
);
mag = Method( {},
magnitude = Sqrt( x ^ 2 + y ^ 2 + z ^ 2 )
);
normalize = Method( {},
nv = [0,0,0];
length = Sqrt( x ^ 2 + y ^ 2 + z ^ 2 );
show(length);
nv[1] = x / length;
nv[2] = y / length;
nv[3] = z / length;
Print(char(nv));
nv;
);
);
bob = New Object( vector( 4, 2 ) );
Show( bob );
bob = bob:normalize();
show(bob);
```

Ther is a method to pass self. I suggest the JMP Scripting Guide and searching this website for Classes.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: OOP Need help getting methods to return objects.

Georgia,

Thanks for your reply. I found the "clone" message for objects that I can use to create a copy of the object and then normalize it. I will keep looking around and see if I can find anything on how to return objects from methods directly like they do in the "complex" class example.

John