Accessing complex objects through properties

J

Jason Bell

Every example of properties I've seen have used simple types such as
integers and strings.

Here's the scenario I'm trying to work out (3D graphics programming):

I have a class called Matrix16f, containing 16 floats that define the
matrix, which in turn defines an object's position and orientation in 3D
space. As well I want it to have a "position" property, which is a 3D
vector that sets the Position values of the matrix.

public class Vector3f
{
public float x;
public float y;
public float z;

public void Set( float X, float Y, float Z )
{
x = X; y = Y; z = Z;
}
}

public class Matrix16f
{
private float Values[16];

public Vector3f Position
{
set
{
Values[12] = value.x;
Values[13] = value.y;
Values[14] = value.z;
}
}
}

So ideally I'd like to be able to do something like:

SomeMatrix.Position.z = 12.4f;

This doesn't work though: accessing the members of an object through a
property seems to have no effect. Instead you'd have to do something
like:

SomeVector.Set( 0.0f, 0.0f, 12.4f );
SomeMatrix.Position = SomeVector;

Which is obviously uglier. Any thoughts on how to do it using the first
approach?
 
D

Dmitriy Lapshin [C# / .NET MVP]

Jason,
SomeMatrix.Position.z = 12.4f;

This is perfectly possible, but you will have to store the matrix as a set
of three Vector3f instances and return the appropriate Vector3f instance as
the getter of the Position property.
Or, modify the Vector3f class to be able to store its data in specified
array at specified indices. In this case, you can construct an instance of
Vector3f every time the Position property is queried, telling the instance
to use Values[12], Values[13] and Values[14];

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Jason Bell said:
Every example of properties I've seen have used simple types such as
integers and strings.

Here's the scenario I'm trying to work out (3D graphics programming):

I have a class called Matrix16f, containing 16 floats that define the
matrix, which in turn defines an object's position and orientation in 3D
space. As well I want it to have a "position" property, which is a 3D
vector that sets the Position values of the matrix.

public class Vector3f
{
public float x;
public float y;
public float z;

public void Set( float X, float Y, float Z )
{
x = X; y = Y; z = Z;
}
}

public class Matrix16f
{
private float Values[16];

public Vector3f Position
{
set
{
Values[12] = value.x;
Values[13] = value.y;
Values[14] = value.z;
}
}
}

So ideally I'd like to be able to do something like:

SomeMatrix.Position.z = 12.4f;

This doesn't work though: accessing the members of an object through a
property seems to have no effect. Instead you'd have to do something
like:

SomeVector.Set( 0.0f, 0.0f, 12.4f );
SomeMatrix.Position = SomeVector;

Which is obviously uglier. Any thoughts on how to do it using the first
approach?
 

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

Top