( Index )


Brief Information about the April 2016 CSIG Meeting

Review of String Builder () Class from CodeGuru Article

NEWS:   http:// www.techsupportalert.com /content/microsoft- visual-studio- now-free-non- commercial-use.htm

(General Csig Info)   

                             CodeGuru Logo        

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This group is a "Special Interest Group" of the ACGNJ devoted to discussing programming languages in general and C, C++, and C++ for Windows programming in particular. Each month a small but hopefully useful program (complete with source code) is presented for discussion.

This month's topic will be a review of the CodeGuru website, www.codeguru.com, and a discussion about an article about C# StringBuilder Class by Peter Shaw.  Here's some information from the CodeGuru "about" page:

"There are millions, if not billions, of lines of code in the world. Every day, more lines of code are being created. CodeGuru is a site where tips, tricks, and other pieces of useful code can be found. CodeGuru contains thousands of articles and thousands of pieces of code that you can download, look at, modify, play with, and use. You can simply plug-and-play, or you can look at the code and learn.

CodeGuru is about the
sharing of code. Almost all of the code that you can find is presented as part of an article submitted by someone like you. These articles often explain the code and its use. Most of the people who contribute code articles to CodeGuru are the same people who found and used something from here. Sharing works.
" Excellent article on Stringbuilder Class This article can be found online at the following location: http://www.codeguru.com// csharp/csharp/ cs_syntax/ the-stringbuilder-class.html .NET Back to Basics: The StringBuilder Class
Posted by Peter Shaw on March 13th, 2016
Last month, [in CodeGuru] we saw the 'String' class in the Back to Basics column. A close relative to the 'String' class is the 'StringBuilder' class, and, as its name suggests, it's designed mostly for the purpose of constructing large strings.

If you've been around .NET for some time now, you'll know there are no end of posts from all manner of folk, arguing over which is faster, string or stringbuilder. Myself, personally I think both have their strengths and weaknesses, and no one seems particularly faster than the other, and no I'm not about to start using a high resolution timer to show the difference of a few milliseconds :-)

Where I think the string builder has its advantages is in creating large bodies of complex or structured text, such as "Rich Text" or even HTML documents. StringBuilder also tends to be more efficient, and make better use of memory when dealing with considerably large blocks of text, such as complete documents.

StringBuilder is also a better choice for string modification, especially if you're expecting those modifications to be many and large. When you modify strings using the standard string class, for every modification you make, you create a new instance of the string with the modifications in place.

Even though this might not seem like a big deal, if you're performing high speed modifications inside loops and such like, you could in theory use a large amount more memory than you need to.

A string builder does all its edits in place, so you never use more memory than the single block being used by your string builder, and while that can grow and shrink as needed, it's only ever one block of memory.

Where string builder fails in comparison to a standard string class is in operations such as searching and indexing.

So, What Can the StringBuilder Do?

Well, let's start with what it does best: building strings.

Create a new console project and make sure you have the following code within it in your program.cs file.

using System; using System.Text; namespace StringBuilderClass { class Program { static void Main() { StringBuilder sb = new StringBuilder(); Console.WriteLine("Current StringBuilder length {0}", sb.Length); Console.WriteLine("Maximum StringBuilder capacity {0}", sb.MaxCapacity); Console.WriteLine("Current StringBuilder capacity (Amount available before SB will resize itself) {0}", sb.Capacity); } } } Run the application, and you should see the following output:

Stringbuilder Jpg

Follow the link mentioned at the top to see the whole article.

About the Author
Peter Shaw

As an early adopter of IT back in the late 1970s to early 1980s, I started out with a humble little 1KB Sinclair ZX81 home computer. Within a very short space of time, this small 1KB machine became a 16KB Tandy TRS-80, followed by an Acorn Electron and, eventually, after going through many other different machines, a 4MB, ARM-powered Acorn A5000. After leaving school and getting involved with DOS-based PCs, I went on to train in many different disciplines in the computer networking and communications industries. After returning to university in the mid-1990s and gaining a Bachelor of Science in Computing for Industry, I now run my own consulting business in the northeast of England called Digital Solutions Computer Software, Ltd. I advise clients at both a hardware and software level in many different IT disciplines, covering a wide range of domain-specific knowledge—from mobile communications and networks right through to geographic information systems and banking and finance.

There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 4.5
C++ 9.0, etc.
.Net 4.5, etc.
Common Language Infrastructure

Sample Code from Previous Meetings

namespace Crop { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /////////////////////////////////////////////////////// // // Suffix _SC = Screen Coordinates // Suffix _CC = Client Coordinates // Rectangle cropRectangle_CC; Point startPoint_CC; bool isDrag = false; bool fileChanged = false; /////////////////////////////////////////////////////// // // Drag and Drop step 3 of 3. The users releases the mouse to drop the image. // private void Form1_DragDrop(object sender, DragEventArgs e) { if (fileChanged) // Prompt user. { // Title = Bad Result // Default Button = No // Message = Caution System.Windows.Forms.DialogResult Result; Result = MessageBox.Show(this, "Cropped Image will be Disregarded! Continue?", "Drop New File", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (Result != System.Windows.Forms.DialogResult.Yes) return; } if (e.Data.GetDataPresent(DataFormats.FileDrop)) { if (this.pictureBox1.Image != null) this.pictureBox1.Image.Dispose(); // Get a list of filenames but use only the 1st name. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); if (files.Length > 0) { files[0] = files[0].ToLower(); if (files[0].EndsWith(".jpg")) { try { this.pictureBox1.Image = Image.FromFile(files[0]); imageData.Text = String.Format("{0} x {1}", pictureBox1.Image.Width, pictureBox1.Image.Height); fileChanged = false; this.pictureBox1.Visible = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "E R R O R"); return; } } } } this.Activate(); } // Drag and Drop step 2 of 3. The users drags the image into the program's GUI. // Note: Step 1 is Form1 AllowDrop property has been set to true. // private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; }


Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page