Author Topic: Overloading types.  (Read 9764 times)

0 Members and 2 Guests are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1882
Overloading types.
« on: May 22, 2007, 12:00:02 AM »
This overloading is you beaut, but it seems like sometimes you need a few extra choices.

Code: [Select]
       
 static public Arc Arc(Point3d Cen,Vector3d V, double Rad,double startAng,double endAng,string sOwner) 
            //standard arc
 
        static public Arc Arc(Center Center, Point3d StartP, Point3d EndP, string sOwner)
        //3 point arc with center
   
        static public Arc Arc( Point3d StartP,Point3d MidP, Point3d EndP, string sOwner)
        //3 point arc
   


Both the 3 point arc with center and the 3 point arc  have the same arguments so I was messing with making Center a class just to make it different than a Point3d, I would almost rather it was an enum but I can't figure that out.
Any ideas?

sinc

  • Guest
Re: Overloading types.
« Reply #1 on: May 22, 2007, 12:30:41 AM »
This is a really heavy-handed reason for using a class, or even a struct.  For a case like that, I would tend to lean toward creating another method called Arc3P() or something like that.

If you want to use an enum, you could do that too, but it would require adding another argument to your command, which gets kind of messy:

Code: [Select]
public enum ArcCreationMethod
{
    CenterAndEndpoints,
    ThreePointCurve
}

static public Arc Arc(ArcCreationMethod method, Point3d param1, Point3d param2, Point3d param3, string sOwner)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Overloading types.
« Reply #2 on: May 22, 2007, 12:47:01 AM »
I'm giving this a go, thanks.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Overloading types.
« Reply #3 on: May 22, 2007, 01:36:42 AM »
So far it doesn't seem to work,
 Error   1   Type 'Util.AddEnt' already defines a member called 'Arc' with the same parameter types.
But I do like your Arc3P() idea.
string sOwner is a way to figure out if the item is to be added to Paperspace,modelspace,Currentspace or block.
This is where I started thinking about enums and while they work perfect for the first 3, I can't figure out how to pass either a stringname or an id for the fourth.   

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Overloading types.
« Reply #4 on: May 22, 2007, 02:13:00 AM »
You are overloading your constructor correct?
If so, create a few constructors as you have but make sure you have a default ctor that takes no arg's. This way you can create a few arc creation methods that can return a new arc as a return type (as your ctor is really doing) and return a new arc to the 'null' arc you defined like-

Arc myarc;
myarc = Arc.Arc2pntAng(...);

In fact, you could simply call your arc create methods inside your ctors passing the param's as required that way you still have both choices to assign on definition or after you declare -

public Arc Arc(p1,p2,angle)
{
    return = Arc2pntsAng(p1,p2,ang);
}

// the method
public Arc Arc2pntsAng(p1,p2,angle)
{
    Arc tmparc = new Arc();
    tmparc = Arc2pntsAng(p1,p2,ang);
    return tmparc;
}

<edit> added ctor and method

you may have to make the class methods static though and you will still have only limited ctor's but at least you still have a way to create different methods. Perhaps not create ctor's at all and just enforce the creation??
It's only food for thought though, it's been a while with c# :)

hth.
« Last Edit: May 22, 2007, 02:17:57 AM by MickD »
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Glenn R

  • Guest
Re: Overloading types.
« Reply #5 on: May 22, 2007, 02:19:09 AM »
Instead of 'sOwner', why not pass in the ObjectId of the block table record to add it to...? (ie. the current space for example)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Overloading types.
« Reply #6 on: May 22, 2007, 02:43:23 AM »
Mick, I think if I do that I still have to start with 2 ctors  e.g public Arc Arc(p1,p2,p3) and public Arc Arc(p1,Cen,p2) that have the same parameter list.

Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.
 
Code: [Select]
static private void AddEntTtoDb(Entity Ent, string sOwner)
        {
            BlockTable bt;
            BlockTableRecord btr;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                switch (sOwner)
                {
                    case "Ms":
                        btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        break;
                    case "Ps":
                        btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
                        break;
                    case "Cs":
                        btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        break;
                    default:
                        if ((bt.Has(sOwner)))
                        {
                            btr = (BlockTableRecord)trans.GetObject(bt[sOwner], OpenMode.ForWrite);
                            Util.GetandPrint.Print("The block " + btr.Name + " exists");
                        }
                        else
                        {

                            btr = new BlockTableRecord();
                            btr.Name = sOwner;
                            bt.Add(btr);
                            trans.AddNewlyCreatedDBObject(btr, true);

                        }

                        break;
                }

                btr.AppendEntity(Ent);
                trans.AddNewlyCreatedDBObject(Ent, true);

       
            trans.Commit();
            }

So I don't need to know the id's but I already know the block name.
Perhaps the best way is a simple Constant
        public const string Ms = "ModelSpace";
        public const string Ps = "PaperSpace";
        public const string Cs = "CurrentSpace";
While this is not as nice as an enum it is available in intellisense.


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Overloading types.
« Reply #7 on: May 22, 2007, 04:34:33 AM »

Bryco, did you know these were equivalent ?

Have a look at the Fields .Modelspace and  .Paperspace of the BlockTableRecord .. The fields are String Constants, which you could pass in in you wanted to ..
Code: [Select]
              BlockTableRecord modelSpaceRecord1 = (BlockTableRecord)tr.GetObject(
                  bt[BlockTableRecord.ModelSpace],
                  OpenMode.ForWrite);   

              BlockTableRecord modelSpaceRecord2 = (BlockTableRecord)tr.GetObject(
                  bt["*MODEL_SPACE"],
                  OpenMode.ForWrite);

.... but I'd personally pass the ObjectID of the container I wanted the Ent added to .. as Glenn mentioned.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Overloading types.
« Reply #8 on: May 22, 2007, 06:09:07 AM »
.... but I'd personally pass the ObjectID of the container I wanted the Ent added to .. as Glenn mentioned.

Sorry to ask, but may we have an example?

sinc

  • Guest
Re: Overloading types.
« Reply #9 on: May 22, 2007, 08:15:28 AM »
I had started getting the impression you are trying to define a Util.AddEnt class, and these were methods that created arcs in your utility class.

If they are constructors for an Arc class, they should not have a return value.

Code: [Select]
public class Arc
{
    ; this is a constructor
    public Arc()
   {
   }
}
Code: [Select]
using SomethingWithAnArcClass;
namespace Util;
{
    public class AddEnt()
    {
        ; this is the constructor for the AddEnt class
        public AddEnt()
        {
        }

        ; this is NOT a constructor
        public Arc Arc()
       {
       }
    }
}
In any case, the only way to use method overloading is to have arguments with distinct types.  That's how overloading works.  But overloading is simply a convenience for the programmer; it is not a goal.  You are gaining nothing good by trying to FORCE your code to use overloading.  All overloading lets you do is avoid having a lot of methods like ArcFromStartAndEndAngles(), ArcFromCenterAndTwoPoints(), and Arc3P().  Overloading lets you use the same name for the first two.  But because the arguments are the same, you can't use operator overloading for the last one.  Instead, you can use the names Arc(), Arc(), and Arc3P(), and the compiler will be able to tell the difference between the first two methods by looking at the arguments.

LE

  • Guest
Re: Overloading types.
« Reply #10 on: May 22, 2007, 10:25:39 AM »
I do not know what you are doing...

Quote
static private void AddEntTtoDb(Entity Ent, string sOwner)

If you have a chance, look for the "MgdDbg" project by Jim Awe from Adesk, and read the "SymTbl.cs"

http://discussion.autodesk.com/thread.jspa?messageID=4907654

hth

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Overloading types.
« Reply #11 on: May 22, 2007, 11:46:30 AM »
I do not know what you are doing...

Quote
static private void AddEntTtoDb(Entity Ent, string sOwner)

If you have a chance, look for the "MgdDbg" project by Jim Awe from Adesk, and read the "SymTbl.cs"

http://discussion.autodesk.com/thread.jspa?messageID=4907654

hth


That's it!
Thanks Luis

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Overloading types.
« Reply #12 on: May 23, 2007, 12:07:00 AM »
Kerry,*MODEL_SPACE point taken.
sinc, good advice again.
Le, that's opened plenty of doors. Great find.


TonyT

  • Guest
Re: Overloading types.
« Reply #13 on: May 26, 2007, 03:44:32 AM »
Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.

Well, if I may say so myself, I've found that trying to invent
your own API that offers the 'full monty' way of doing things,
will ultimately create more problems than it solves.

API design is an art, and is not as simple as it often seems.

The problem with your approach to a 'full monty' way of
adding an ent to a block, is that if the code is used in a
context where the name of the block is coming from user
input, and the user intended to give the name of an
existing block, but mistyped it, your code just creates the
block and doesn't return an error.

The point here is that you really can't avoid the 'verbose'
nature of ObjectARX programming, and this is an excellent
example of why.

If the code that calls that sub doesn't do what it should to
check to see if a block exists (e.g., the verbose code that
your full monty sub is supposed to avoid) when it knows or
can assume that the block should exist, and rather, blindly
passes the name of the block to your full monty sub, you
will quickly find your code riddled with hard to find bugs.

You also haven't learned about the problem with the default
indexer and the Has() method yet (they return erased entries),
and that means that to find out if a block exists, you must
test the IsErased property of the object id returned by the
default indexer, and if it is erased, you must iterate over the
entire table to find another non-erased entry with the same
name.

So, while your sub may seem seem like a great convenience
at its conception, I think after using it for real, you will quickly
learn that it has the potential to be more of a problem than a
solution.  I'm not trying to knock it down for the sake of doing
that, I'm trying to show why someone that is just learniing to
use an API is not yet able to see every potential problem in
their code until the one using it is unceremoneously confronted
with an exception dialog,

It's better bite the bullet and contend with the verbose nature
of the API, and focus on learning to use it, rather than trying
to come up with ways of hiding or insulating yourself from it,
as this sub appears to be a case of.

It's hard to learn an API if you're trying to hide from it :)

« Last Edit: May 26, 2007, 03:48:31 AM by TonyT »

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Overloading types.
« Reply #14 on: May 26, 2007, 11:28:59 AM »
Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.

Well, if I may say so myself, I've found that trying to invent
your own API that offers the 'full monty' way of doing things,
will ultimately create more problems than it solves.

<snip>
I traversed this same path, asked myself the same questions you have asked yourself, and lamented all the extra work involved in using this API.  But in the end, I agree with Tony.

We all know however that we are lazy and typing is not how we want to spend our days.  If you're using Visual Studio as your IDE, I suggest Code Snippets.  You can create snippets that encapsulate a logical action, like adding an entity to a database, although most of mine aren't quite that chunky, and modify the code after it's inserted. 
Bobby C. Jones