Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2011, 01:29 PM
Registered User
 
Join Date: Feb 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default What is the string.Format method for?

In the code on page 159 the string.Format method is used within several Debug.WriteLine statements within the function Maxima. I removed two of them and the program ran exactly the same! Below I put in boldface the two lines of code I changed.
Code:
        static int Maxima (int[] integers, out int[] indices)
                {
        Debug.WriteLine("Maximum value search started.");
        indices = new int[1];
        int maxVal = integers[0];
        indices[0] = 0;
        int count = 1;
        Debug.WriteLine(string.Format("Maximum value initialized to {0}, at element index 0.",
             maxVal));
        for (int i = 1; i < integers.Length; i++ )
        {
            Debug.WriteLine(string.Format("Now looking at element at index {0}.",i));
            if(integers[i] > maxVal)
            {
                maxVal = integers[i];
                count = 1;
                indices = new int [1];
                indices[0] = i;
                Debug.WriteLine(
                    "New maximum found. New value is {0}, at element index {1}.",maxVal, i);   
            }
            else
            {
                if (integers[i] == maxVal)
                {

                    count++;
                    int[] oldIndices = indices;
                    indices = new int[count];
                    oldIndices.CopyTo(indices, 0);
                    indices[count-1] = i;
                    Debug.WriteLine(
                        "Duplicate maximum found at element index {0}.", i);                }
            }
        }
        Trace.WriteLine(string.Format(
            "Maximum value {0} found with {1} occurences.", maxVal, count));
        Debug.WriteLine("Maximum value search completed.");
        return maxVal;
 

        }
        
    }
}
 
Old April 15th, 2011, 03:39 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

string.Format is used to create a string out from a format string. As Debug.WriteLine has this format support by itself, string.Format is not needed in that case.

Christian
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel





Similar Threads
Thread Thread Starter Forum Replies Last Post
String format gugu C# 1 April 20th, 2006 03:08 PM
Optimized method to post data & format into Excel shampabera Excel VBA 1 July 14th, 2005 09:23 AM
String.replace() method JohnD Javascript 0 August 20th, 2004 06:46 AM
Property/Method access using string values jaucourt VB.NET 2002/2003 Basics 1 February 10th, 2004 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.