.NET

.NET

Reply
Distinguished Contributor
wesbird
Posts: 359
Registered: ‎12-05-2003
Message 1 of 5 (100 Views)

What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

100 Views, 4 Replies
10-11-2005 12:31 PM
Hi,
I tried to create a function to calculate the center of a pline. first I like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
Vault Pro 2014, Windows 7 64 bit, AutoCAD (ACA, Map) 2014
*J. Daniel Smith
Message 2 of 5 (100 Views)

Re: What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

10-11-2005 02:37 PM in reply to: wesbird
Throw an ArgumentNullException instead; nullable types are coming in .NET
2.0.

If you really must have a "null" Point3d (throwing the exception is better),
initialize a read-only variable to something that makes sense for you such
as Double.MaxValue or Double.MinValue or maybe Double.NaN.

Dan

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I
like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a
value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
*Tony Tanzillo
Message 3 of 5 (100 Views)

Re: What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

10-11-2005 05:18 PM in reply to: wesbird
In the case of an invalid argument, as Dan says, you really
should throw an exception, because calling the function without
a valid Entity parameter is almost certainly a hard error.

When you have cases like that, you are supposed to throw an
exception. If you don't, and instead return null or something
equally meaningless, it only results in hiding bugs and causing
them to manifest farther from the source.

For cases where they may not be a need to return a valid
Point3D (e.g., the polyline is a straight segment with no
center point), You can use a ref paramter instead:

private bool PlineCen(Entity _pline, ref Point3d center)
{
if( ! condition ) // condition = false if _pline has no center point
return false;
else
{
center = ComputeCenter(_pline); compute center point
return true;
}
}

You would call it like so:

Entity ent = //...
Point3d center;
if( PlineCen( ent, ref center ) )
// center point was found
else
// no center point found


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
*J. Daniel Smith
Message 4 of 5 (100 Views)

Re: What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

10-12-2005 06:45 AM in reply to: wesbird
This starts to get into a (big?) gray area. Should you throw an
ArgumentException (or ArgumentOutOfRangeException) or return a status code?
I don't think there is a single right answer, it depends on the exact
semantics of PlineCen() and whether or not this kind of failure is
"expected" for normal operations (whatever *that* means :-) ). Here
(http://www.cuj.com/documents/s=9481/cuj0408g/0408mill.html) is an article
that may help: "When, for what, and how should you use exceptions instead of
error codes to report errors?" In this column, I propose a clear and
practical answer..."

An "out" (instead of "ref") parameter might be slightly better, although
then the compiler will force you to set "center" to some value. But at
least in this case, you can use an "I don't care" value--Point3d(0.0, 0.0,
0.0)--since you "know" it will never be used (everybody will check the
return value, right?).

Finally I forget to mention this...I know the rules for C# are different
than C++, but I'd "just not go there" and use Standard C++ names for
identifers. Avoid "_pline", "pline_" if you must; but why not just "pline"
in this case?

Dan

"Tony Tanzillo" wrote in message
news:4981703@discussion.autodesk.com...
In the case of an invalid argument, as Dan says, you really
should throw an exception, because calling the function without
a valid Entity parameter is almost certainly a hard error.

When you have cases like that, you are supposed to throw an
exception. If you don't, and instead return null or something
equally meaningless, it only results in hiding bugs and causing
them to manifest farther from the source.

For cases where they may not be a need to return a valid
Point3D (e.g., the polyline is a straight segment with no
center point), You can use a ref paramter instead:

private bool PlineCen(Entity _pline, ref Point3d center)
{
if( ! condition ) // condition = false if _pline has no center point
return false;
else
{
center = ComputeCenter(_pline); compute center point
return true;
}
}

You would call it like so:

Entity ent = //...
Point3d center;
if( PlineCen( ent, ref center ) )
// center point was found
else
// no center point found


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4981339@discussion.autodesk.com...
Hi,
I tried to create a function to calculate the center of a pline. first I
like to check if pline is null. if yes, I will return null.
Now I got a error message:
"Cannot convert null to 'Autodesk.AutoCAD.Geometry.Point3d' because it is a
value type"
How Point3d is value type?

here is the code.
private Point3d PlineCen(Entity _pline)
{
// todo
if (_pline == null) return null;
...


Thank you
Wes
*Tony Tanzillo
Message 5 of 5 (100 Views)

Re: What this mean and how to fixed it: Cannot convert null to 'Autodesk.AutoCAD.Geo

10-13-2005 09:48 AM in reply to: wesbird
"J. Daniel Smith" wrote:

>> This starts to get into a (big?) gray area.
>> Should you throw an ArgumentException
>> (or ArgumentOutOfRangeException) or return
>> a status code?

I would throw the exception. A programmer does
not have to check a result, and if they don't and
the code is allowed to continue running, then it
can result in corruption of data.

If an exception is thrown, and the programmmer
calling the function does not handle it, then the
program will stop, which is what should happen.

That view is based on the notion that the worse
kind of bugs are not the ones that cause code to
fail, but rather, allow it to continue executing, in
a corrupt state.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.