• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

How to pass params to WriteLine method

I am trying to pass parameters into my WriteLine statement.

Code compiles and runs but the number (  i  ) is not showing up where I put the placeholder  {0}


What am I doing wrong?


for(int i=0; i < history.Length; ++i)
                  {
                        
                        history[i] = new HistoryItem(j.History[i]);
                        System.Diagnostics.Debug.WriteLine("history[{0}] was just created using j.History[{0}]",i.ToString());
                  }
0
Tom Knowlton
Asked:
Tom Knowlton
  • 4
  • 3
1 Solution
 
existenz2Commented:
I dont get the fully context of what you are trying to make, but the following format works:

for(int i=0; i < history.Length; ++i)
{      
    history[i] = new HistoryItem(j.History[i]);
  System.Diagnostics.Debug.WriteLine(string.Format("history[{0}] was just created using j.History[{0}]",i));
}

If you want to show the values from history and j.history do:
for(int i=0; i < history.Length; ++i)
{      
    history[i] = new HistoryItem(j.History[i]);
  System.Diagnostics.Debug.WriteLine(string.Format("{0} was just created using {1}",history[i],j.History[i]));
}
0
 
Tom KnowltonWeb developerAuthor Commented:
Looks like I was forgetting string.Format.


I thought that you could do parameters inside of a WriteLine:


System.Diagnostics.Debug.WriteLine("Param {0}", i);


0
 
Tom KnowltonWeb developerAuthor Commented:
Or am I thinking of

Console.WriteLine( )


????
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
existenz2Commented:
You can do it indeed with Console.WriteLine() without making use of string.Format()

Eg.
Console.WriteLine{"{0}",1);
Console.WriteLine{"{0} {1}",1,2);
Console.WriteLine{"{0} {1} {2}",1,2,3);
0
 
Tom KnowltonWeb developerAuthor Commented:
Okay....

But this does NOT work for:

System.Diagnostics.Debug.WriteLine(  )


?????


That seems dumb.  Is there a good reason for it working in one and not the other?
0
 
existenz2Commented:
That was most likely done because for debugging, normally you dont want to output (much) variables at once on your screen. You can access them in other ways easily (eg. with breakpoints). Console.WriteLine is meant to be used for console applications which require mostly a more advanced output with lots of variables.
0
 
Tom KnowltonWeb developerAuthor Commented:
I'll buy that.  :)


Thank you for restoring my sanity.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now