TheSwamp

Code Red => .NET => Topic started by: David Hall on May 24, 2006, 07:03:06 PM

Title: Lab 3 reworked
Post by: David Hall on May 24, 2006, 07:03:06 PM
OK, so Kerry started this, but Im so far behind I need a refresher.  I obviously dont understand Ellispes very well.  What Im trying to do is use both of the ways to create an ellispe.  here is what I have.

Code: [Select]
Ellipse el = new Ellipse(center,Vector3d.ZAxis,new Vector3d(3,0,0),0.5,0,0);  // this works
Ellipse el = new Ellipse(); //center,Vector3d.ZAxis,new Vector3d(3,0,0),0.5,0,0);  // this doesn't
el.Center=center;
el.Normal=Vector3d.ZAxis;
el.MajorAxis= new Vector3d(3,0,0);
el.RadiusRatio=0.5;
el.StartAngle=0;
el.EndAngle=0;
If you take out the // on line 1, it all works. (You wouldnt need any other lines if // are removed)
But, with the comments, Im trying to manually put in the values passed in line one.  It pukes on the MajorAxis and Normal.  Why?
Title: Re: Lab 3 reworked
Post by: David Hall on May 24, 2006, 07:05:01 PM
Normal and MajorAxis are readonly, yet assignable in line 1???
Title: Re: Lab 3 reworked
Post by: MickD on May 24, 2006, 07:34:39 PM
That's because these private members are initialised in the constructor, when you are trying to assign or read these values they are set/get through property methods which can be created to only set or get if required to protect the inner workings of an object.
hth.
Title: Re: Lab 3 reworked
Post by: David Hall on May 25, 2006, 10:08:33 AM
Thanks Mick, that makes sense.  So if I understand you right, you cant create using those, but you could modify later using set/get statements.
Title: Re: Lab 3 reworked
Post by: David Hall on May 25, 2006, 11:16:36 AM
Next question, why would I get this error?
Quote
C:\CSHARP\LAB3\LAB3\Class.cs(100): An object reference is required for the nonstatic field, method, or property 'ClassLibrary.TEP_Class.CreateLayr()'
this is what I'm trying to do
Code: [Select]
public ObjectId CreateLayr()
{
ObjectId layerId;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor ;
Database db = HostApplicationServices.WorkingDatabase ;
Transaction trans = db.TransactionManager.StartTransaction();
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForWrite);
if (lt.Has("EmployeeLayer"))
{
layerId = lt["EmployeeLayer"];
}
else
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "EmployeeLayer";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr,true);
}
trans.Commit();
trans.Dispose();
return layerId;
}


[CommandMethod("create")]
static public void createEmployee() // This method can have any name
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor ;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans= db.TransactionManager.StartTransaction();
ObjectId objLay = CreateLayr();
.........
Its erring on the last line
Title: Re: Lab 3 reworked
Post by: Draftek on May 25, 2006, 11:49:31 AM
Hi David,

Your CreateEmployee method is declared as 'static', meaning it can be accessed thru the class without creating an instance (object) of the class.

Your CraeteLayr method is not static. You cannot access it without creating an object of your class in the calling application.

If you change the CreateLayr method to 'Static', it will work as required.

I make the same mistake all the time....
Title: Re: Lab 3 reworked
Post by: David Hall on May 25, 2006, 12:09:29 PM
ok, while that made sense, why does the Adesk version look the same as mine?  Their calling function was not static, so it worked?
Title: Re: Lab 3 reworked
Post by: David Hall on May 25, 2006, 12:10:34 PM
To confirm what i was just babbling about, I removed the static from both of my functions, and it worked.  So should they be static?
Title: Re: Lab 3 reworked
Post by: Bobby C. Jones on May 25, 2006, 01:07:52 PM
To confirm what i was just babbling about, I removed the static from both of my functions, and it worked.  So should they be static?
static vs. instance is design decision that you'll need to make.  For a utility method like CreateLayer() you'll most likely want it to be static.  You call a static method like this

Code: [Select]
<className>.CreateLayer();
Title: Re: Lab 3 reworked
Post by: MickD on May 25, 2006, 06:00:37 PM
Just to add to what Bobby said, when you create a static member variable or function it gets created in memory on loading and there will only ever be one instance of the static variable or function. This is handy as you can have your utility functions that really don't require creating an instance of a class. This is how you can declare global 'like' variables and functions.

For example, a Circle is a class but it may have many instances with different values for diameter say, so having 1 static diameter variable would make all circles the same size but if you wanted a generic function to say change the colour of a circle you could write it as static and do something like Circle.ChangeColor(mycircle); (not that you would though ;) ).

So you could think of static variables and functions as global but really they are contained in a class hence
<className>.CreateLayer();
to avoid naming clashes in your assemblies.