reference objects (newbie question)

R

Roderik

Hi,

I'd like to make a function to manipulate objects of a given class.

Let's say there are two objects, called MyPoint1 and MyPoint2.
They are both of the class cCustomPoint and contain an x an y
attribute.

The function should assign a value for x to a passed object (MyPoint1
or MyPoint2).

How should I make this function so it knows which object to manipulate
and the value to be assigned?

Kind regards,

Roderik
 
A

Anthony Borla

Roderik said:
Hi,

I'd like to make a function to manipulate objects of a
given class.

Let's say there are two objects, called MyPoint1
and MyPoint2. They are both of the class
cCustomPoint and contain an x an y attribute.

The function should assign a value for x to a passed
object (MyPoint1 or MyPoint2).

class cCustomPoint
{
public int getX() { return x; }
public int getY() { return y; }
public int setX(int x) { this.x = x; }
public int setY(int y) { this.y = y; }

private int x, y;
}

class cCustomPointManager
{
...
public void passXTocCustomPoint(int x, cCustomPoint ccp)
{
ccp.setX(x);
}
...
}

...
public static void main(String[] args)
{
...
cCustomPointManager ccpm = new cCustomPointManager();
...
cCustomPoint MyPoint1 = new ..., MyPoint2 = new ...;
...
...
ccpm.passXTocCustomPoint(5, MyPoint1);
ccpm.passXTocCustomPoint(6, MyPoint2);
...
...
How should I make this function so it knows which
object to manipulate and the value to be assigned?

I'm not sure what you are asking here. As the sample code [a very contrived
example at that] shows, you normally know which object you want manipulated
when you pass it as an argument to a method.

I hope this helps [though you may need to rephrase your question]

Anthony Borla
 
J

Jose Rubio

Something like

manipulate( cCustomPoint aPoint )
{
aPoint.setX( whatever );

etc. etc. etc.
}

So you would call this method passing the object you want like this:

manipulate( MyPoint1 );
 
R

Roderik

Roderik said:
Hi,

I'd like to make a function to manipulate objects of a
given class.

Let's say there are two objects, called MyPoint1
and MyPoint2. They are both of the class
cCustomPoint and contain an x an y attribute.

The function should assign a value for x to a passed
object (MyPoint1 or MyPoint2).

class cCustomPoint
{
public int getX() { return x; }
public int getY() { return y; }
public int setX(int x) { this.x = x; }
public int setY(int y) { this.y = y; }

private int x, y;
}

class cCustomPointManager
{
...
public void passXTocCustomPoint(int x, cCustomPoint ccp)
{
ccp.setX(x);
}
...
}

...
public static void main(String[] args)
{
...
cCustomPointManager ccpm = new cCustomPointManager();
...
cCustomPoint MyPoint1 = new ..., MyPoint2 = new ...;
...
...
ccpm.passXTocCustomPoint(5, MyPoint1);
ccpm.passXTocCustomPoint(6, MyPoint2);
...
...
How should I make this function so it knows which
object to manipulate and the value to be assigned?

I'm not sure what you are asking here. As the sample code [a very contrived
example at that] shows, you normally know which object you want manipulated
when you pass it as an argument to a method.

I hope this helps [though you may need to rephrase your question]

Anthony Borla

Yes, this is okay.

thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top