如何在消息框中获取文本和变量 [英] How to get text and a variable in a messagebox
问题描述
我只需要知道如何在消息框中包含纯文本和变量即可.
I just need to know how to have plain text and a variable in a messagebox.
例如:
我可以这样做:MsgBox(variable)
我可以这样做:MsgBox("Variable = ")
但是我不能这样做:MsgBox("Variable = " + variable)
推荐答案
如所建议的那样,使用string.format方法既简单又易于理解.
As has been suggested, using the string.format method is nice and simple and very readable.
在vb.net中,"+"用于加法,&"用于字符串连接.
In vb.net the " + " is used for addition and the " & " is used for string concatenation.
在您的示例中:
MsgBox("Variable = " + variable)
成为:
MsgBox("Variable = " & variable)
我可能很快回答了这个问题,因为看起来这些运算符都可以用于级联,但是建议使用的是&",来源
I may have been a bit quick answering this as it appears these operators can both be used for concatenation, but recommended use is the "&", source http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.100).aspx
也许会打电话
variable.ToString()
更新:
使用字符串插值(我认为是2015年以后的版本):
Use string interpolation (vs2015 onwards I believe):
MsgBox($"Variable = {variable}")
这篇关于如何在消息框中获取文本和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!