我想制作一條訊息,使用格式、多行并添加 3 個引數。但我遇到了一些麻煩
Public Class vbList
'Declare
Dim users As IList(Of User) = New List(Of User)()
Public Sub New()
InitializeComponent()
users.Add(New User With {
.Id = 1,
.Name = "Suresh Dasari",
.Location = "Hyderabad"
})
MsgBox("Id: {0}", users.Item(0).Id.ToString() & vbCrLf & "Name: {0}", users.Item(0).Name) & vbCrLf & "Location: {0}", users.Item(0).Location)
End Sub
我沒有在下面收到此訊息。我不需要將 Id 轉換為字串以將其放入訊息中嗎?
System.InvalidCastException:“從字串“1 名稱:{0}”到型別“整數”的轉換無效。”
這個怎么回事,我不能有超過 2 個引數?
'Public Function MsgBox(Prompt As Object, [Buttons As MsgBoxStyle = ApplicationModal], [Title As Object = Nothing]) As MsgBoxResult'的引數太多。VSBasics C:\Users\ljhha\Documents\it\vb\VSBasics\VSBasics\vbList.vb 33 活動
uj5u.com熱心網友回復:
使用String.Format或字串插值來插入變數。您也可以以這種方式插入換行符,或者首先使用多行文字。
Dim str1 = String.Format("Date: {0}{1}Time: {2}", Date.Now.ToShortDateString(), Environment.NewLine, Date.Now.ToShortTimeString())
Dim str2 = $"Date: {Date.Now.ToShortDateString()}{Environment.NewLine}Time: {Date.Now.ToShortTimeString()}"
Dim str3 = $"Date: {Date.Now.ToShortDateString()}
Time: {Date.Now.ToShortTimeString()}"
MessageBox.Show(str1)
MessageBox.Show(str2)
MessageBox.Show(str3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490846.html
標籤:VB.net
