Author Topic: Array of arrays vs Class  (Read 10347 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5248
Array of arrays vs Class
« on: May 16, 2007, 05:42:35 PM »
In my plot routine I use an array of arrays to store all the options for plotting.  I was thinking that I could make a class specific for this instead.  I could set up all the properties I would need, and if I need to add some later, I wouldn't have to rework the code to see the number of items in the array divided by the items per nested array, which I would have to do now.

Is this doable?  Is it a good idea?  Comments/opinions wanted and welcomed.

Thanks in advance.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Array of arrays vs Class
« Reply #1 on: May 16, 2007, 06:16:45 PM »
If your array holds arrays of the same data (say an array of strings for options) then yes, wrapping this into a class (or struct even) is a good idea.
This way you can use your class like a 'record' in a db, just create a new one each plot, set it up and add it to the array.
This would make parsing the internal arrays much neater as well.
Is that what you're proposing?
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

T.Willey

  • Needs a day job
  • Posts: 5248
Re: Array of arrays vs Class
« Reply #2 on: May 16, 2007, 06:20:55 PM »
If your array holds arrays of the same data (say an array of strings for options) then yes, wrapping this into a class (or struct even) is a good idea.
This way you can use your class like a 'record' in a db, just create a new one each plot, set it up and add it to the array.
This would make parsing the internal arrays much neater as well.
Is that what you're proposing?
Yep, that is my line of thinking.  The class/struct will have properties (string, bool, double, int and Acad specific types like StdScaleType and PlotRotation) that will correspond with what the plot engine wants.  I was just reading up on class right now.  I will have a look at struct when I get a chance.  Thanks Mick.

Edit:  This is how I was thinking of setting up my class.
Code: [Select]
public class MyPlotParams {
private string DeviceName;
private string Paper;
private string ctbName;
private bool ScLw;
private double Rot;
private int Cnt;
private Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp;
private Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot;
}
« Last Edit: May 16, 2007, 06:27:43 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Array of arrays vs Class
« Reply #3 on: May 16, 2007, 06:42:30 PM »
No prob's, I'm not to sure how structures work in .net but if you could just make your variable public it would save a lot of property get/set code, your could just asign the variables to the plot engine like -

plotEngine.Paper = myPlotParams.Paper;

I think properties are the prefered way to do it though.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Array of arrays vs Class
« Reply #4 on: May 16, 2007, 06:46:16 PM »
or a better example from the array would be -

foreach(plotparam in plotparams)//loop through the plotparams array
{
    plotEngine.Paper = plotparams[ i ].plotparam.Paper;
    plotEngine.DeviceName = plotparams[ i ].plotparam.DeviceName;
    // etc etc
}
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

LE

  • Guest
Re: Array of arrays vs Class
« Reply #5 on: May 16, 2007, 06:51:16 PM »
Tim;

Have a look into this article:

http://www.c-sharpcorner.com/UploadFile/rajeshvs/StructuresInCS11112005234341PM/StructuresInCS.aspx

I have my own examples, but wait for the masters... :)

T.Willey

  • Needs a day job
  • Posts: 5248
Re: Array of arrays vs Class
« Reply #6 on: May 16, 2007, 06:58:56 PM »
Since I call my plotting portion like
Code: [Select]
public void MyPlottingPart (
                            string DeviceName,
                            string Paper,
                            string ctbName,
                            StdScaleType ScaleType,
                            bool IsModel,
                            bool ScLws,
                            PlotRotation PltRot,
                            bool LastPlot
                           )
I would pass the properties from the class to it like Mick shows in the first response.
plotEngine.Paper = myPlotParams.Paper;
where I would make a new class object per file to plot.  I have constructed the constructor to MyPlotParms like
Code: [Select]
public MyPlotParams(){}
public MyPlotParams(string DwgPath, string DeviceName, string Paper, string ctbName, bool ScLw, double Rot, int Cnt, Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp, Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot) {
this.DwgPath = DwgPath;
this.DeviceName = DeviceName;
this.Paper = Paper;
this.ctbName = ctbName;
this.ScLw = ScLw;
this.Rot = Rot;
this.Cnt = Cnt;
this.ScTyp = ScTyp;
this.PltRot = PltRot;
}
This is the way I'm heading right now.  If it seems wrong please let me know.

Tim;

Have a look into this article:

http://www.c-sharpcorner.com/UploadFile/rajeshvs/StructuresInCS11112005234341PM/StructuresInCS.aspx

I have my own examples, but wait for the masters... :)
Luis thanks for the link.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Array of arrays vs Class
« Reply #7 on: May 16, 2007, 07:06:52 PM »
Tim;

I know that later, the ones that really know, will guide better, but on the mean time, let me post a little simple class I wrote for one of my routines, it might help, I do not know... :)

Code: [Select]
    public partial class Door : DoorsAndWindowsForm
    {
        // Declarations
        private ObjectId m_blockid       = new ObjectId();
        private const Int32 m_vertices   = 4;
        private String m_doorName        = "";
        private Double m_doorThick       = 1.75; //1-3/4"
        private Double m_doorWidth       = 0.0;
        private Double m_wallThick       = 0.0;
        private Double m_dirRight        = 0.0;
        private Double m_dirUp           = 0.0;
        private Double m_dirLeft         = 0.0;
        private Double m_swingStartAngle = 0.0;
        private Double m_swingEndAngle   = 0.0;

        public enum Names
        {
            dr90,
            dr45,
            dr30,
            dr15,
            dr180,
            drPair90,
            drPair90egress,
            drSliding,
            drPocket,
            drFolding,
            Rolling
        };

        // Default Constructor
        public Door()
        {
        }

        // Alternate constructor
        public Door(Double doorWidth, Double wallThick, int drValue)
        {
            drWidth = doorWidth;
            getDoorName(drValue);

            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ...

                Entity[] ent = new Entity[entities.Count];
                entities.CopyTo(ent, 0);
                ObjectId blockid = makeBlock("*U", ent);

                DoorId = blockid;

                // remove originals
                en1.Erase(true);
                en2.Erase(true);
                en3.Erase(true);
                en4.Erase(true);

                tr.Commit();
            }
        }

        // Properties
        public Double SwingStartAngle
        {
            get
            {
                return m_swingStartAngle;
            }
            set
            {
                m_swingStartAngle = value;
            }
        }

        public Double SwingEndAngle
        {
            get
            {
                return m_swingEndAngle;
            }
            set
            {
                m_swingEndAngle = value;
            }
        }

        public ObjectId DoorId
        {
            get
            {
                return m_blockid;
            }
            set
            {
                m_blockid = value;
            }
        }

        public Double DirRight
        {
            get
            {
                return m_dirRight;
            }
            set
            {
                m_dirRight = value;
            }
        }

        public Double DirUp
        {
            get
            {
                return m_dirUp;
            }
            set
            {
                m_dirUp = value;
            }
        }

        public Double DirLeft
        {
            get
            {
                return m_dirLeft;
            }
            set
            {
                m_dirLeft = value;
            }
        }

        public Double Thick
        {
            get
            {
                return m_doorThick;
            }
            set
            {
                m_doorThick = value;
            }
        }

        public Double drWidth
        {
            get
            {
                return m_doorWidth;
            }
            set
            {
                 m_doorWidth = value;
            }
        }

        public Double WallThick
        {
            get
            {
                return m_wallThick;
            }
            set
            {   
                m_wallThick = value;
            }
        }

        public String drName
        {
            get
            {
                return m_doorName;
            }
            set
            {
                m_doorName = value;
            }
        }

        public String getDoorName(int valor)
        {
            string block_dwg = "90 degrees swing"; //default
            switch (valor)
            {
                case 0:
                    SwingStartAngle = 0.0;
                    SwingEndAngle = c90Degrees;
                    DirRight = 0.0;
                    DirUp = c90Degrees;
                    DirLeft = Math.PI;
                    drName = "90 degrees swing";
                    block_dwg = "90 degrees swing";
                    break;
                case 1:
                    SwingStartAngle = 0.0;
                    SwingEndAngle = 0.785398;
                    DirRight = c315Degrees;
                    DirUp = c45Degrees;
                    DirLeft = c135Degrees;
                    drName = "45 degrees swing";
                    block_dwg = "45 degrees swing";
                    break;
                case 2:
                    SwingStartAngle = 0.0;
                    SwingEndAngle = 0.523599;
                    DirRight = 5.23599;
                    DirUp = 0.523599;
                    DirLeft = 2.0944;
                    drName = "30 degrees swing";
                    block_dwg = "30 degrees swing";
                    break;
                case 3:
                    SwingStartAngle = 0.0;
                    SwingEndAngle = 0.261799;
                    DirRight = 4.97419;
                    DirUp = 0.2618;
                    DirLeft = 1.8326;
                    drName = "15 degrees swing";
                    block_dwg = "15 degrees swing";
                    break;
                case 4:
                    SwingStartAngle = 0.0;
                    SwingEndAngle = Math.PI;
                    DirRight = c90Degrees;
                    DirUp = Math.PI;
                    DirLeft = c270Degrees;
                    drName = "180 degrees swing";
                    block_dwg = "180 degrees swing";
                    break;
                case 5: // ignore this door - remove from here
                    block_dwg = "Pair 90 degrees swing";
                    break;
                case 6: // ignore this door - remove from here
                    block_dwg = "Pair 90 degrees double egress";
                    break;
                case 7:
                    block_dwg = "Sliding";
                    break;
                case 8:
                    block_dwg = "Pocket";
                    break;
                case 9:
                    block_dwg = "Folding";
                    break;
                case 10:
                    block_dwg = "Rolling";
                    break;
            }
            return block_dwg;
        }
               
    }//end of Door class

Then, when I need to change/update one of the doors I just read all the data in the dictionaries and call it like:

Code: [Select]
Door door = new Door(dw, wwidth, doorNamesIndex);
Point2d pgeo = new Point2d(x, y);

// save values to door!!!
door.drName = dname;
door.WallThick = wwidth;
 door.drWidth = dw;

Super Mickey Mouse.... I know..
« Last Edit: May 17, 2007, 10:10:39 AM by LE »

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Array of arrays vs Class
« Reply #8 on: May 16, 2007, 07:13:56 PM »
That link has all you need to know, if you don't need member methods/functions there's no need to create a class. You create a struct the same as a class but as the member var's are public you just assign straight to them as you would any ather variable except you use the struct's instance name in front like -

MyStruct s = new MyStruct();
s.var1 = somevar;
s.var2 = another var
etc....
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

T.Willey

  • Needs a day job
  • Posts: 5248
Re: Array of arrays vs Class
« Reply #9 on: May 16, 2007, 07:23:19 PM »
Tim;

I know that later, the ones that really know, will guide better, but on the mean time, let me post a little simple class I wrote for one of my routines, it might help, I do not know... :)
<snip>
Thanks Luis.  This will take me some time to digest.

That link has all you need to know, if you don't need member methods/functions there's no need to create a class. You create a struct the same as a class but as the member var's are public you just assign straight to them as you would any ather variable except you use the struct's instance name in front like -

MyStruct s = new MyStruct();
s.var1 = somevar;
s.var2 = another var
etc....
This what I was thinking out of reading that article.  A struct sounds like it may be better suited for what I'm doing right now.

Thanks you two.  I'm off to play now, and tomorrow since today is almost done, and I have to take my car to the shop right after work.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7494
  • AKA Daniel
Re: Array of arrays vs Class
« Reply #10 on: May 16, 2007, 11:00:42 PM »
In my plot routine I use an array of arrays to store all the options for plotting.  I was thinking that I could make a class specific for this instead.  I could set up all the properties I would need, and if I need to add some later, I wouldn't have to rework the code to see the number of items in the array divided by the items per nested array, which I would have to do now.

Is this doable?  Is it a good idea?  Comments/opinions wanted and welcomed.

Thanks in advance.

Hi Tim,

The Framework Design Guidelines say that the maximum size of a struct should be around 16 bytes,
they should be used for making value types. In your case you should consider using a class along with something
from System.Collections.Generic.. For example List<T>

Dan
Retired

T.Willey

  • Needs a day job
  • Posts: 5248
Re: Array of arrays vs Class
« Reply #11 on: May 16, 2007, 11:20:18 PM »
In my plot routine I use an array of arrays to store all the options for plotting.  I was thinking that I could make a class specific for this instead.  I could set up all the properties I would need, and if I need to add some later, I wouldn't have to rework the code to see the number of items in the array divided by the items per nested array, which I would have to do now.

Is this doable?  Is it a good idea?  Comments/opinions wanted and welcomed.

Thanks in advance.

Hi Tim,

The Framework Design Guidelines say that the maximum size of a struct should be around 16 bytes,
they should be used for making value types. In your case you should consider using a class along with something
from System.Collections.Generic.. For example List<T>

Dan

Dan,

  Does .Net 1.1 support 'Generic's?  I didn't think it did.  I will do some research on this tomorrow.  Thanks for the idea.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7494
  • AKA Daniel
Re: Array of arrays vs Class
« Reply #12 on: May 16, 2007, 11:26:50 PM »
  Does .Net 1.1 support 'Generic's?  I didn't think it did.  I will do some research on this tomorrow.  Thanks for the idea.

Nope, May I ask why your not using a newer framework?
Retired

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: Array of arrays vs Class
« Reply #13 on: May 16, 2007, 11:27:58 PM »
...

The Framework Design Guidelines say that the maximum size of a struct should be around 16 bytes,
...

Is that for real?! I noticed that struct's derive from value type (therefore are by inheritance) and I also read that value types are 'recommended' to be 16bytes, does that apply for subclasses like struct's, it just doesn't sound right.
It sucks if it is right though, a struct is a perfect solution for something that just needs to hold data in bundles like in Tim's situation.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7494
  • AKA Daniel
Re: Array of arrays vs Class
« Reply #14 on: May 17, 2007, 12:28:37 AM »
Here is one link http://msdn2.microsoft.com/en-us/library/y23b5415(VS.71).aspx

From what I gather, it’s just a matter of efficiency. I read a great article (I need to find the link) which explained the impact of placing large objects on the managed stack. Interesting stuff.
Retired