1

If I want to show something in a console I usually write it like this: Console.Write("Hello{0}", AnyNumber);.

But if I use Interaction.MsgBox and write Interaction.MsgBox("Hello {0}, AnyNumber"); I am getting an error, why is that?

I like to use placeholders like {0} a lot more than + (string concatenation) when writing, I think it is easier to write it like that...

So my question is: Why can't I use placeholders in MsgBox and is there any other way to use them?

Here is a example how I would like to use it in MsgBox:

static void multiplikation(int tal1, int tal2)
{        
    if (tal1*tal2 == 100)
    {
          Interaction.MsgBox("Hello!\nResult:\n" + (tal1 * tal2));
    }
    else
    {
          Interaction.MsgBox("Resultat:\n" + (tal1 * tal2));
    }
}
static void addition(int tal1, int tal2)
{
    if (tal1 + tal2 == 100)
    {
         Console.Write("Hello!\nResult:{0}\n", (tal1 + tal2));
    }
    else
    {
         Console.Write("Resultat:{0}\n", (tal1 + tal2));
    }
}
Matt
  • 25,467
  • 18
  • 120
  • 187
user3356636
  • 115
  • 1
  • 11

4 Answers4

5

Interaction.MsgBox receives a string as its first parameter. If you want to format a string, you have to do that yourself, and pass the formatted string to Interaction.MsgBox:

Interaction.MsgBox(string.Format("Hello!\nResult:{0}\n", (tal1 + tal2)));

On the other hand, Console.Write was written with overloads that allow the formatting to be performed inside Console.Write.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

This is due to the fact that unlike Console.Write, Interaction.Msgbox does not know anything about string.Format: It just displays the string "as is".


Since C# version 6, there is a comfortable way:

 Interaction.MsgBox($"Result:\n {tal1 * tal2}");

The '$' is telling the compiler to treat {tal1 * tal2} as an expression and insert the result into the string. More details about this so-called string interpolation:

With string interpolation, expressions within curly braces {} can also be evaluated. The result will be inserted at the corresponding location within the string. For example, to calculate the maximum of foo and bar and insert it, use Math.Max within the curly braces:

Console.WriteLine($"And the greater one is: {
Math.Max(foo, bar) }");

Output:

And the greater one is: 42

Note: Any leading or trailing whitespace (including space, tab and CRLF/newline) between the curly brace and the expression is completely ignored and not included in the output


For all C# versions (especially the ones before C#6) you can do the following:

Instead of having to invoke string.Format every time, you can create a function like

    public Microsoft.VisualBasic.MsgBoxResult MsgBox(string msg, params object[] p)
    {
        var fmt = string.Format(msg, p);
        var msgStyle = Microsoft.VisualBasic.MsgBoxStyle.OkOnly;
        return Interaction.MsgBox(fmt, msgStyle, "Information");
    }

This can be used as follows:

    var tal1=1; var tal2=2;
    MsgBox("Hello!\nResult:{0}\n", (tal1 + tal2));

Note: This solution supports not only {0}, but also {1}, {2}, ... because I have used a params array. Hence, the following is working too:

MsgBox("Calculation: {0} + {1} = {2}", tal1, tal2, (tal1+tal2));

which outputs Calculation: 1 + 2 = 3.

You can read here (at Microsoft) more about the Interaction.MsgBox method, how to change the style of the messagebox (e.g. how to display Yes/No/Cancel buttons and about the MsgBoxResult values being returned) so you can tailor it to your needs.


This is the complete program, you can run it in LinqPad or Visual Studio:

    static void Main(string[] args)
    {
            var tal1=1; var tal2=2;
            MsgBox("Hello!\nResult:{0}\n", (tal1 + tal2));
            MsgBox("Calculation: {0} + {1} = {2}", tal1, tal2, (tal1+tal2));
    }

    public static MsgBoxResult MsgBox(string msg, params object[] p)
    {
        var fmt=string.Format(msg, p);
        var msgStyle= Microsoft.VisualBasic.MsgBoxStyle.OkOnly;
        return Interaction.MsgBox(fmt, msgStyle, "Information");
    }

Don't forget to add a reference to Microsoft.VisualBasic.dll, using the namespace Microsoft.VisualBasic in order to run this example.

If you're using LinqPad, right-click, choose "Query properties..." then add the dll in "Additional references" and the namespace in "Additional Namespace imports".

LW001
  • 2,452
  • 6
  • 27
  • 36
Matt
  • 25,467
  • 18
  • 120
  • 187
1

Console.WriteLine has an overloads which supports a string.Format type implementation.

http://msdn.microsoft.com/en-us/library/586y06yf(v=vs.110).aspx

As pointed out in the comments, you will need to use string.Format() to replicate that behavior for Interaction.MsgBox.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
0

you can use the string format method http://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

Chino
  • 821
  • 6
  • 13