1

I need to convert this line in one string, because my method "DisplayMessage" only accept 1 argument, so how can I do this?

_userOptions.DisplayMessage("\nFile Generated: " +
     BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + 
     "\nTime Elapsed:  {0} minute(s) {1} second(s)",
     timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n");
casperOne
  • 73,706
  • 19
  • 184
  • 253
ale
  • 3,301
  • 10
  • 40
  • 48

9 Answers9

3

It looks like your DisplayMessage method does not allow for the string format. Try putting this entire content (everything inside the parenthesis of DisplayMessage) inside a String.Format() method. That will make it one string and still allow for the multiple parameters you are passing.

casperOne
  • 73,706
  • 19
  • 184
  • 253
IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
1

Try this:

string s= String.Format(
    "\nFile Generated: " +
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + 
    "\nTime Elapsed:  {0} minute(s) {1} second(s) {2} msec(s)\n",
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
_userOptions.DisplayMessage(s);
Marco
  • 56,740
  • 14
  • 129
  • 152
1
var msg = String.Format("\nFile Generated: {0}\nTime Elapsed: {1} minute(s) {2} second(s)\n", BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)), timeSpan.Minutes, timeSpan.Seconds);

_userOptions.DisplayMessage(msg);

This should do it...

Roja Buck
  • 2,354
  • 16
  • 26
1

I guess you want to use String.Format. This takes a string and replace {#} by the argument index.

Example:

String.Format("Hi {0}, welcome to Stack Overflow!", "ale");

You might want to have a look at How should I concatenate strings?

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
1

Your string indicates that you want to call the static Format method on the String class, like so:

_userOptions.DisplayMessage(string.Format(
    "\nFile Generated: {0}\nTime Elapsed:  {1} minute(s) {2} second(s)\n",
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)),
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10));

However, this will give you a problem, as you have more parameters than you have placeholders in the string.

Additionally, given comments regarding the use of "\n" as a new line delimiter, unless you have a specific need for that specific format (and it does not seem that you do, you aren't indicating you are writing to a file, or to something where the data is going to an external system), it's better to use Environment.NewLine, which you can use like so (note, this still does not address the fact you have more parameters than you do placeholders:

_userOptions.DisplayMessage(string.Format(
    "{0}File Generated: {1}{0}Time Elapsed:  {2} minute(s) {3} second(s){0}",
    Environment.NewLine,
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)),
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10));
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • are you sure that your parenthesis are right? Check... :) I think `+"\n"` goes into string declaration, not in params... – Marco May 02 '11 at 14:24
  • @Marco: Was already editing for that when you commented, thanks for pointing it out. – casperOne May 02 '11 at 14:30
1

This is more elegant in my opinion:

string message = string.Format("{0}File Generated: {1}{0}Time Elapsed:  {2} minute(s) {3} second(s) {4} milliseconds{0}", 
    "\n", BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)), timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
_userOptions.DisplayMessage(message);

Using Format there's no need to use any + operator on the strings.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

I think, you can use a StringBuilder

StringBuilder sb = new StringBuilder();
        sb.Append("\n");
        sb.Append("File Generated: ");
        sb.Append(BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)));
        sb.Append("\n");
        sb.Append("Time Elapsed: ");
        sb.Append(timeSpan.Minutes);
        sb.Append(" minute(s)");
        sb.Append(timeSpan.Seconds);
        sb.Append(" second(s)");
        sb.Append();
        _userOptions.DisplayMessage(sb.ToString());

but i think, you have some bug: you have 2 parameters but actually is 3

zzfima
  • 1,528
  • 1
  • 14
  • 21
0

Find below.

string str = String.Format("\n" + "File Generated: " + BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "\n" + "Time Elapsed: " + " {0} minute(s)" + " {1} second(s)", timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n");

_userOptions.DiplayMessage(str);

Hope this helps.

scartag
  • 17,548
  • 3
  • 48
  • 52
0

Instead of {0} and {1}, just use your arguments directly:

_userOptions.DisplayMessage("\n" + "File Generated: " + BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "\n" + "Time Elapsed:" + timeSpan.Minutes + "minute(s)" + timeSpan.Seconds + "second(s)" + timeSpan.Milliseconds / 10 + "\n");
Matthias
  • 12,704
  • 13
  • 35
  • 56
  • that's not a good way to write code ;) If you need to change a param you have to search in the string and modify... Params are much better IMHO :) – Marco May 02 '11 at 14:17
  • @Marco: Agreed, I was just giving the simpelest, most straight-forward solution – Matthias May 02 '11 at 14:27