S

#### stevencheng_2007

I got a CER from a client said that a system.overflowexception thrown
when executing the following stentence:
Vertor3 v = p1 - p2;
Here the p1, p2 are variables of self defined struct --- Point3,
Vector3 is also a self defined struct type, they are definitions as
below:
struct Point3 struct
Vector3
{ {
float x, y, z;
float x, y, z;
} }

Point3 overode its -operation:
struct Point3
{
public static Vector3 operator - (Point3 p1, Point3 p2)
{
return new Vector3(p1.x-p2.x, p1.y - p2.y, p1.z - p2.z);
}
}

As I know that the arithmetic between the float type never throw
system.overflowexception.
So, the exception only can be thrown when create a Vector3.
Vector3 has three type constructs:
1. public Vector3(float x, float y, float z)
2. public Vector3(double x, double y, double z)
3. public Vector3(int x, int y, int z)
That means during runtime the construct --- Vector3(int x, int y, int
z) was invoked which eventually caused this exception.
Does anyone could help me to explain how could this case happen? or
there are some errors in my analysis?

Thanks,

Steven

M

#### Marc Gravell

That means during runtime the construct --- Vector3(int x, int y, int z) was invoked

What makes you think this? Can you post the ctor code at all? float-
float=> float, so your line Vector3(p1.x-p2.x, p1.y - p2.y, p1.z -
p2.z) should invoke Vector3(float,float,float). Eitherway, float will
do an implicit cast to double, but not to int - so it won't be using
the int ctor unless you are twisting its arm.

I think the devil may be in the detail here... can you post the actual
code for Point3 and Vector3 at all? In particular the - operator, the
constructor, and the field/property members for x,y,z

Marc

S

#### stevencheng_2007

Thank you Marc and I so impressed for the quick reply.
The Vector3's constructors as following:
public struct Vector3
{
float _x, _y, _z;
public Vector3(int x, int y, int z)
{
_x = x;
_y = y;
_z = z;
}

public Vector3(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}

public Vector3(double x, double y, double z)
{
_x = (float)x;
_y = (float)y;
_z = (float)z;
}
}

I thought that the system.overflowexception only could be thrown by an
integer type, so I guessed the constructor Vector3(int x, int y, int
z) being called.

The -operation of the Point3 just exactly as I wrote.
For the whole actual code, because I am in home now, you know, I can
not recall it nicely.

Steven

}

I

#### Ignacio Machin \( .NET/ C# MVP \)

Hi,

Did you try to debug it? and go into the methods?

Also a short but a complete example will help as we could run it and try
ourselves.

M

#### Marc Gravell

And Point3? again, in particular the properties/fields... I can't seee
anything obvious; are you sure about where it blows up?

Marc