[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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