vb.net 如何在消息框中获取文本和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8630146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get text and a variable in a messagebox
提问by Mark Kramer
I just need to know how to have plain text and a variable in a messagebox.
我只需要知道如何在消息框中包含纯文本和变量。
For example:
例如:
I can do this: MsgBox(variable)
我可以做这个: MsgBox(variable)
And I can do this: MsgBox("Variable = ")
我可以这样做: MsgBox("Variable = ")
But I can't do this: MsgBox("Variable = " + variable)
但我不能这样做: MsgBox("Variable = " + variable)
回答by Ric
As has been suggested, using the string.format method is nice and simple and very readable.
正如所建议的那样,使用 string.format 方法既简单又好读。
In vb.net the " + " is used for addition and the " & " is used for string concatenation.
在 vb.net 中,“+”用于加法,“&”用于字符串连接。
In your example:
在你的例子中:
MsgBox("Variable = " + variable)
becomes:
变成:
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
我可能回答得有点快,因为看起来这些运算符都可以用于连接,但推荐使用的是“&”,来源http://msdn.microsoft.com/en-us/library/te2585xw(v =VS.100).aspx
maybe call
也许打电话
variable.ToString()
update:
更新:
Use string interpolation (vs2015 onwards I believe):
使用字符串插值(vs2015 以后我相信):
MsgBox($"Variable = {variable}")
回答by IAbstract
Why not use:
为什么不使用:
Dim msg as String = String.Format("Variable = {0}", variable)
More info on String.Format
有关String.Format 的更多信息
回答by Julieta
I kind of run into the same issue. I wanted my message box to display the message and the vendorcontractexpiration. This is what I did:
我遇到了同样的问题。我希望我的消息框显示消息和 vendorcontractexpiration。这就是我所做的:
Dim ab As String
Dim cd As String
ab = "THE CONTRACT FOR THIS VENDOR WILL EXPIRE ON "
cd = VendorContractExpiration
If InvoiceDate >= VendorContractExpiration - 120 And InvoiceDate < VendorContractExpiration Then
MsgBox [ab] & [cd], vbCritical, "WARNING"
End If
回答by Muhammad Saeed
MsgBox("Variable {0} " , variable)