Home > Blockchain >  Build a Message
Build a Message

Time:06-10

I want to make a Message, using formatting, multiple lines and adding 3 arguments. But I'm having some trouble

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

I don't get this message below. Don't I need to convert the Id to a string to put it in a message?

System.InvalidCastException: 'Conversion from string "1 Name: {0}" to type 'Integer' is not valid.'

And whats up with this one, I can't have more than 2 arguments?

Too many arguments to '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 Active

CodePudding user response:

Use String.Format or string interpolation to insert the variables. You can insert line breaks that way too or use a multiline literal in the first place.

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)
  • Related