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

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Array of arrays vs Class
« Reply #15 on: May 17, 2007, 12:35:04 AM »
Ahh, the stack size eh!, thanks Dan...still sucks though :laugh:
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

TonyT

  • Guest
Re: Array of arrays vs Class
« Reply #16 on: May 17, 2007, 02:02:00 AM »
Hi Tim. Don't use a struct for something like that.

I don't know if anyone mentioned it, but a struct is a value type,
which when passed to a function as an argument, is passed by
value (e.g., a copy of the struct is created and passed).

When you pass an instance of a class to a function, only a
reference to the class is passed, and there is no copy made.

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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Array of arrays vs Class
« Reply #17 on: May 17, 2007, 10:38:29 AM »
  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?
I'm still on '06, and they told me not to go to .Net 2.0.  I have the newer version on my system, but just not using it right now.

Quote from: TonyT
Hi Tim. Don't use a struct for something like that.

I don't know if anyone mentioned it, but a struct is a value type,
which when passed to a function as an argument, is passed by
value (e.g., a copy of the struct is created and passed).

When you pass an instance of a class to a function, only a
reference to the class is passed, and there is no copy made.
Thanks Tony.

So I guess the way to go is a class per what everyone is saying here.  I have meetings this morning, so I will have to look into this in the afternoon.

Thanks once again all.
Tim

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

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Array of arrays vs Class
« Reply #18 on: May 17, 2007, 06:35:29 PM »
Class was the way to go.  I made a class, then assigned it to the 'tag' of each item in the listview.  Then when the plot button is pressed I fill a global object array with the tags from the listview.  Works nice.
Class
Code: [Select]
public class MyPlotParams {
private string DwgPath;
private string DeviceName;
private string PaperSize;
private string ctbName;
private bool ScLw;
private int Cnt;
private Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp;
private Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot;

public MyPlotParams(){}
public MyPlotParams(string DwgPath, string DeviceName, string PaperSize, string ctbName, bool ScLw, int Cnt, Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp, Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot) {
this.DwgPath = DwgPath;
this.DeviceName = DeviceName;
this.Paper = PaperSize;
this.ctbName = ctbName;
this.ScLw = ScLw;
this.Cnt = Cnt;
this.ScTyp = ScTyp;
this.PltRot = PltRot;
}

public string DrawingPath {
get {return DwgPath;}
set {DwgPath = value;}
}

public string Device {
get {return DeviceName;}
set {DeviceName = value;}
}

public string Paper {
get {return PaperSize;}
set {PaperSize = value;}
}

public string ctbFile {
get {return ctbName;}
set {ctbName = value;}
}

public bool ScaleLineweight {
get {return ScLw;}
set {ScLw = value;}
}

public int Amount {
get {return Cnt;}
set {Cnt = value;}
}

public Autodesk.AutoCAD.DatabaseServices.StdScaleType AcScaleType {
get {return ScTyp;}
set {ScTyp = value;}
}

public Autodesk.AutoCAD.DatabaseServices.PlotRotation AcPlotRotation {
get {return PltRot;}
set {PltRot = value;}
}
}
Then when it comes to plotting the drawings.
Code: [Select]
if (DiaRslt == DialogResult.OK) {
bool IsModel;
Document tempDoc = null;
DocumentCollection DocCol = AcadApp.DocumentManager;
foreach (MyPlotParams mpp in PlotObjectsArray) {
if (mpp != null) {
try {
tempDoc = DocCol.Open(mpp.DrawingPath, true);
using (DocumentLock DocLock = tempDoc.LockDocument()) {
if (string.Compare("Model", LayoutManager.Current.CurrentLayout) == 0) {
IsModel = true;
}
else IsModel = false;
MyPlottingPart(mpp.Device,
               mpp.Paper,
               mpp.Amount,
               mpp.ctbFile,
               mpp.AcScaleType,
               IsModel,
               mpp.ScaleLineweight,
               mpp.AcPlotRotation,
               true
              );
}
}
catch (Autodesk.AutoCAD.Runtime.Exception AcadEr) {
MessageBox.Show(AcadEr.Message, "Drawing error (AutoCAD).");
}
catch (System.Exception ex){
MessageBox.Show(ex.Message, "Drawing error (System).");
}
finally {
if (tempDoc != null) tempDoc.CloseAndDiscard();
}
}
}
}

Thanks again for all the help.
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: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Array of arrays vs Class
« Reply #19 on: May 17, 2007, 06:58:31 PM »
Nice work Tim, sorry to lead you astray with the struct's (that's how I'd do it in C), we all learned something out of it though  :roll:
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Array of arrays vs Class
« Reply #20 on: May 17, 2007, 07:05:28 PM »
Nice work Tim, sorry to lead you astray with the struct's (that's how I'd do it in C), we all learned something out of it though  :roll:
True.  If I go down one path and find it's not the right one, I know I at the minimum learned something.  I appreciate your help Mick.
Tim

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

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Array of arrays vs Class
« Reply #21 on: May 18, 2007, 12:08:54 AM »
Hi Tim.

On thing I noticed about your code is that it will try to close a document
that may not have been opened, because the finally{} block executes,
regardless of whether the document was opened or not.

You might want to consider rearranging your code to ensure that it
doesn't try to close a document that may not have been opened.

The general pattern might be like this:

Code: [Select]

    try
    {
         // open document here
        if( <document was opened successfully> )
        {
            try
            {
                  // work with opened document here
            }
            catch (Exceptions that might occur while working with open document)
            {
            }
            finally
            {
                 // close the opened document
            }
        }
        catch( Exception that might occur attempting to open the document )
        {
        }
   }       // outer try


One other thing is that you can unclutter your code logic by passing
the MyPlotParams instance to MyPlottingPart() and use its properties
directly from within that function.
     
Class was the way to go.  I made a class, then assigned it to the 'tag' of each item in the listview.  Then when the plot button is pressed I fill a global object array with the tags from the listview.  Works nice.
Class
Code: [Select]
public class MyPlotParams {
private string DwgPath;
private string DeviceName;
private string PaperSize;
private string ctbName;
private bool ScLw;
private int Cnt;
private Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp;
private Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot;

public MyPlotParams(){}
public MyPlotParams(string DwgPath, string DeviceName, string PaperSize, string ctbName, bool ScLw, int Cnt, Autodesk.AutoCAD.DatabaseServices.StdScaleType ScTyp, Autodesk.AutoCAD.DatabaseServices.PlotRotation PltRot) {
this.DwgPath = DwgPath;
this.DeviceName = DeviceName;
this.Paper = PaperSize;
this.ctbName = ctbName;
this.ScLw = ScLw;
this.Cnt = Cnt;
this.ScTyp = ScTyp;
this.PltRot = PltRot;
}

public string DrawingPath {
get {return DwgPath;}
set {DwgPath = value;}
}

public string Device {
get {return DeviceName;}
set {DeviceName = value;}
}

public string Paper {
get {return PaperSize;}
set {PaperSize = value;}
}

public string ctbFile {
get {return ctbName;}
set {ctbName = value;}
}

public bool ScaleLineweight {
get {return ScLw;}
set {ScLw = value;}
}

public int Amount {
get {return Cnt;}
set {Cnt = value;}
}

public Autodesk.AutoCAD.DatabaseServices.StdScaleType AcScaleType {
get {return ScTyp;}
set {ScTyp = value;}
}

public Autodesk.AutoCAD.DatabaseServices.PlotRotation AcPlotRotation {
get {return PltRot;}
set {PltRot = value;}
}
}
Then when it comes to plotting the drawings.
Code: [Select]
if (DiaRslt == DialogResult.OK) {
bool IsModel;
Document tempDoc = null;
DocumentCollection DocCol = AcadApp.DocumentManager;
foreach (MyPlotParams mpp in PlotObjectsArray) {
if (mpp != null) {
try {
tempDoc = DocCol.Open(mpp.DrawingPath, true);
using (DocumentLock DocLock = tempDoc.LockDocument()) {
if (string.Compare("Model", LayoutManager.Current.CurrentLayout) == 0) {
IsModel = true;
}
else IsModel = false;
MyPlottingPart(mpp.Device,
               mpp.Paper,
               mpp.Amount,
               mpp.ctbFile,
               mpp.AcScaleType,
               IsModel,
               mpp.ScaleLineweight,
               mpp.AcPlotRotation,
               true
              );
}
}
catch (Autodesk.AutoCAD.Runtime.Exception AcadEr) {
MessageBox.Show(AcadEr.Message, "Drawing error (AutoCAD).");
}
catch (System.Exception ex){
MessageBox.Show(ex.Message, "Drawing error (System).");
}
finally {
if (tempDoc != null) tempDoc.CloseAndDiscard();
}
}
}
}

Thanks again for all the help.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Array of arrays vs Class
« Reply #22 on: May 21, 2007, 11:20:40 AM »
Thanks for the points Tony.  I like them, and will update my code when I get a chance to.
Tim

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

Please think about donating if this post helped you.

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Array of arrays vs Class
« Reply #23 on: May 21, 2007, 02:24:35 PM »
Silly question, I'm not even coding in .NET yet, but isn't an array of arrays the same as a multi-dimensional array?

IE: Dim 2DArray (0 to 5, 0 to 10) as double

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Array of arrays vs Class
« Reply #24 on: May 21, 2007, 02:31:16 PM »
Silly question, I'm not even coding in .NET yet, but isn't an array of arrays the same as a multi-dimensional array?

IE: Dim 2DArray (0 to 5, 0 to 10) as double
Yea (I think so).  Array of arrays just came to mind while typing.  :-)
Tim

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

Please think about donating if this post helped you.

sinc

  • Guest
Re: Array of arrays vs Class
« Reply #25 on: May 21, 2007, 08:49:45 PM »
Doesn't a two-dimensional array have to be "rectangular"?  In other words, array[5][10] has five rows, each of which has 10 columns, but an array of arrays might imply that each row has a different number of columns?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Array of arrays vs Class
« Reply #26 on: May 21, 2007, 11:17:51 PM »
............. imply that each row has a different number of columns?

In that case it becomes a jagged array, yes ?

http://msdn2.microsoft.com/en-us/library/2s05feca(VS.80).aspx
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.

sinc

  • Guest
Re: Array of arrays vs Class
« Reply #27 on: May 22, 2007, 12:01:28 AM »
Yeah, like that.