Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'properties of objects' topicStudent Support Forum > General > Archives > "properties of objects"

Help | Reply To Topic
Author Comment/Response
Jeremy Jones
04/08/12 04:10am

Hello, I am trying to create objects and store properties about those objects. I think I know how to create objects. As a simple example,

createMyCircle[radius_/;radius>=0]:=MyCircle[radius];

And if I want to get the radius, or change it, then I can write functions like

getRadius[circle_MyCircle]:=circle[[1]];
setRadius[circle_,radius_/;radius>=0]:=circle[[1]]=radius;
SetAttributes[setRadius,HoldFirst];

Up to here, I have two questions:

1) Am I going about this the right way, or is there a better way of creating new objects?
2) For setRadius, it doesn't work if I put circle_MyCircle instead of just circle_. Why?

Once I have created the object, I want to work out certain properties of the object. For the above example, let's say area and perimeter. But I only want to work them out once and then store them somehow with the object. I'm not sure how to do this. The only way I can think of is to change the first function to something like

createMyCircle[radius_/;radius>=0]:=MyCircle[radius,Null,Null];

and use positions 2 and 3 for the area and perimeter, but this doesn't seem very nice. Is there some way of referencing information in an object with strings instead of positions in an array?

Thank you very much for the help.

URL: ,
Help | Reply To Topic