Author Topic: arrays  (Read 10255 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: arrays
« Reply #15 on: July 29, 2014, 06:27:27 PM »
Visual studio will make you forget about VIM  :evil:

I will find and hunt you down like the dog you are!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #16 on: July 29, 2014, 06:29:35 PM »
LOL! :D
Ok I will but I planned on trying MonoDevelop as well so I can be cross-platform (No MS Windows at home) so I can't promise I'll stick with Visual Studio.

I'm using MonoDevelop in Unity. Not a bad editor .. but I prefer MSVS
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: arrays
« Reply #17 on: July 29, 2014, 06:43:01 PM »
LOL! :D
Ok I will but I planned on trying MonoDevelop as well so I can be cross-platform (No MS Windows at home) so I can't promise I'll stick with Visual Studio.

I'm using MonoDevelop in Unity. Not a bad editor .. but I prefer MSVS
Hummm… Interesting.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #18 on: July 29, 2014, 06:48:09 PM »
Just realised there is another question buried in there ...
-i.e. Why use a foreach when a for statement will be much cleaner.



The foreach knows how to iterate arrays and collections without us having to do the counting to determine the index.
Personally I prefer it.

This is interesting.
http://www.dotnetperls.com/for-foreach
http://www.dotnetperls.com/foreach
http://www.dotnetperls.com/collections


« Last Edit: July 29, 2014, 06:51:16 PM by Kerry »
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: arrays
« Reply #19 on: July 29, 2014, 06:59:57 PM »
RE: For-Foreach:
That is interesting. However, I find it a little funny that the author uses post-incriment while talking about micro-benchmarking; in C++ there can be a lot of extra cost to post-incriment so I've got in the habit of using pre-incriment at all costs. Of course the overhead is negligible for built in types like INT, FLOAT and such but custom classes and whatnot…well, that's a funny mistake as long as its in someone else's code.

Off to read the others now.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: arrays
« Reply #20 on: July 29, 2014, 07:02:30 PM »
< .. >

One last tiny pet peeve... When dealing with booleans, stop re-evaluating if they're true or false.  Boolean functions are already evaluated to be either true or false, therefore a comparison is just wasted cycles.
Code - C#: [Select]
  1. // This...
  2. if (acLineTypTbl.Has(sLineTypName) == false)
  3. {
  4.     acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  5. }
  6.  
  7. // Becomes this:
  8. if (!acLineTypTbl.Has(sLineTypName))
  9. {
  10.     acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  11. }
  12.  

Good point Parrish.
« Last Edit: July 29, 2014, 07:34:00 PM by Kerry »
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.

JONTHEPOPE

  • Guest
Re: arrays
« Reply #21 on: July 30, 2014, 01:33:29 PM »
Thanks everyone for the advice I have two books instant and c# something.
They are very cryptic and they run ahead too fast.
I am also really enjoying the disscussions on this board.
I am very much a beginner and any advice from someone experienced helps alot.

class : new class
 
« Last Edit: July 30, 2014, 01:50:31 PM by JONTHEPOPE »

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: arrays
« Reply #22 on: July 30, 2014, 02:32:25 PM »
Good JONTHEPOPE. Take all the advice you can get and study as much code as you can. Find an opensource project and study the code to pick up tricks.

As I said, I dont know C# yet but I have some general programming habits from my C++ I can share.

Here is my quick run through of your code.
Code - C#: [Select]
  1. foreach ( string linetype in acLinetypes ) {
  2.     // give me a space after the paren's
  3.     if ( !acLineTypTbl.Has(sLineTypName) ) {
  4.         // decide to use brackets or not. Don't mix.
  5.         // If you do decide to mix, decide upon a consistant
  6.         // method and stick to it.
  7.             acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  8.         }
  9. }
  10.  
  11. //
  12. // ...
  13. //
  14.  
  15. int nCnt = 0;
  16.  
  17. foreach ( string sLayerName in sLayerNames ) {
  18.     // Find a different variable name; only difference between the variable
  19.     // and the object is one letter. DO NOT DO THIS!
  20.     //
  21.     if ( !acLyrTbl.Has(sLayerName) ) {
  22.         using ( LayerTableRecord acLyrTblRec = new LayerTableRecord() ) {
  23.             acLyrTblRec.Name = sLayerName;
  24.  
  25.             if ( !acLyrTbl.IsWriteEnabled ) {
  26.                 acLyrTbl.UpgradeOpen();
  27.             }
  28.  
  29.             acLyrTbl.Add(acLyrTblRec);
  30.             acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  31.  
  32.             acLyrTblRec.Color = acColors[nCnt];
  33.  
  34.         }
  35.     } else {
  36.         LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
  37.                                                          OpenMode.ForWrite) as LayerTableRecord;
  38.         acLyrTblRec.Color = acColors[nCnt];
  39.     }
  40.     // nCnt = nCnt + 1;
  41.     //  Don't get in the habit of doing this. The C language
  42.     //  programmers will have your hide. If all you want to do
  43.     //  is to incriment a variable use pre or post-incriment
  44.     //  and move on. This method is easier to understand at a glance
  45.     ++nCnt;
  46. }
  47.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: arrays
« Reply #23 on: July 30, 2014, 03:37:15 PM »
<snippy>Find an opensource project and study the code to pick up tricks.

THIS.  Not just for reading, but also submitting code can generate some excellent feedback, and its also excellent exposure to managed code development including using diff's, version control, and bug reporting.  One of the reasons I know what I do (including some interesting genetic algorithm stuff) is from doing exactly that.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Locke

  • Guest
Re: arrays
« Reply #24 on: July 30, 2014, 03:42:41 PM »
One of the reasons I know what I do (including some interesting genetic algorithm stuff) is from doing exactly that.

Hmm, we may be talking soon.  I have a genetic algorithm I'm using to play battleship really well, and I need minds to bounce concepts off of.  (Genome design is what I'd like feedback on)

LE3

  • Guest
Re: arrays
« Reply #25 on: July 30, 2014, 03:53:29 PM »
THIS.  Not just for reading, but also submitting code can generate some excellent feedback, and its also excellent exposure to managed code development including using diff's, version control, and bug reporting.  One of the reasons I know what I do (including some interesting genetic algorithm stuff) is from doing exactly that.

Is interesting to see that on all your posts, there is no code sample or even partial chunks it is added, as a complement, is there any code around, that we/or me can have a look, in case there is something, please post the url link(s) ?

Thanks, LE!

Jeff H

  • Needs a day job
  • Posts: 6149
Re: arrays
« Reply #26 on: July 30, 2014, 06:20:09 PM »

How do I create a new array with linetypes and extract data from the slayernames array into the linetypes array.
Also am I trying to do too many things inside a method. Any help would save me from spontaneous combustion.

Code - C#: [Select]
  1.  
  2. [CommandMethod("assignlinetype")]
  3. public static void assignlinetype()
  4. {
  5.     Document acDoc = Application.DocumentManager.MdiActiveDocument;
  6.     Database acCurDb = acDoc.Database;
  7.  
  8.     using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  9.     {
  10.         LayerTable acLyrTbl;
  11.         acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
  12.                                         OpenMode.ForRead) as LayerTable;
  13.  
  14.         LinetypTable acLineTbl;
  15.         acLineTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
  16.                                         OpenMode.ForRead) as LinetypeTable;
  17.  
  18.         string[] acLinetypes = new string[3];
  19.         acLinetypes[0] = "hidden10"
  20.         acLinetypes[1] = "center10"
  21.         acLinetypes[2] = "dot10"
  22.  
  23.        foreach  (string linetype in acLinetypes)    
  24.          if (acLineTypTbl.Has(sLineTypName) == false)
  25.         {
  26.             acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
  27.         }
  28.  
  29. // how do I error handle this if true???
  30.  
  31.         string[] sLayerNames = new string[3];
  32.         sLayerNames[0] = "ACIRed";
  33.         sLayerNames[1] = "TrueBlue";
  34.         sLayerNames[2] = "ColorBookYellow";
  35.  
  36.         Color[] acColors = new Color[3];
  37.         acColors[0] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  38.         acColors[1] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  39.         acColors[2] = Color.FromColorIndex(ColorMethod.ByAci, 1);
  40.  
  41. // can I have an array here that references linetypes to the layers?
  42.                                    
  43.         int nCnt = 0;
  44.  
  45.         foreach (string sLayerName in sLayerNames)
  46.         {
  47.             if (acLyrTbl.Has(sLayerName) == false)
  48.             {
  49.                 using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
  50.                 {
  51.                     acLyrTblRec.Name = sLayerName;
  52.  
  53.                     if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
  54.  
  55.                     acLyrTbl.Add(acLyrTblRec);
  56.                     acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
  57.  
  58.                     acLyrTblRec.Color = acColors[nCnt];
  59.                  
  60.  
  61. // should there be a linetype reference here ?
  62.                      
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
  68.                                                                  OpenMode.ForWrite) as LayerTableRecord;
  69.  
  70.                 acLyrTblRec.Color = acColors[nCnt];
  71.  
  72. // should there be a linetype reference here do I load the linetype table record for write now ?
  73.  
  74.             }
  75.             nCnt = nCnt + 1;
  76.         }
  77.         acTrans.Commit();
  78.     }
  79. }

I do not understand what you are asking?
Forgot about arrays, loading linetypes, etc... what are you trying to accomplish?


dgorsman

  • Water Moccasin
  • Posts: 2437
Re: arrays
« Reply #27 on: July 30, 2014, 06:33:29 PM »
THIS.  Not just for reading, but also submitting code can generate some excellent feedback, and its also excellent exposure to managed code development including using diff's, version control, and bug reporting.  One of the reasons I know what I do (including some interesting genetic algorithm stuff) is from doing exactly that.

Is interesting to see that on all your posts, there is no code sample or even partial chunks it is added, as a complement, is there any code around, that we/or me can have a look, in case there is something, please post the url link(s) ?

Thanks, LE!

The lack of detailed code comes from the usual reasons - a lot of the code is modular or specialized so its a bit of work to "dial it back" for a stand-alone sample, when it comes to ownership/conflict of interest I draw the line a little tighter than most, paid work comes first, and there are those much more qualified and have the time to detail out the examples.

As for a GA sample, the one I was learning on was in MegaMek in SourceForge.  Its in Java, but the syntax is almost identical to C#.  At some point in the distant past somebody had dropped in a GA "bot" for the game but it had little documentation.  I was interested in cleaning it up (the classic bot had/has serious performance issues) so I jumped in with both feet.  I got as far as understanding how GA's work and was doing some initial planning work when work/career took off leaving little time for non-work programming.  Not to say I haven't been looking for work applications, like command prediction (think right-click command history but in reverse).
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: arrays
« Reply #28 on: July 30, 2014, 07:12:07 PM »
RE: For-Foreach:
That is interesting. However, I find it a little funny that the author uses post-incriment while talking about micro-benchmarking; in C++ there can be a lot of extra cost to post-incriment so I've got in the habit of using pre-incriment at all costs. Of course the overhead is negligible for built in types like INT, FLOAT and such but custom classes and whatnot…well, that's a funny mistake as long as its in someone else's code.

Off to read the others now.

If I may stray off topic a tad for a sec, it's still relevant in most respects though (to people new to C#/.net).

Hi John,
Inheritance and polymorphism is over emphasised in learning literature IMO, when learning it's very easy to get caught up inheriting from everything to keep things DRY, this is bad as it creates very tightly coupled code that makes your libraries almost useless for any other applications. If you are trying to write unit/integration tests for existing code you will quickly learn why inheritance can be overused very easily.

The less talked about paradigm is Interfaces, these are what make things like the 'foreach' statement very powerful. By using the ICollection interface and overriding the necessary methods you can create your own collection types that can be iterated using foreach.

It's taken me a while but I've finally got my head around unit testing and it was interfaces that provided the 'aha!' moment, by creating your own interfaces you are creating an api that you can then plug your inherited classes into as dependencies. Test driven development pretty much enforces this style of architecture and well worth investigating once you get the language basic down.

Windows .Net loves you to inherit from their classes as it ties you tightly to the technology, I don't think this was deliberate but it does make things easy when creating user controls etc. By using interfaces though you can then separate the internal logic of your custom control behavior from the api and reuse it in other platforms (think gaming or web tech).

Check out some vid's/books from Robert C. Martin (Uncle Bob) on S.O.L.I.D design, well worth a look.

"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

LE3

  • Guest
Re: arrays
« Reply #29 on: July 30, 2014, 09:21:45 PM »
...when it comes to ownership/conflict of interest I draw the line a little tighter than most, paid work comes first,

Thank you I was just curios, and I understand too.